Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/fr
diff options
context:
space:
mode:
authorKevin Dagostino <kevin@tonkworks.com>2023-12-15 18:59:26 +0100
committerArthur de Jong <arthur@arthurdejong.org>2024-02-25 17:31:24 +0100
commit9c7c669ece1491e7fd697f2184ad9b7185be59b2 (patch)
tree31bab155348b3ab6df070cb6fd828cf068a106d6 /stdnum/fr
parent1e412ee066afaa0861b4885604aa6106a3c4c879 (diff)
Imporve French NIF validation (checksum)
The last 3 digits are a checksum. % 511 https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx Closes https://github.com/arthurdejong/python-stdnum/pull/426
Diffstat (limited to 'stdnum/fr')
-rw-r--r--stdnum/fr/nif.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/stdnum/fr/nif.py b/stdnum/fr/nif.py
index 4d57e4e..d043674 100644
--- a/stdnum/fr/nif.py
+++ b/stdnum/fr/nif.py
@@ -30,8 +30,12 @@ More information:
* https://ec.europa.eu/taxation_customs/tin/tinByCountry.html
* https://fr.wikipedia.org/wiki/Numéro_d%27Immatriculation_Fiscale#France
->>> validate('0701987765432')
-'0701987765432'
+>>> validate('3023217600053')
+'3023217600053'
+>>> validate('3023217600054')
+Traceback (most recent call last):
+ ...
+InvalidChecksum: ...
>>> validate('070198776543')
Traceback (most recent call last):
...
@@ -40,8 +44,8 @@ InvalidLength: ...
Traceback (most recent call last):
...
InvalidComponent: ...
->>> format('0701987765432')
-'07 01 987 765 432'
+>>> format('3023217600053')
+'30 23 217 600 053'
"""
from stdnum.exceptions import *
@@ -54,6 +58,11 @@ def compact(number):
return clean(number, ' ').strip()
+def calc_check_digits(number):
+ """Calculate the check digits for the number."""
+ return '%03d' % (int(number[:10]) % 511)
+
+
def validate(number):
"""Check if the number provided is a valid NIF."""
number = compact(number)
@@ -63,6 +72,8 @@ def validate(number):
raise InvalidComponent()
if len(number) != 13:
raise InvalidLength()
+ if calc_check_digits(number) != number[-3:]:
+ raise InvalidChecksum()
return number