Export to GitHub

vatnumber - issue #20

VAT number is incorrectly evaluated as a negative integer in the try: int(vat)


Posted on Oct 17, 2011 by Massive Ox

Hi,

The VAT number is correctly evaluated as a negative integer in the try: int(vat) which is used for a lot of countries.

However, there should have been a False return as later on the following error occurs: "ValueError: invalid literal for int() with base 10: '-'"

I think this can be solved with a check on a negative integer. And already doing so a check on an integer equal to 0 is also useful.

What steps will reproduce the problem? See below for a code example.

What is the expected output? What do you see instead? A False return from function check_vat with VAT number RO-7793957 and not a hard error.

What version of the product are you using? On what operating system? 1.0

Please provide any additional information below. ''' Start of example code snippet ''' def check_vat_ro(vat): ''' Check Romania VAT number. ''' try: int(vat) except ValueError: return False # New code to evaluate a negative integer(and 0) if int(vat) <= 0: return False # End successfully when int(vat) > 0 else: return True

def check_vat(vat): ''' Check VAT number. ''' code = vat[:2].lower() print(code) number = vat[2:] print(number) try: checker = globals()['check_vat_%s' % code] except KeyError: return False return checker(number)

Negative integer example

vat_code_in = 'RO-7793957'

Incorrect string example

vat_code_in = 'RO/7793957'

print(check_vat(vat_code_in))

''' End of example code snippet '''

Comment #1

Posted on Oct 18, 2011 by Massive Ox

Or what I realized later on, the expected input is something like 'RO7793957' and the input should be sanitized/rejected before calling the function vatnumber.

Am I correct in this assumption?

Sanitizing is not so difficult. Something like upper() and regex ['rA-Z0-9] and compare to original input would solve this too.

Comment #2

Posted on Nov 8, 2012 by Helpful Kangaroo

Fixed in r98719bed743b

Status: Fixed

Labels:
Type-Defect Priority-Medium