-
Notifications
You must be signed in to change notification settings - Fork 158
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
tabbols95
wants to merge
13
commits into
python-validators:master
Choose a base branch
from
tabbols95:validators_inn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b60cd0
Add script validators inn.py
tabbols95 f4dd913
Validators inn company
tabbols95 20bf7eb
weight coefficients from company
tabbols95 3fd2d39
validators inn person
tabbols95 148c491
update __init__.py
tabbols95 f34f7e3
update .gitignore
tabbols95 94d66cf
drop comment
tabbols95 9cd8abe
Update weights && add description function
tabbols95 6f80d56
Add link validator algorithm
tabbols95 1bc9f46
Add decorator validator
tabbols95 5808f70
Moved all files to the i18n directory.
tabbols95 abea471
add tests
tabbols95 5828572
rename function `inn` -> `ru_inn`
tabbols95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Inn.""" | ||
|
||
from src.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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: