Skip to content

Commit 50df0ae

Browse files
committed
refactor: Move type-only imports to type-checking blocks (TC001)
- Standardise type-checking imports to `from typing import TYPE_CHECKING` Unused imports add a performance overhead at runtime, and risk creating import cycles. If an import is only used in typing-only contexts, it can instead be imported conditionally under an if TYPE_CHECKING: block to minimize runtime overhead. https://docs.astral.sh/ruff/rules/typing-only-first-party-import/#why-is-this-bad
1 parent f4323ab commit 50df0ae

File tree

13 files changed

+51
-29
lines changed

13 files changed

+51
-29
lines changed

pyproject.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ build-backend = "poetry.core.masonry.api"
6161
line-length = 120
6262
target-version = "py39"
6363
lint.select = [
64-
"B", # flake8-bugbear
65-
"E4", # pycodestyle - error - import
66-
"E7", # pycodestyle - error - statement
67-
"E9", # pycodestyle - error - runtime
68-
"F", # pyflakes
69-
"I", # isort
70-
"UP", # pyupgrade
64+
"B", # flake8-bugbearBugbear
65+
"E4", # pycodestyle - error - import
66+
"E7", # pycodestyle - error - statement
67+
"E9", # pycodestyle - error - runtime
68+
"F", # pyflakes
69+
"I", # isort
70+
"TC", # flake8-type-checking
71+
"UP", # pyupgrade
7172
]
7273
lint.isort.required-imports = [
7374
"from __future__ import annotations",

src/pytest_bdd/compat.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3-
from collections.abc import Sequence
43
from importlib.metadata import version
4+
from typing import TYPE_CHECKING
55

66
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest
7-
from _pytest.nodes import Node
87
from packaging.version import parse as parse_version
98

9+
if TYPE_CHECKING:
10+
from collections.abc import Sequence
11+
12+
from _pytest.nodes import Node
13+
1014
pytest_version = parse_version(version("pytest"))
1115

1216
__all__ = ["getfixturedefs", "inject_fixture"]

src/pytest_bdd/feature.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828

2929
import glob
3030
import os.path
31-
from collections.abc import Iterable
31+
from typing import TYPE_CHECKING
3232

3333
from .parser import Feature, FeatureParser
3434

35+
if TYPE_CHECKING:
36+
from collections.abc import Iterable
37+
3538
# Global features dictionary
3639
features: dict[str, Feature] = {}
3740

src/pytest_bdd/generation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from .compat import getfixturedefs
1414
from .feature import get_features
15-
from .parser import Feature, ScenarioTemplate, Step
1615
from .scenario import (
1716
inject_fixturedefs_for_step,
1817
make_python_docstring,
@@ -32,6 +31,8 @@
3231
from _pytest.main import Session
3332
from _pytest.nodes import Node
3433

34+
from .parser import Feature, ScenarioTemplate, Step
35+
3536

3637
template_lookup = TemplateLookup(directories=[os.path.join(os.path.dirname(__file__), "templates")])
3738

src/pytest_bdd/gherkin_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import linecache
44
import re
55
import textwrap
6-
import typing
7-
from collections.abc import Mapping, Sequence
86
from dataclasses import dataclass, field
9-
from typing import Any
7+
from typing import TYPE_CHECKING, Any
108

119
from gherkin.errors import CompositeParserException # type: ignore
1210
from gherkin.parser import Parser # type: ignore
1311

1412
from . import exceptions
1513

16-
if typing.TYPE_CHECKING:
14+
if TYPE_CHECKING:
15+
from collections.abc import Mapping, Sequence
16+
1717
from typing_extensions import Self
1818

1919

src/pytest_bdd/gherkin_terminal_reporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3-
import typing
3+
from typing import TYPE_CHECKING
44

55
from _pytest.terminal import TerminalReporter
66

77
from .reporting import test_report_context_registry
88

9-
if typing.TYPE_CHECKING:
9+
if TYPE_CHECKING:
1010
from _pytest.config import Config
1111
from _pytest.config.argparsing import Parser
1212
from _pytest.reports import TestReport

src/pytest_bdd/hooks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from __future__ import annotations
22

3-
from collections.abc import Callable
3+
from typing import TYPE_CHECKING
44

55
import pytest
6-
from _pytest.fixtures import FixtureRequest
76

8-
from pytest_bdd.parser import Feature, Scenario, Step
7+
if TYPE_CHECKING:
8+
from collections.abc import Callable
9+
10+
from _pytest.fixtures import FixtureRequest
11+
12+
from pytest_bdd.parser import Feature, Scenario, Step
913

1014
"""Pytest-bdd pytest hooks."""
1115

src/pytest_bdd/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import re
66
import textwrap
77
from collections import OrderedDict
8-
from collections.abc import Generator, Iterable, Mapping, Sequence
98
from dataclasses import dataclass, field
9+
from typing import TYPE_CHECKING
1010

1111
from .exceptions import StepError
1212
from .gherkin_parser import Background as GherkinBackground
@@ -18,6 +18,9 @@
1818
from .gherkin_parser import Tag as GherkinTag
1919
from .types import STEP_TYPE_BY_PARSER_KEYWORD
2020

21+
if TYPE_CHECKING:
22+
from collections.abc import Generator, Iterable, Mapping, Sequence
23+
2124
PARAM_RE = re.compile(r"<(.+?)>")
2225

2326

src/pytest_bdd/plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import Generator
65
from typing import TYPE_CHECKING, Callable, TypeVar, cast
76

87
import pytest
@@ -12,6 +11,8 @@
1211
from .utils import CONFIG_STACK
1312

1413
if TYPE_CHECKING:
14+
from collections.abc import Generator
15+
1516
from _pytest.config import Config, PytestPluginManager
1617
from _pytest.config.argparsing import Parser
1718
from _pytest.fixtures import FixtureRequest

src/pytest_bdd/scenario.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import logging
1818
import os
1919
import re
20-
from collections.abc import Iterable, Iterator
2120
from inspect import signature
2221
from typing import TYPE_CHECKING, Callable, TypeVar, cast
2322
from weakref import WeakKeyDictionary
@@ -39,6 +38,8 @@
3938
)
4039

4140
if TYPE_CHECKING:
41+
from collections.abc import Iterable, Iterator
42+
4243
from _pytest.mark.structures import ParameterSet
4344
from _pytest.nodes import Node
4445

0 commit comments

Comments
 (0)