Skip to content

Commit 3960c5b

Browse files
committed
Add support for Mauritius TIN
Fixes #356
1 parent 6d366e3 commit 3960c5b

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

stdnum/mu/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding: utf-8
33
#
44
# Copyright (C) 2018 Arthur de Jong
5+
# Copyright (C) 2023 Leandro Regueiro
56
#
67
# This library is free software; you can redistribute it and/or
78
# modify it under the terms of the GNU Lesser General Public
@@ -19,3 +20,6 @@
1920
# 02110-1301 USA
2021

2122
"""Collection of Mauritian numbers."""
23+
24+
# provide aliases
25+
from stdnum.mu import tan as vat # noqa: F401

stdnum/mu/tan.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# tan.py - functions for handling Mauritius TAN numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Leandro Regueiro
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""TAN (Tax Account Number, Mauritius tax number).
22+
23+
This number consists of 8 digits. For individuals the first digit is always 1,
24+
5, 7 or 8. For entities the first digit is always 2 or 3.
25+
26+
More information:
27+
28+
* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Mauritius-TIN.pdf
29+
30+
>>> validate('20405553')
31+
'20405553'
32+
>>> validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> format('20405553')
37+
'20405553'
38+
""" # noqa: E501
39+
40+
from stdnum.exceptions import *
41+
from stdnum.util import clean, isdigits
42+
43+
44+
def compact(number):
45+
"""Convert the number to the minimal representation.
46+
47+
This strips the number of any valid separators and removes surrounding
48+
whitespace.
49+
"""
50+
return clean(number, ' -').strip()
51+
52+
53+
def validate(number):
54+
"""Check if the number is a valid Mauritius TAN number.
55+
56+
This checks the length and formatting.
57+
"""
58+
number = compact(number)
59+
if len(number) != 8:
60+
raise InvalidLength()
61+
if not isdigits(number):
62+
raise InvalidFormat()
63+
if number[0] not in ('1', '2', '3', '5', '7', '8'):
64+
raise InvalidComponent()
65+
return number
66+
67+
68+
def is_valid(number):
69+
"""Check if the number is a valid Mauritius TAN number."""
70+
try:
71+
return bool(validate(number))
72+
except ValidationError:
73+
return False
74+
75+
76+
def format(number):
77+
"""Reformat the number to the standard presentation format."""
78+
return compact(number)

tests/test_mu_tan.doctest

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
test_mu_tan.doctest - more detailed doctests for stdnum.mu.tan module
2+
3+
Copyright (C) 2023 Leandro Regueiro
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.mu.tan module. It
22+
tries to test more corner cases and detailed functionality that is not really
23+
useful as module documentation.
24+
25+
>>> from stdnum.mu import tan
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> tan.validate('20405553')
31+
'20405553'
32+
>>> tan.validate('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> tan.validate('1234567V')
37+
Traceback (most recent call last):
38+
...
39+
InvalidFormat: ...
40+
>>> tan.validate('42345678')
41+
Traceback (most recent call last):
42+
...
43+
InvalidComponent: ...
44+
>>> tan.format('20405553')
45+
'20405553'
46+
47+
48+
These have been found online and should all be valid numbers.
49+
50+
>>> numbers = '''
51+
...
52+
... 20405553
53+
... 18421000
54+
...
55+
... '''
56+
>>> [x for x in numbers.splitlines() if x and not tan.is_valid(x)]
57+
[]

0 commit comments

Comments
 (0)