Skip to content

Commit

Permalink
Add type-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Feb 11, 2024
1 parent 642792b commit 4452f68
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
include:
- python-version: '3.8'
toxenv: lint
- python-version: '3.8'
toxenv: typing
steps:
- name: Check out repository
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v0.5.0 (in development)
- Don't cause an internal error on marker misuse with pluggy 1.4+
- Drop support for pytest 6
- Require pluggy 1.1+
- Add type-checking

v0.4.0 (2023-10-21)
-------------------
Expand Down
21 changes: 19 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ classifiers = [
"Framework :: Pytest",
"Intended Audience :: Developers",
"Topic :: Software Development :: Testing",
"Typing :: Typed",
]

dependencies = [
Expand All @@ -50,12 +51,12 @@ fail-slow = "pytest_fail_slow"
"Bug Tracker" = "https://github.com/jwodder/pytest-fail-slow/issues"

[tool.hatch.version]
path = "pytest_fail_slow.py"
path = "src/pytest_fail_slow/__init__.py"

[tool.hatch.build.targets.sdist]
include = [
"/docs",
"/pytest_fail_slow.py",
"/src",
"/test",
"CHANGELOG.*",
"CONTRIBUTORS.*",
Expand All @@ -64,3 +65,19 @@ include = [

[tool.hatch.envs.default]
python = "3"

[tool.mypy]
allow_incomplete_defs = false
allow_untyped_defs = false
ignore_missing_imports = false
# <https://github.com/python/mypy/issues/7773>:
no_implicit_optional = true
implicit_reexport = false
local_partial_types = true
pretty = true
show_error_codes = true
show_traceback = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
4 changes: 2 additions & 2 deletions pytest_fail_slow.py → src/pytest_fail_slow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def parse_duration(s: str | int | float) -> int | float:
return float(s) * mul


def pytest_configure(config) -> None:
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line(
"markers",
"fail_slow(duration): Fail test if it takes more than this long to run",
Expand Down Expand Up @@ -120,7 +120,7 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(
item: pytest.Item, call: pytest.CallInfo
) -> Generator[None, pytest.TestResult, pytest.TestResult]:
) -> Generator[None, pytest.TestReport, pytest.TestReport]:
report = yield
if report.outcome != "passed":
return report
Expand Down
Empty file added src/pytest_fail_slow/py.typed
Empty file.
6 changes: 3 additions & 3 deletions test/test_fail_slow.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

@pytest.mark.parametrize("src,success,slow", CASES)
def test_fail_slow_no_threshold(
pytester, src: str, success: bool, slow: bool # noqa: U100
pytester: pytest.Pytester, src: str, success: bool, slow: bool # noqa: U100
) -> None:
pytester.makepyfile(test_func=src.format(decor=""))
result = pytester.runpytest()
Expand All @@ -74,7 +74,7 @@ def test_fail_slow_no_threshold(
],
)
def test_fail_slow_threshold(
pytester,
pytester: pytest.Pytester,
src: str,
success: bool,
slow: bool,
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_fail_slow_threshold(


@pytest.mark.parametrize("args", ["", "42, 'foo'"])
def test_fail_slow_marker_bad_args(pytester, args: str) -> None:
def test_fail_slow_marker_bad_args(pytester: pytest.Pytester, args: str) -> None:
pytester.makepyfile(
test_func=(
"import pytest\n"
Expand Down
16 changes: 10 additions & 6 deletions test/test_fail_slow_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
)
def test_fail_slow_setup_threshold(
pytester, args: list[str], decor: str, limitrgx: str | None
pytester: pytest.Pytester, args: list[str], decor: str, limitrgx: str | None
) -> None:
pytester.makepyfile(
test_func=(
Expand Down Expand Up @@ -53,7 +53,9 @@ def test_fail_slow_setup_threshold(


@pytest.mark.parametrize("threshold,limitrgx", [(4, "4s"), (10, None)])
def test_fail_slow_multi_setup(pytester, threshold: str, limitrgx: str | None) -> None:
def test_fail_slow_multi_setup(
pytester: pytest.Pytester, threshold: str, limitrgx: str | None
) -> None:
pytester.makepyfile(
test_func=(
"from time import sleep\n"
Expand Down Expand Up @@ -89,7 +91,9 @@ def test_fail_slow_multi_setup(pytester, threshold: str, limitrgx: str | None) -


@pytest.mark.parametrize("threshold,success", [(2, False), (5, True)])
def test_fail_slow_setup_skips_test(pytester, threshold: str, success: bool) -> None:
def test_fail_slow_setup_skips_test(
pytester: pytest.Pytester, threshold: str, success: bool
) -> None:
pytester.makepyfile(
test_func=(
"from pathlib import Path\n"
Expand All @@ -114,7 +118,7 @@ def test_fail_slow_setup_skips_test(pytester, threshold: str, success: bool) ->
assert not (pytester.path / "test.txt").exists()


def test_fail_slow_setup_teardown_still_run(pytester) -> None:
def test_fail_slow_setup_teardown_still_run(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
test_func=(
"from pathlib import Path\n"
Expand All @@ -137,7 +141,7 @@ def test_fail_slow_setup_teardown_still_run(pytester) -> None:
assert (pytester.path / "teardown.txt").read_text() == "Torn down\n"


def test_fail_slow_setup_all_run(pytester) -> None:
def test_fail_slow_setup_all_run(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
test_func=(
"from pathlib import Path\n"
Expand All @@ -163,7 +167,7 @@ def test_fail_slow_setup_all_run(pytester) -> None:


@pytest.mark.parametrize("args", ["", "42, 'foo'"])
def test_fail_slow_setup_marker_bad_args(pytester, args: str) -> None:
def test_fail_slow_setup_marker_bad_args(pytester: pytest.Pytester, args: str) -> None:
pytester.makepyfile(
test_func=(
"import pytest\n"
Expand Down
16 changes: 12 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = lint,py{38,39,310,311,312,py3}-pytest{7,8}
envlist = lint,typing,py{38,39,310,311,312,py3}-pytest{7,8}
skip_missing_interpreters = True
isolated_build = True
minversion = 3.3.0
Expand All @@ -25,7 +25,14 @@ deps =
flake8-builtins
flake8-unused-arguments
commands =
flake8 pytest_fail_slow.py test
flake8 src test

[testenv:typing]
deps =
mypy
pytest
commands =
mypy src test

[pytest]
filterwarnings = error
Expand All @@ -38,8 +45,8 @@ source = pytest_fail_slow

[coverage:paths]
source =
pytest_fail_slow.py
.tox/**/site-packages/pytest_fail_slow.py
src
.tox/**/site-packages

[coverage:report]
precision = 2
Expand All @@ -66,3 +73,4 @@ lines_between_sections = 0
profile = black
reverse_relative = True
sort_relative_in_force_sorted_sections = True
src_paths = src

0 comments on commit 4452f68

Please sign in to comment.