Skip to content

Commit

Permalink
Strict typing and py.typed
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Aug 26, 2024
1 parent 8f50465 commit 804adb5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jaraco/test/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions jaraco/test/cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
"""
Expand Down
18 changes: 10 additions & 8 deletions jaraco/test/http.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Expand All @@ -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]
Empty file added jaraco/test/py.typed
Empty file.
7 changes: 6 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,3 +12,8 @@ explicit_package_bases = True

# Disable overload-overlap due to many false-positives
disable_error_code = 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
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ doc = [
"sphinx-lint",

# local
"pytest >= 6, != 8.1.*" # We expose pytest types, Sphinx needs to be able to find it
]

check = [
Expand Down Expand Up @@ -73,7 +74,3 @@ pytest11 = {"jaraco.test.http" = "jaraco.test.http"}


[tool.setuptools_scm]


[tool.pytest-enabler.mypy]
# Disabled due to jaraco/skeleton#143
5 changes: 3 additions & 2 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
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.
"""
urllib.request.urlopen('http://pypi.org/')


@pytest.mark.network
def test_network_marker():
def test_network_marker() -> None:
"""
This test should always succeed or be skipped.
"""
Expand Down

0 comments on commit 804adb5

Please sign in to comment.