Skip to content

Commit 8629438

Browse files
committed
Add missing type annotations, and require all files to be typed
1 parent 0573c6f commit 8629438

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ python_version = "3.8"
9494
warn_return_any = true
9595
warn_unused_configs = true
9696
files = "src/pytest_bdd/**/*.py"
97+
disallow_untyped_defs = true
9798

9899
[[tool.mypy.overrides]]
99100
module = ["parse", "parse_type"]

src/pytest_bdd/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class NoScenariosFound(Exception):
2626
class GherkinParseError(Exception):
2727
"""Base class for all Gherkin parsing errors."""
2828

29-
def __init__(self, message: str, line: int, line_content: str, filename: str):
29+
def __init__(self, message: str, line: int, line_content: str, filename: str) -> None:
3030
super().__init__(message)
3131
self.message = message
3232
self.line = line

src/pytest_bdd/feature.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def get_feature(base_path: str, filename: str, encoding: str = "utf-8") -> Featu
5757
return feature
5858

5959

60-
def get_features(paths: list[str], **kwargs) -> list[Feature]:
60+
def get_features(paths: list[str], encoding: str = "utf-8") -> list[Feature]:
6161
"""Get features for given paths.
6262
6363
:param list paths: `list` of paths (file or dirs)
@@ -71,10 +71,10 @@ def get_features(paths: list[str], **kwargs) -> list[Feature]:
7171
seen_names.add(path)
7272
if os.path.isdir(path):
7373
file_paths = list(glob.iglob(os.path.join(path, "**", "*.feature"), recursive=True))
74-
_features.extend(get_features(file_paths, **kwargs))
74+
_features.extend(get_features(file_paths, encoding=encoding))
7575
else:
7676
base, name = os.path.split(path)
77-
feature = get_feature(base, name, **kwargs)
77+
feature = get_feature(base, name, encoding=encoding)
7878
_features.append(feature)
7979
_features.sort(key=lambda _feature: _feature.name or _feature.filename)
8080
return _features

src/pytest_bdd/gherkin_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def get_gherkin_document(abs_filename: str, encoding: str = "utf-8") -> GherkinD
309309

310310
def handle_gherkin_parser_error(
311311
raw_error: str, line: int, line_content: str, filename: str, original_exception: Exception | None = None
312-
):
312+
) -> None:
313313
"""Map the error message to a specific exception type and raise it."""
314314
# Split the raw_error into individual lines
315315
error_lines = raw_error.splitlines()

src/pytest_bdd/hooks.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,71 @@
11
from __future__ import annotations
22

3+
from collections.abc import Callable
4+
35
import pytest
6+
from _pytest.fixtures import FixtureRequest
7+
8+
from pytest_bdd.parser import Feature, Scenario, Step
49

510
"""Pytest-bdd pytest hooks."""
611

712

8-
def pytest_bdd_before_scenario(request, feature, scenario):
13+
def pytest_bdd_before_scenario(request: FixtureRequest, feature: Feature, scenario: Scenario) -> object:
914
"""Called before scenario is executed."""
1015

1116

12-
def pytest_bdd_after_scenario(request, feature, scenario):
17+
def pytest_bdd_after_scenario(request: FixtureRequest, feature: Feature, scenario: Scenario) -> object:
1318
"""Called after scenario is executed."""
1419

1520

16-
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
21+
def pytest_bdd_before_step(
22+
request: FixtureRequest, feature: Feature, scenario: Scenario, step: Step, step_func: Callable[..., object]
23+
) -> object:
1724
"""Called before step function is set up."""
1825

1926

20-
def pytest_bdd_before_step_call(request, feature, scenario, step, step_func, step_func_args):
27+
def pytest_bdd_before_step_call(
28+
request: FixtureRequest,
29+
feature: Feature,
30+
scenario: Scenario,
31+
step: Step,
32+
step_func: Callable[..., object],
33+
step_func_args: dict[str, object],
34+
) -> object:
2135
"""Called before step function is executed."""
2236

2337

24-
def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args):
38+
def pytest_bdd_after_step(
39+
request: FixtureRequest,
40+
feature: Feature,
41+
scenario: Scenario,
42+
step: Step,
43+
step_func: Callable[..., object],
44+
step_func_args: dict[str, object],
45+
) -> object:
2546
"""Called after step function is successfully executed."""
2647

2748

28-
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
49+
def pytest_bdd_step_error(
50+
request: FixtureRequest,
51+
feature: Feature,
52+
scenario: Scenario,
53+
step: Step,
54+
step_func: Callable[..., object],
55+
step_func_args: dict[str, object],
56+
exception: Exception,
57+
) -> object:
2958
"""Called when step function failed to execute."""
3059

3160

32-
def pytest_bdd_step_func_lookup_error(request, feature, scenario, step, exception):
61+
def pytest_bdd_step_func_lookup_error(
62+
request: FixtureRequest, feature: Feature, scenario: Scenario, step: Step, exception: Exception
63+
) -> object:
3364
"""Called when step lookup failed."""
3465

3566

3667
@pytest.hookspec(firstresult=True)
37-
def pytest_bdd_apply_tag(tag, function):
68+
def pytest_bdd_apply_tag(tag: str, function: Callable[..., object]) -> object:
3869
"""Apply a tag (from a ``.feature`` file) to the given scenario.
3970
4071
The default implementation does the equivalent of

src/pytest_bdd/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def parse_steps(self, steps_data: list[GherkinStep]) -> list[Step]:
358358
List[Step]: A list of Step objects.
359359
"""
360360

361-
def get_step_content(_gherkin_step):
361+
def get_step_content(_gherkin_step: GherkinStep) -> str:
362362
step_name = strip_comments(_gherkin_step.text)
363363
if _gherkin_step.docString:
364364
step_name = f"{step_name}\n{_gherkin_step.docString.content}"

src/pytest_bdd/scenario.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import logging
1818
import os
1919
import re
20-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, List, Optional, TypeVar, cast
20+
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, List, TypeVar, cast
2121

2222
import pytest
2323
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest, call_fixture_func
@@ -151,7 +151,7 @@ def get_fixture_path(fixture_def: FixtureDef) -> list[str]:
151151
del fixturemanager._arg2fixturedefs[bdd_name]
152152

153153

154-
def get_step_function(request, step: Step) -> StepFunctionContext | None:
154+
def get_step_function(request: FixtureRequest, step: Step) -> StepFunctionContext | None:
155155
"""Get the step function (context) for the given step.
156156
157157
We first figure out what's the step fixture name that we have to inject.

0 commit comments

Comments
 (0)