From 81a4e4cea5dd2066ef57870fb076ab4c9a66e8a2 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 26 Aug 2024 01:50:40 -0400 Subject: [PATCH] Strict typing and py.typed --- docs/conf.py | 6 ++++++ jaraco/test/__init__.py | 2 +- jaraco/test/cpython.py | 6 +++--- jaraco/test/http.py | 18 ++++++++++-------- jaraco/test/py.typed | 0 mypy.ini | 7 ++++++- newsfragments/8.feature.rst | 1 + pyproject.toml | 4 ---- tests/test_http.py | 5 +++-- 9 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 jaraco/test/py.typed create mode 100644 newsfragments/8.feature.rst diff --git a/docs/conf.py b/docs/conf.py index 240329c..4283ca5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -54,3 +54,9 @@ extensions += ['sphinx.ext.extlinks'] # local + +# jaraco/jaraco.test#8 +nitpick_ignore += [ + ('py:class', '_pytest.config.Config'), + ('py:class', '_pytest.nodes.Item'), +] diff --git a/jaraco/test/__init__.py b/jaraco/test/__init__.py index 9c77a06..3851bbd 100644 --- a/jaraco/test/__init__.py +++ b/jaraco/test/__init__.py @@ -1,7 +1,7 @@ import sys -def property_error(name): +def property_error(name: str) -> str: """ Generate a regular expression for capturing the expected error messages that can result from attempting to set diff --git a/jaraco/test/cpython.py b/jaraco/test/cpython.py index 47a1ffc..005573a 100644 --- a/jaraco/test/cpython.py +++ b/jaraco/test/cpython.py @@ -10,11 +10,11 @@ import importlib import types -from jaraco.context import suppress from jaraco.collections import Projection +from jaraco.context import suppress -def from_test_support(*names): +def from_test_support(*names: str) -> types.SimpleNamespace: """ Return a SimpleNamespace of names from test.support. @@ -28,7 +28,7 @@ def from_test_support(*names): @suppress(ImportError) -def try_import(name): +def try_import(name: str) -> types.ModuleType: """ Attempt to import a submodule of test.support; return None if missing. """ diff --git a/jaraco/test/http.py b/jaraco/test/http.py index 5eeddab..96447c6 100644 --- a/jaraco/test/http.py +++ b/jaraco/test/http.py @@ -1,39 +1,41 @@ import urllib.request import pytest + import jaraco.functools from jaraco.context import ExceptionTrap @jaraco.functools.once -def has_internet(): +def has_internet() -> bool: """ Is this host able to reach the Internet? Return True if the internet appears reachable and False otherwise. """ - with ExceptionTrap() as trap: + with ExceptionTrap() as trap: # type: ignore[no-untyped-call] # jaraco/jaraco.context#15 urllib.request.urlopen('http://pypi.org') return not trap -def check_internet(): +def check_internet() -> None: """ (pytest) Skip if internet is unavailable. """ - has_internet() or pytest.skip('Internet connectivity unavailable') + if not has_internet(): + pytest.skip('Internet connectivity unavailable') @pytest.fixture -def needs_internet(): +def needs_internet() -> None: """ Pytest fixture signaling that internet is required. """ check_internet() -def pytest_configure(config): +def pytest_configure(config: pytest.Config) -> None: """ Register the 'network' marker. """ @@ -42,9 +44,9 @@ def pytest_configure(config): ) -def pytest_runtest_setup(item): +def pytest_runtest_setup(item: pytest.Item) -> None: """ For any tests marked with 'network', install fixture. """ for marker in item.iter_markers(name='network'): - item.fixturenames.extend({'needs_internet'} - set(item.fixturenames)) + item.fixturenames.extend({'needs_internet'} - set(item.fixturenames)) # type: ignore[attr-defined] diff --git a/jaraco/test/py.typed b/jaraco/test/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/mypy.ini b/mypy.ini index efcb8cb..ef7f254 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,6 @@ [mypy] # Is the project well-typed? -strict = False +strict = True # Early opt-in even when strict = False warn_unused_ignores = True @@ -13,3 +13,8 @@ explicit_package_bases = True disable_error_code = # Disable due to many false positives overload-overlap, + +# Can't ignore inline because the error is inconsistent +# Exists but untyped on Python 3.8 on Windows. Doesn't exist anywhere else. +[mypy-test.*] +ignore_missing_imports = True diff --git a/newsfragments/8.feature.rst b/newsfragments/8.feature.rst new file mode 100644 index 0000000..88e9b7c --- /dev/null +++ b/newsfragments/8.feature.rst @@ -0,0 +1 @@ +Complete annotations and add ``py.typed`` marker -- by :user:`Avasam` diff --git a/pyproject.toml b/pyproject.toml index c49594b..38e7fa5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,7 +73,3 @@ pytest11 = {"jaraco.test.http" = "jaraco.test.http"} [tool.setuptools_scm] - - -[tool.pytest-enabler.mypy] -# Disabled due to jaraco/skeleton#143 diff --git a/tests/test_http.py b/tests/test_http.py index 972f6bc..28bf1f2 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -3,7 +3,8 @@ import pytest -def test_needs_internet(needs_internet): +@pytest.mark.usefixtures("needs_internet") +def test_needs_internet() -> None: """ This test should always succeed or be skipped. """ @@ -11,7 +12,7 @@ def test_needs_internet(needs_internet): @pytest.mark.network -def test_network_marker(): +def test_network_marker() -> None: """ This test should always succeed or be skipped. """