Skip to content

Commit 8a0d996

Browse files
committed
Add typing to code - closes #93
* Add tox environment to check typing * Add py.typed file to package tio indicate it's typed to type checking tools
1 parent 02ae485 commit 8a0d996

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __version.py
55
.cache
66
.eggs
77
.tox
8+
.idea/*
89

910
build
1011
dist

pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,28 @@ exclude = [
7878

7979
[tool.hatch.build.hooks.vcs]
8080
version-file = "src/pytest_metadata/__version.py"
81+
82+
[tool.mypy]
83+
allow_redefinition = false
84+
allow_untyped_globals = false
85+
check_untyped_defs = true
86+
disallow_incomplete_defs = true
87+
disallow_subclassing_any = true
88+
disallow_untyped_calls = true
89+
disallow_untyped_decorators = true
90+
disallow_untyped_defs = true
91+
follow_imports = "silent"
92+
ignore_missing_imports = false
93+
implicit_reexport = false
94+
no_implicit_optional = true
95+
pretty = true
96+
show_error_codes = true
97+
strict_equality = true
98+
warn_no_return = false
99+
warn_return_any = true
100+
warn_unreachable = true
101+
warn_unused_ignores = true
102+
103+
[[tool.mypy.overrides]]
104+
module = ["_pytest._pluggy.*"]
105+
ignore_missing_imports = true

src/pytest_metadata/hooks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
from typing import Dict
45

6+
from pytest import Config
57

6-
def pytest_metadata(metadata, config):
8+
9+
def pytest_metadata(metadata: Dict, config: Config) -> None:
710
"""Called after collecting metadata"""

src/pytest_metadata/plugin.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import json
55
import os
66
import platform
7+
from typing import Dict, Callable
8+
9+
from pytest import PytestPluginManager, Config, Parser
710

811
try:
912
import _pytest._pluggy as pluggy
@@ -36,27 +39,31 @@
3639
metadata_key = pytest.StashKey[dict]()
3740

3841

39-
def pytest_addhooks(pluginmanager):
42+
def pytest_addhooks(pluginmanager: PytestPluginManager) -> None:
4043
from pytest_metadata import hooks
4144

4245
pluginmanager.add_hookspecs(hooks)
4346

4447

4548
@pytest.fixture(scope="session")
46-
def metadata(pytestconfig):
49+
def metadata(pytestconfig: Config) -> Dict:
4750
"""Provide test session metadata"""
4851
return pytestconfig.stash[metadata_key]
4952

5053

5154
@pytest.fixture(scope="session")
52-
def include_metadata_in_junit_xml(metadata, pytestconfig, record_testsuite_property):
55+
def include_metadata_in_junit_xml(
56+
metadata: Dict,
57+
pytestconfig: Config,
58+
record_testsuite_property: Callable[[str, object], None],
59+
) -> None:
5360
"""Provide test session metadata"""
5461
metadata_ = pytestconfig.stash[metadata_key]
5562
for name, value in metadata_.items():
5663
record_testsuite_property(name, value)
5764

5865

59-
def pytest_addoption(parser):
66+
def pytest_addoption(parser: Parser) -> None:
6067
group = parser.getgroup("pytest-metadata")
6168
group.addoption(
6269
"--metadata",
@@ -83,7 +90,7 @@ def pytest_addoption(parser):
8390

8491

8592
@pytest.hookimpl(tryfirst=True)
86-
def pytest_configure(config):
93+
def pytest_configure(config: Config) -> None:
8794
config.stash[metadata_key] = {
8895
"Python": platform.python_version(),
8996
"Platform": platform.platform(),
@@ -118,13 +125,15 @@ def pytest_configure(config):
118125
config.hook.pytest_metadata(metadata=config.stash[metadata_key], config=config)
119126

120127

121-
def pytest_report_header(config):
128+
def pytest_report_header(config: Config) -> str | None:
122129
if config.getoption("verbose") > 0:
123130
return "metadata: {0}".format(config.stash[metadata_key])
124131

125132

126133
@pytest.hookimpl(optionalhook=True)
127-
def pytest_testnodedown(node):
134+
def pytest_testnodedown(
135+
node: "pytest_xdist.WorkerController", # noqa: F821
136+
) -> None: # type:ignore[name-defined]
128137
# note that any metadata from remote workers will be replaced with the
129138
# environment from the final worker to quit
130139
if hasattr(node, "workeroutput"):

src/pytest_metadata/py.typed

Whitespace-only changes.

tests/test_metadata.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import pytest
5+
from pytest import Pytester, MonkeyPatch
56
from xml.etree import ElementTree as ET
67

78
pytest_plugins = ("pytester",)
89

910

10-
def test_metadata(pytester):
11+
def test_metadata(pytester: Pytester) -> None:
1112
pytester.makepyfile(
1213
"""
1314
def test_pass(metadata):
@@ -20,7 +21,7 @@ def test_pass(metadata):
2021
assert result.ret == 0
2122

2223

23-
def test_environment_variables(pytester, monkeypatch):
24+
def test_environment_variables(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
2425
monkeypatch.setenv("JENKINS_URL", "foo")
2526
monkeypatch.setenv("GIT_COMMIT", "bar")
2627
pytester.makepyfile(
@@ -34,7 +35,7 @@ def test_pass(metadata):
3435
assert result.ret == 0
3536

3637

37-
def test_additional_metadata(pytester):
38+
def test_additional_metadata(pytester: Pytester) -> None:
3839
pytester.makepyfile(
3940
"""
4041
def test_pass(metadata):
@@ -49,7 +50,7 @@ def test_pass(metadata):
4950

5051

5152
@pytest.mark.parametrize("junit_format", ["xunit1", "xunit2"])
52-
def test_junit_integration(pytester, junit_format):
53+
def test_junit_integration(pytester: Pytester, junit_format: str) -> None:
5354
pytester.makepyfile(
5455
"""
5556
import pytest
@@ -78,7 +79,7 @@ def test_pass():
7879
assert {"name": "Daffy", "value": "Duck"} in xml_metadata
7980

8081

81-
def test_additional_metadata_from_json(pytester):
82+
def test_additional_metadata_from_json(pytester: Pytester) -> None:
8283
pytester.makepyfile(
8384
"""
8485
def test_pass(metadata):
@@ -89,7 +90,7 @@ def test_pass(metadata):
8990
assert result.ret == 0
9091

9192

92-
def test_additional_metadata_from_json_file(pytester):
93+
def test_additional_metadata_from_json_file(pytester: Pytester) -> None:
9394
pytester.makepyfile(
9495
"""
9596
def test_pass(metadata):
@@ -102,7 +103,9 @@ def test_pass(metadata):
102103
assert result.ret == 0
103104

104105

105-
def test_additional_metadata_using_key_values_json_str_and_file(pytester):
106+
def test_additional_metadata_using_key_values_json_str_and_file(
107+
pytester: Pytester,
108+
) -> None:
106109
pytester.makepyfile(
107110
"""
108111
def test_pass(metadata):
@@ -125,7 +128,7 @@ def test_pass(metadata):
125128
assert result.ret == 0
126129

127130

128-
def test_metadata_hook(pytester):
131+
def test_metadata_hook(pytester: Pytester) -> None:
129132
pytester.makeconftest(
130133
"""
131134
import pytest
@@ -144,7 +147,7 @@ def test_pass(metadata):
144147
assert result.ret == 0
145148

146149

147-
def test_report_header(pytester):
150+
def test_report_header(pytester: Pytester) -> None:
148151
result = pytester.runpytest()
149152
assert not any(line.startswith("metadata:") for line in result.stdout.lines)
150153
result = pytester.runpytest("-v")

0 commit comments

Comments
 (0)