Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator russian individual tax number #408

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# VSCode
.vscode/
Expand Down
Empty file added src/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions src/validators/i18n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .fi import fi_business_id, fi_ssn
from .fr import fr_department, fr_ssn
from .ind import ind_aadhar, ind_pan
from .ru_inn import ru_inn

__all__ = (
"fi_business_id",
Expand All @@ -17,4 +18,6 @@
"fr_ssn",
"ind_aadhar",
"ind_pan",
# Russian Individual Tax Number
"ru_inn"
)
53 changes: 53 additions & 0 deletions src/validators/i18n/ru_inn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Inn."""

from src.validators.utils import validator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import directly from validators, everywhere:

from validators.utils import validator



@validator
def ru_inn(value: str, /):
"""Return whether or not given value is a valid russian individual tax number.

This validator is algorithm [1].

[1]: https://ru.wikipedia.org/wiki/Идентификационный_номер_налогоплательщика

Examples:
>>> inn('7736050003')
# Output: True
>>> inn('781100086042')
# Output: True

Args:
value:
Individual tax number string to validate

Returns:
(Literal[True]): If `value` is a valid russian individual tax number.
(ValidationError): If `value` is an invalid russian individual tax number.

Returns:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format your files. Double check your docstrings. Refer other modules.


"""
if not value:
return False

try:
digits = list(map(int, value))
# company
if len(digits) == 10:
weight_coefs = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
control_number = sum([d * w for d, w in zip(digits, weight_coefs)]) % 11
return (control_number % 10) == digits[-1] if control_number > 9 else control_number == digits[-1]
# person
elif len(digits) == 12:
weight_coefs1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0, 0]
control_number1 = sum([d * w for d, w in zip(digits, weight_coefs1)]) % 11
weight_coefs2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0]
control_number2 = sum([d * w for d, w in zip(digits, weight_coefs2)]) % 11
print(control_number1, control_number2, value)
return ((control_number1 % 10) == digits[-2] if control_number1 > 9 else control_number1 == digits[-2] and
(control_number2 % 10) == digits[-1] if control_number2 > 9 else control_number2 == digits[-1])
else:
return False
except ValueError:
return False
48 changes: 48 additions & 0 deletions tests/i18n/test_ru_inn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Test i18n/inn."""

# external
import pytest

# local
from src.validators import ValidationError
from src.validators.i18n import ru_inn


@pytest.mark.parametrize(
("value",),
[
("2222058686",),
("7709439560",),
("5003052454",),
("7730257499",),
("3664016814",),
("026504247480",),
("780103209220",),
("7707012148",),
("140700989885",),
("774334078053",),
],
)
def test_returns_true_on_valid_ru_inn(value: str):
"""Test returns true on valid russian individual tax number"""
assert ru_inn(value)


@pytest.mark.parametrize(
("value",),
[
("2222058687",),
("7709439561",),
("5003052453",),
("7730257490",),
("3664016815",),
("026504247481",),
("780103209222",),
("7707012149",),
("140700989886",),
("774334078054",),
],
)
def test_returns_false_on_valid_ru_inn(value: str):
"""Test returns true on valid russian individual tax number"""
assert isinstance(ru_inn(value), ValidationError)
Loading