Skip to content

Commit 7688f20

Browse files
authored
Merge pull request #15 from hugovk/typing
2 parents 410b51d + f550e98 commit 7688f20

13 files changed

+264
-177
lines changed

.github/workflows/docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
uses: actions/setup-python@v3
1414
with:
1515
python-version: "3.x"
16-
pip: cache
17-
pip-dependency-path: tox.ini
16+
cache: pip
17+
cache-dependency-path: tox.ini
1818

1919
- name: Install dependencies
2020
run: |

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ repos:
5656
args: ["--convention", "google"]
5757
files: "src/"
5858

59+
- repo: https://github.com/pre-commit/mirrors-mypy
60+
rev: v0.942
61+
hooks:
62+
- id: mypy
63+
additional_dependencies: [pytest, types-freezegun, types-setuptools]
64+
args: [--strict]
65+
5966
- repo: https://github.com/asottile/setup-cfg-fmt
6067
rev: v1.20.1
6168
hooks:

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mkdocs>=1.1
22
mkdocs-material
3-
mkdocstrings[python-legacy]>=0.18
3+
mkdocstrings[python]>=0.18
44
mkdocs-include-markdown-plugin
55
pygments
66
pymdown-extensions>=9.2

src/humanize/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import importlib.metadata as importlib_metadata
2525
except ImportError:
2626
# <Python 3.7 and lower
27-
import importlib_metadata
27+
import importlib_metadata # type: ignore
2828

2929
__version__ = importlib_metadata.version(__name__)
3030

src/humanize/filesize.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
"""Bits and bytes related humanization."""
4+
from __future__ import annotations
45

56
suffixes = {
67
"decimal": ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
@@ -9,7 +10,12 @@
910
}
1011

1112

12-
def naturalsize(value, binary=False, gnu=False, format="%.1f"):
13+
def naturalsize(
14+
value: float | str,
15+
binary: bool = False,
16+
gnu: bool = False,
17+
format: str = "%.1f",
18+
) -> str:
1319
"""Format a number of bytes like a human readable filesize (e.g. 10 kB).
1420
1521
By default, decimal suffixes (kB, MB) are used.

src/humanize/i18n.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"""Activate, get and deactivate translations."""
2+
from __future__ import annotations
3+
24
import gettext as gettext_module
35
import os.path
46
from threading import local
57

68
__all__ = ["activate", "deactivate", "thousands_separator"]
79

8-
_TRANSLATIONS = {None: gettext_module.NullTranslations()}
10+
_TRANSLATIONS: dict[str | None, gettext_module.NullTranslations] = {
11+
None: gettext_module.NullTranslations()
12+
}
913
_CURRENT = local()
1014

1115

@@ -15,7 +19,7 @@
1519
}
1620

1721

18-
def _get_default_locale_path():
22+
def _get_default_locale_path() -> str | None:
1923
try:
2024
if __file__ is None:
2125
return None
@@ -24,14 +28,14 @@ def _get_default_locale_path():
2428
return None
2529

2630

27-
def get_translation():
31+
def get_translation() -> gettext_module.NullTranslations:
2832
try:
2933
return _TRANSLATIONS[_CURRENT.locale]
3034
except (AttributeError, KeyError):
3135
return _TRANSLATIONS[None]
3236

3337

34-
def activate(locale, path=None):
38+
def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
3539
"""Activate internationalisation.
3640
3741
Set `locale` as current locale. Search for locale in directory `path`.
@@ -61,12 +65,12 @@ def activate(locale, path=None):
6165
return _TRANSLATIONS[locale]
6266

6367

64-
def deactivate():
68+
def deactivate() -> None:
6569
"""Deactivate internationalisation."""
6670
_CURRENT.locale = None
6771

6872

69-
def _gettext(message):
73+
def _gettext(message: str) -> str:
7074
"""Get translation.
7175
7276
Args:
@@ -78,7 +82,7 @@ def _gettext(message):
7882
return get_translation().gettext(message)
7983

8084

81-
def _pgettext(msgctxt, message):
85+
def _pgettext(msgctxt: str, message: str) -> str:
8286
"""Fetches a particular translation.
8387
8488
It works with `msgctxt` .po modifiers and allows duplicate keys with different
@@ -103,13 +107,13 @@ def _pgettext(msgctxt, message):
103107
return message if translation == key else translation
104108

105109

106-
def _ngettext(message, plural, num):
110+
def _ngettext(message: str, plural: str, num: int) -> str:
107111
"""Plural version of _gettext.
108112
109113
Args:
110114
message (str): Singular text to translate.
111115
plural (str): Plural text to translate.
112-
num (str): The number (e.g. item count) to determine translation for the
116+
num (int): The number (e.g. item count) to determine translation for the
113117
respective grammatical number.
114118
115119
Returns:
@@ -118,7 +122,7 @@ def _ngettext(message, plural, num):
118122
return get_translation().ngettext(message, plural, num)
119123

120124

121-
def _gettext_noop(message):
125+
def _gettext_noop(message: str) -> str:
122126
"""Mark a string as a translation string without translating it.
123127
124128
Example usage:
@@ -137,7 +141,7 @@ def num_name(n):
137141
return message
138142

139143

140-
def _ngettext_noop(singular, plural):
144+
def _ngettext_noop(singular: str, plural: str) -> tuple[str, str]:
141145
"""Mark two strings as pluralized translations without translating them.
142146
143147
Example usage:
@@ -154,7 +158,7 @@ def num_name(n):
154158
Returns:
155159
tuple: Original text, unchanged.
156160
"""
157-
return (singular, plural)
161+
return singular, plural
158162

159163

160164
def thousands_separator() -> str:

0 commit comments

Comments
 (0)