diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 85785c1..49f7407 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,7 @@ repos: rev: v2.32.1 hooks: - id: pyupgrade - args: ["--py37-plus"] + args: ["--py37-plus", "--keep-runtime-typing"] # TODO: Enable mypy checker when the checks succeed #- repo: https://github.com/pre-commit/mirrors-mypy # rev: v0.931 diff --git a/pytest_bdd/parsers.py b/pytest_bdd/parsers.py index fa1344d..af1970c 100644 --- a/pytest_bdd/parsers.py +++ b/pytest_bdd/parsers.py @@ -3,7 +3,7 @@ from __future__ import annotations import abc import re as base_re -from typing import Any, Dict, cast +from typing import Any, Dict, TypeVar, Union, cast import parse as base_parse from parse_type import cfparse as base_cfparse @@ -99,7 +99,10 @@ class string(StepParser): return self.name == name -def get_parser(step_name: Any) -> StepParser: +TStepParser = TypeVar("TStepParser", bound=StepParser) + + +def get_parser(step_name: Union[str, TStepParser]) -> TStepParser: """Get parser by given name.""" if isinstance(step_name, StepParser): diff --git a/pytest_bdd/steps.py b/pytest_bdd/steps.py index 44d2036..4932d03 100644 --- a/pytest_bdd/steps.py +++ b/pytest_bdd/steps.py @@ -36,7 +36,7 @@ def given_beautiful_article(article): """ from __future__ import annotations -from typing import Any, Callable, TypeVar +from typing import Any, Callable, TypeVar, Union import pytest from _pytest.fixtures import FixtureDef, FixtureRequest @@ -60,7 +60,7 @@ def get_step_fixture_name(name: str, type_: str) -> str: def given( - name: Any, + name: Union[str, StepParser], converters: dict[str, Callable] | None = None, target_fixture: str | None = None, ) -> Callable: @@ -76,7 +76,9 @@ def given( return _step_decorator(GIVEN, name, converters=converters, target_fixture=target_fixture) -def when(name: Any, converters: dict[str, Callable] | None = None, target_fixture: str | None = None) -> Callable: +def when( + name: Union[str, StepParser], converters: dict[str, Callable] | None = None, target_fixture: str | None = None +) -> Callable: """When step decorator. :param name: Step name or a parser object. @@ -89,7 +91,9 @@ def when(name: Any, converters: dict[str, Callable] | None = None, target_fixtur return _step_decorator(WHEN, name, converters=converters, target_fixture=target_fixture) -def then(name: Any, converters: dict[str, Callable] | None = None, target_fixture: str | None = None) -> Callable: +def then( + name: Union[str, StepParser], converters: dict[str, Callable] | None = None, target_fixture: str | None = None +) -> Callable: """Then step decorator. :param name: Step name or a parser object. @@ -104,7 +108,7 @@ def then(name: Any, converters: dict[str, Callable] | None = None, target_fixtur def _step_decorator( step_type: str, - step_name: Any, + step_name: Union[str, StepParser], converters: dict[str, Callable] | None = None, target_fixture: str | None = None, ) -> Callable: