<commit_before>from django import forms

from vat import validate

class VATField(forms.CharField):
    """
    >>> f = VATField(external_validation=True)
    >>> f.clean("SE556056625801")
    'SE556056625801'
    """
    def __init__(self, external_validation=False, required=True):
        self.external_validation = external_validation
        super(VATField, self).__init__(required=required, min_length=3)

    def clean(self, value):
        if not value:
            return value

        try:
            is_valid, vat_number = validate.validate(value,
                                                     self.external_validation)
        except validate.CommunicationError:
            raise forms.ValidationError(u"Unable to contact VAT validation "
                                        u"service.")
        if not is_valid:
            raise forms.ValidationError(u"Not a valid VAT number.")

        return vat_number

if __name__ == '__main__':
    import doctest
    doctest.testmod()<commit_msg>Added support for superclass parameters.<commit_after>from django import forms

from vat import validate

class VATField(forms.CharField):
    """
    >>> f = VATField(external_validation=True)
    >>> f.clean("SE556056625801")
    'SE556056625801'
    """
    def __init__(self, external_validation=False, *args, **kwargs):
        self.external_validation = external_validation
        kwargs['min_length'] = 3
        super(VATField, self).__init__(*args, **kwargs)

    def clean(self, value):
        if not value:
            return value

        try:
            is_valid, vat_number = validate.validate(value,
                                                     self.external_validation)
        except validate.CommunicationError:
            raise forms.ValidationError(u"Unable to contact VAT validation "
                                        u"service.")
        if not is_valid:
            raise forms.ValidationError(u"Not a valid VAT number.")

        return vat_number

if __name__ == '__main__':
    import doctest
    doctest.testmod()
{'fraction_non_alphanumeric': 0.08775313404050145, 'fraction_numerical': 0.024108003857280617, 'mean_word_length': 2.143939393939394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1012965', 'n_tokens_mistral': 581, 'n_tokens_neox': 533, 'n_words': 130}