Skip to content

Commit 0e857fb

Browse files
Daverballarthurdejong
authored andcommitted
Add type hints
Closes #467 Closes #433
1 parent bc689fd commit 0e857fb

File tree

320 files changed

+2058
-1235
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

320 files changed

+2058
-1235
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
strategy:
3131
fail-fast: false
3232
matrix:
33-
tox_job: [docs, flake8, headers]
33+
tox_job: [docs, flake8, mypy, headers]
3434
steps:
3535
- uses: actions/checkout@v3
3636
- name: Set up Python

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ __pycache__
66

77
# /
88
/.coverage
9+
/.mypy_cache
10+
/.ruff_cache
911
/.tox
12+
/.venv
1013
/build
1114
/coverage
1215
/dist

docs/index.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Common Interface
99

1010
Most of the number format modules implement the following functions:
1111

12-
.. function:: module.validate(number)
12+
.. function:: module.validate(number: str) -> str
1313

1414
Validate the number and return a compact, consistent representation of
1515
the number or code. If the validation fails,
@@ -19,23 +19,23 @@ Most of the number format modules implement the following functions:
1919
:raises ValidationError: When the specified number is invalid
2020
:returns: str -- A compact (canonical) representation of the number
2121

22-
.. function:: module.is_valid(number)
22+
.. function:: module.is_valid(number: str) -> bool
2323

2424
Return either ``True`` or ``False`` depending on whether the passed number
2525
is in any supported and valid form and passes all embedded checks of the
2626
number. This function should never raise an exception.
2727

2828
:returns: bool -- ``True`` if validated, ``False`` otherwise
2929

30-
.. function:: module.compact(number)
30+
.. function:: module.compact(number: str) -> str
3131

3232
Return a compact representation of the number or code. This function
3333
generally does not do validation but may raise exceptions for wildly
3434
invalid numbers.
3535

3636
:returns: str -- The compacted number
3737

38-
.. function:: module.format(number)
38+
.. function:: module.format(number: str) -> str
3939

4040
Return a formatted version of the number in the preferred format.
4141
This function generally expects to be passed a valid number or code and
@@ -45,15 +45,15 @@ Most of the number format modules implement the following functions:
4545

4646
The check digit modules generally also provide the following functions:
4747

48-
.. function:: module.checksum(number)
48+
.. function:: module.checksum(number: str) -> int
4949

5050
Calculate the checksum over the provided number. This is generally a
5151
number that can be used to determine whether the provided number is
5252
valid. It depends on the algorithm which checksum is considered valid.
5353

5454
:returns: int -- A numeric checksum over the number
5555

56-
.. function:: module.calc_check_digit(number)
56+
.. function:: module.calc_check_digit(number: str) -> str
5757

5858
Calculate the check digit that should be added to the number to make it
5959
valid.

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ max-complexity = 15
3636
max-line-length = 120
3737
extend-exclude =
3838
.github
39+
.mypy_cache
3940
.pytest_cache
41+
.ruff_cache
42+
.venv
4043
build
4144

4245
[isort]
@@ -47,3 +50,8 @@ known_third_party =
4750
openpyxl
4851
requests
4952
xlrd
53+
54+
[mypy]
55+
python_version = 3.9
56+
strict = True
57+
warn_unreachable = True

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
],
7777
packages=find_packages(),
7878
install_requires=[],
79-
package_data={'': ['*.dat', '*.crt']},
79+
package_data={'': ['*.dat', '*.crt', 'py.typed']},
8080
extras_require={
8181
# The SOAP feature is only required for a number of online tests
8282
# of numbers such as the EU VAT VIES lookup, the Dominican Republic

stdnum/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
parsing, validation, formatting or conversion functions.
3838
"""
3939

40+
from __future__ import annotations
41+
4042
from stdnum.util import get_cc_module
4143

4244

stdnum/ad/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020

2121
"""Collection of Andorran numbers."""
2222

23+
from __future__ import annotations
24+
2325
# Provide aliases.
2426
from stdnum.ad import nrt as vat # noqa: F401

stdnum/ad/nrt.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
'D-059888-N'
4545
""" # noqa: E501
4646

47+
from __future__ import annotations
48+
4749
from stdnum.exceptions import *
4850
from stdnum.util import clean, isdigits
4951

5052

51-
def compact(number):
53+
def compact(number: str) -> str:
5254
"""Convert the number to the minimal representation.
5355
5456
This strips the number of any valid separators and removes surrounding
@@ -57,7 +59,7 @@ def compact(number):
5759
return clean(number, ' -.').upper().strip()
5860

5961

60-
def validate(number):
62+
def validate(number: str) -> str:
6163
"""Check if the number is a valid Andorra NRT number.
6264
6365
This checks the length, formatting and other constraints. It does not check
@@ -79,15 +81,15 @@ def validate(number):
7981
return number
8082

8183

82-
def is_valid(number):
84+
def is_valid(number: str) -> bool:
8385
"""Check if the number is a valid Andorra NRT number."""
8486
try:
8587
return bool(validate(number))
8688
except ValidationError:
8789
return False
8890

8991

90-
def format(number):
92+
def format(number: str) -> str:
9193
"""Reformat the number to the standard presentation format."""
9294
number = compact(number)
9395
return '-'.join([number[0], number[1:-1], number[-1]])

stdnum/al/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020

2121
"""Collection of Albanian numbers."""
2222

23+
from __future__ import annotations
24+
2325
# provide vat as an alias
2426
from stdnum.al import nipt as vat # noqa: F401

stdnum/al/nipt.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
InvalidFormat: ...
5151
"""
5252

53+
from __future__ import annotations
54+
5355
import re
5456

5557
from stdnum.exceptions import *
@@ -60,7 +62,7 @@
6062
_nipt_re = re.compile(r'^[A-M][0-9]{8}[A-Z]$')
6163

6264

63-
def compact(number):
65+
def compact(number: str) -> str:
6466
"""Convert the number to the minimal representation. This strips the
6567
number of any valid separators and removes surrounding whitespace."""
6668
number = clean(number, ' ').upper().strip()
@@ -71,7 +73,7 @@ def compact(number):
7173
return number
7274

7375

74-
def validate(number):
76+
def validate(number: str) -> str:
7577
"""Check if the number is a valid VAT number. This checks the length and
7678
formatting."""
7779
number = compact(number)
@@ -82,7 +84,7 @@ def validate(number):
8284
return number
8385

8486

85-
def is_valid(number):
87+
def is_valid(number: str) -> bool:
8688
"""Check if the number is a valid VAT number."""
8789
try:
8890
return bool(validate(number))

0 commit comments

Comments
 (0)