Skip to content

Commit 11cf662

Browse files
committed
Reformat
1 parent d75356f commit 11cf662

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

src/pytest_codspeed/_wrapper/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
from typing import TYPE_CHECKING
35

@@ -22,7 +24,7 @@ def _get_ffi():
2224
return ffi
2325

2426

25-
def get_lib() -> "LibType":
27+
def get_lib() -> LibType:
2628
try:
2729
ffi = _get_ffi()
2830
build_lock = FileLock(f"{_wrapper_dir}/build.lock")

src/pytest_codspeed/plugin.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1+
from __future__ import annotations
2+
13
import gc
24
import os
35
import pkgutil
46
import sys
57
from dataclasses import dataclass, field
6-
from typing import (
7-
TYPE_CHECKING,
8-
Any,
9-
Callable,
10-
Dict,
11-
List,
12-
Optional,
13-
Tuple,
14-
TypeVar,
15-
Union,
16-
)
8+
from typing import TYPE_CHECKING
179

1810
import pytest
1911
from _pytest.fixtures import FixtureManager
@@ -24,15 +16,19 @@
2416
from ._wrapper import get_lib
2517

2618
if TYPE_CHECKING:
19+
from typing import Any, Callable, TypeVar
20+
2721
from ._wrapper import LibType
2822

23+
T = TypeVar("T")
24+
2925
IS_PYTEST_BENCHMARK_INSTALLED = pkgutil.find_loader("pytest_benchmark") is not None
3026
SUPPORTS_PERF_TRAMPOLINE = sys.version_info >= (3, 12)
3127
BEFORE_PYTEST_8_1_1 = pytest.version_tuple < (8, 1, 1)
3228

3329

3430
@pytest.hookimpl(trylast=True)
35-
def pytest_addoption(parser: "pytest.Parser"):
31+
def pytest_addoption(parser: pytest.Parser):
3632
group = parser.getgroup("CodSpeed benchmarking")
3733
group.addoption(
3834
"--codspeed",
@@ -46,20 +42,20 @@ def pytest_addoption(parser: "pytest.Parser"):
4642
class CodSpeedPlugin:
4743
is_codspeed_enabled: bool
4844
should_measure: bool
49-
lib: Optional["LibType"]
50-
disabled_plugins: Tuple[str, ...]
45+
lib: LibType | None
46+
disabled_plugins: tuple[str, ...]
5147
benchmark_count: int = field(default=0, hash=False, compare=False)
5248

5349

5450
PLUGIN_NAME = "codspeed_plugin"
5551

5652

57-
def get_plugin(config: "pytest.Config") -> "CodSpeedPlugin":
53+
def get_plugin(config: pytest.Config) -> CodSpeedPlugin:
5854
return config.pluginmanager.get_plugin(PLUGIN_NAME)
5955

6056

6157
@pytest.hookimpl(tryfirst=True)
62-
def pytest_configure(config: "pytest.Config"):
58+
def pytest_configure(config: pytest.Config):
6359
config.addinivalue_line(
6460
"markers", "codspeed_benchmark: mark an entire test for codspeed benchmarking"
6561
)
@@ -73,7 +69,7 @@ def pytest_configure(config: "pytest.Config"):
7369
lib = get_lib() if should_measure else None
7470
if lib is not None:
7571
lib.dump_stats_at(f"Metadata: pytest-codspeed {__version__}".encode("ascii"))
76-
disabled_plugins: List[str] = []
72+
disabled_plugins: list[str] = []
7773
# Disable pytest-benchmark if codspeed is enabled
7874
if is_codspeed_enabled and IS_PYTEST_BENCHMARK_INSTALLED:
7975
object.__setattr__(config.option, "benchmark_disable", True)
@@ -89,7 +85,7 @@ def pytest_configure(config: "pytest.Config"):
8985
config.pluginmanager.register(plugin, PLUGIN_NAME)
9086

9187

92-
def pytest_plugin_registered(plugin, manager: "pytest.PytestPluginManager"):
88+
def pytest_plugin_registered(plugin, manager: pytest.PytestPluginManager):
9389
"""Patch the benchmark fixture to use the codspeed one if codspeed is enabled"""
9490
if IS_PYTEST_BENCHMARK_INSTALLED and isinstance(plugin, FixtureManager):
9591
fixture_manager = plugin
@@ -111,7 +107,7 @@ def pytest_plugin_registered(plugin, manager: "pytest.PytestPluginManager"):
111107

112108

113109
@pytest.hookimpl(trylast=True)
114-
def pytest_report_header(config: "pytest.Config"):
110+
def pytest_report_header(config: pytest.Config):
115111
out = [
116112
f"codspeed: {__version__} "
117113
f"(callgraph: {'enabled' if SUPPORTS_PERF_TRAMPOLINE else 'not supported'})"
@@ -132,24 +128,24 @@ def pytest_report_header(config: "pytest.Config"):
132128
return "\n".join(out)
133129

134130

135-
def has_benchmark_fixture(item: "pytest.Item") -> bool:
131+
def has_benchmark_fixture(item: pytest.Item) -> bool:
136132
item_fixtures = getattr(item, "fixturenames", [])
137133
return "benchmark" in item_fixtures or "codspeed_benchmark" in item_fixtures
138134

139135

140-
def has_benchmark_marker(item: "pytest.Item") -> bool:
136+
def has_benchmark_marker(item: pytest.Item) -> bool:
141137
return (
142138
item.get_closest_marker("codspeed_benchmark") is not None
143139
or item.get_closest_marker("benchmark") is not None
144140
)
145141

146142

147-
def should_benchmark_item(item: "pytest.Item") -> bool:
143+
def should_benchmark_item(item: pytest.Item) -> bool:
148144
return has_benchmark_fixture(item) or has_benchmark_marker(item)
149145

150146

151147
@pytest.hookimpl()
152-
def pytest_sessionstart(session: "pytest.Session"):
148+
def pytest_sessionstart(session: pytest.Session):
153149
plugin = get_plugin(session.config)
154150
if plugin.is_codspeed_enabled:
155151
plugin.benchmark_count = 0
@@ -159,7 +155,7 @@ def pytest_sessionstart(session: "pytest.Session"):
159155

160156
@pytest.hookimpl(trylast=True)
161157
def pytest_collection_modifyitems(
162-
session: "pytest.Session", config: "pytest.Config", items: "List[pytest.Item]"
158+
session: pytest.Session, config: pytest.Config, items: list[pytest.Item]
163159
):
164160
plugin = get_plugin(config)
165161
if plugin.is_codspeed_enabled:
@@ -175,9 +171,9 @@ def pytest_collection_modifyitems(
175171

176172

177173
def _run_with_instrumentation(
178-
lib: "LibType",
174+
lib: LibType,
179175
nodeId: str,
180-
config: "pytest.Config",
176+
config: pytest.Config,
181177
fn: Callable[..., Any],
182178
*args,
183179
**kwargs,
@@ -209,7 +205,7 @@ def __codspeed_root_frame__():
209205

210206

211207
@pytest.hookimpl(tryfirst=True)
212-
def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item", None]):
208+
def pytest_runtest_protocol(item: pytest.Item, nextitem: pytest.Item | None):
213209
plugin = get_plugin(item.config)
214210
if not plugin.is_codspeed_enabled or not should_benchmark_item(item):
215211
return (
@@ -258,14 +254,11 @@ def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item",
258254
return reports # Deny further protocol hooks execution
259255

260256

261-
T = TypeVar("T")
262-
263-
264257
class BenchmarkFixture:
265258
"""The fixture that can be used to benchmark a function."""
266259

267-
def __init__(self, request: "pytest.FixtureRequest"):
268-
self.extra_info: Dict = {}
260+
def __init__(self, request: pytest.FixtureRequest):
261+
self.extra_info: dict = {}
269262

270263
self._request = request
271264

@@ -283,22 +276,22 @@ def __call__(self, func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
283276

284277

285278
@pytest.fixture(scope="function")
286-
def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
279+
def codspeed_benchmark(request: pytest.FixtureRequest) -> Callable:
287280
return BenchmarkFixture(request)
288281

289282

290283
if not IS_PYTEST_BENCHMARK_INSTALLED:
291284

292285
@pytest.fixture(scope="function")
293-
def benchmark(codspeed_benchmark, request: "pytest.FixtureRequest"):
286+
def benchmark(codspeed_benchmark, request: pytest.FixtureRequest):
294287
"""
295288
Compatibility with pytest-benchmark
296289
"""
297290
return codspeed_benchmark
298291

299292

300293
@pytest.hookimpl()
301-
def pytest_sessionfinish(session: "pytest.Session", exitstatus):
294+
def pytest_sessionfinish(session: pytest.Session, exitstatus):
302295
plugin = get_plugin(session.config)
303296
if plugin.is_codspeed_enabled:
304297
reporter = session.config.pluginmanager.get_plugin("terminalreporter")

tests/test_pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def fixtured_child():
304304
perf_filepath = f"/tmp/perf-{current_pid}.map"
305305
print(perf_filepath)
306306

307-
with open(perf_filepath, "r") as perf_file:
307+
with open(perf_filepath) as perf_file:
308308
lines = perf_file.readlines()
309309
assert any(
310310
"py::_run_with_instrumentation.<locals>.__codspeed_root_frame__" in line

0 commit comments

Comments
 (0)