Skip to content

Commit 9bed104

Browse files
committed
Add typing checks
1 parent 5ed6efc commit 9bed104

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

.github/workflows/python-package.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
python -m pip install --upgrade pip
2525
python -m pip install testtools
2626
python -m pip install ruff
27+
python -m pip install mypy
2728
- name: Test with testtools
2829
run: |
2930
python -m testtools.run extras.tests.test_suite
@@ -33,3 +34,6 @@ jobs:
3334
- name: Check formatting with ruff
3435
run: |
3536
python -m ruff format --check .
37+
- name: Type check with mypy
38+
run: |
39+
python -m mypy --strict --ignore-missing-imports --follow-imports=skip --exclude tests .

extras/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Extensions to the Python standard library."""
44

55
import sys
6+
from typing import Optional, Callable, Sequence
67

78
__all__ = [
89
"try_import",
@@ -24,7 +25,11 @@
2425
__version__ = (1, 0, 0, "final", 0)
2526

2627

27-
def try_import(name, alternative=None, error_callback=None):
28+
def try_import(
29+
name: str,
30+
alternative: Optional[object] = None,
31+
error_callback: Optional[Callable[[ImportError], None]] = None,
32+
) -> object:
2833
"""Attempt to import ``name``. If it fails, return ``alternative``.
2934
3035
When supporting multiple versions of Python or optional dependencies, it
@@ -38,7 +43,7 @@ def try_import(name, alternative=None, error_callback=None):
3843
when the module cannot be loaded.
3944
"""
4045
module_segments = name.split(".")
41-
last_error = None
46+
last_error: Optional[ImportError] = None
4247
remainder = []
4348
# module_name will be what successfully imports. We cannot walk from the
4449
# __import__ result because in import loops (A imports A.B, which imports
@@ -48,7 +53,7 @@ def try_import(name, alternative=None, error_callback=None):
4853
try:
4954
__import__(module_name)
5055
except ImportError:
51-
last_error = sys.exc_info()[1]
56+
last_error = sys.exc_info()[1] # type: ignore
5257
remainder.append(module_segments.pop())
5358
continue
5459
else:
@@ -60,7 +65,7 @@ def try_import(name, alternative=None, error_callback=None):
6065
module = sys.modules[module_name]
6166
nonexistent = object()
6267
for segment in reversed(remainder):
63-
module = getattr(module, segment, nonexistent)
68+
module = getattr(module, segment, nonexistent) # type: ignore
6469
if module is nonexistent:
6570
if last_error is not None and error_callback is not None:
6671
error_callback(last_error)
@@ -71,7 +76,11 @@ def try_import(name, alternative=None, error_callback=None):
7176
_RAISE_EXCEPTION = object()
7277

7378

74-
def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None):
79+
def try_imports(
80+
module_names: Sequence[str],
81+
alternative: object = _RAISE_EXCEPTION,
82+
error_callback: Optional[Callable[[ImportError], None]] = None,
83+
) -> object:
7584
"""Attempt to import modules.
7685
7786
Tries to import the first module in ``module_names``. If it can be

extras/py.typed

Whitespace-only changes.

setup.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""Distutils installer for extras."""
33

4-
from setuptools import setup
4+
from setuptools import setup, Command
55
import os.path
6+
from typing import cast
67

78
import extras
89

9-
testtools_cmd = extras.try_import("testtools.TestCommand")
10+
testtools_cmd = cast(Command, extras.try_import("testtools.TestCommand"))
1011

1112

12-
def get_version():
13+
def get_version() -> str:
1314
"""Return the version of extras that we are building."""
1415
version = ".".join(str(component) for component in extras.__version__[0:3])
1516
return version
1617

1718

18-
def get_long_description():
19+
def get_long_description() -> str:
1920
readme_path = os.path.join(os.path.dirname(__file__), "README.rst")
20-
return open(readme_path).read()
21+
with open(readme_path) as f:
22+
return f.read()
2123

2224

23-
cmdclass = {}
25+
cmdclass: dict[str, type[Command]] = {}
2426

2527
if testtools_cmd is not None:
2628
cmdclass["test"] = testtools_cmd
@@ -53,5 +55,6 @@ def get_long_description():
5355
"extras",
5456
"extras.tests",
5557
],
58+
package_data={"extras": ["py.typed"]},
5659
cmdclass=cmdclass,
5760
)

0 commit comments

Comments
 (0)