Skip to content

Commit 5bbfb4e

Browse files
committed
Remove _ prefix from TypeVars which appear in public API
The prefixes make the API Reference docs (for e.g. `pytest.raises`, `pytest.fixture`) uglier. Being under `_pytest` is sufficient from a privacy perspective, so let's drop them.
1 parent 146eda9 commit 5bbfb4e

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

src/_pytest/_code/code.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,26 +436,26 @@ def recursionindex(self) -> Optional[int]:
436436
)
437437

438438

439-
_E = TypeVar("_E", bound=BaseException, covariant=True)
439+
E = TypeVar("E", bound=BaseException, covariant=True)
440440

441441

442442
@final
443443
@attr.s(repr=False)
444-
class ExceptionInfo(Generic[_E]):
444+
class ExceptionInfo(Generic[E]):
445445
"""Wraps sys.exc_info() objects and offers help for navigating the traceback."""
446446

447447
_assert_start_repr = "AssertionError('assert "
448448

449-
_excinfo = attr.ib(type=Optional[Tuple[Type["_E"], "_E", TracebackType]])
449+
_excinfo = attr.ib(type=Optional[Tuple[Type["E"], "E", TracebackType]])
450450
_striptext = attr.ib(type=str, default="")
451451
_traceback = attr.ib(type=Optional[Traceback], default=None)
452452

453453
@classmethod
454454
def from_exc_info(
455455
cls,
456-
exc_info: Tuple[Type[_E], _E, TracebackType],
456+
exc_info: Tuple[Type[E], E, TracebackType],
457457
exprinfo: Optional[str] = None,
458-
) -> "ExceptionInfo[_E]":
458+
) -> "ExceptionInfo[E]":
459459
"""Return an ExceptionInfo for an existing exc_info tuple.
460460
461461
.. warning::
@@ -500,25 +500,25 @@ def from_current(
500500
return ExceptionInfo.from_exc_info(exc_info, exprinfo)
501501

502502
@classmethod
503-
def for_later(cls) -> "ExceptionInfo[_E]":
503+
def for_later(cls) -> "ExceptionInfo[E]":
504504
"""Return an unfilled ExceptionInfo."""
505505
return cls(None)
506506

507-
def fill_unfilled(self, exc_info: Tuple[Type[_E], _E, TracebackType]) -> None:
507+
def fill_unfilled(self, exc_info: Tuple[Type[E], E, TracebackType]) -> None:
508508
"""Fill an unfilled ExceptionInfo created with ``for_later()``."""
509509
assert self._excinfo is None, "ExceptionInfo was already filled"
510510
self._excinfo = exc_info
511511

512512
@property
513-
def type(self) -> Type[_E]:
513+
def type(self) -> Type[E]:
514514
"""The exception class."""
515515
assert (
516516
self._excinfo is not None
517517
), ".type can only be used after the context manager exits"
518518
return self._excinfo[0]
519519

520520
@property
521-
def value(self) -> _E:
521+
def value(self) -> E:
522522
"""The exception value."""
523523
assert (
524524
self._excinfo is not None

src/_pytest/fixtures.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@
7979

8080

8181
# The value of the fixture -- return/yield of the fixture function (type variable).
82-
_FixtureValue = TypeVar("_FixtureValue")
82+
FixtureValue = TypeVar("FixtureValue")
8383
# The type of the fixture function (type variable).
84-
_FixtureFunction = TypeVar("_FixtureFunction", bound=Callable[..., object])
84+
FixtureFunction = TypeVar("FixtureFunction", bound=Callable[..., object])
8585
# The type of a fixture function (type alias generic in fixture value).
8686
_FixtureFunc = Union[
87-
Callable[..., _FixtureValue], Callable[..., Generator[_FixtureValue, None, None]]
87+
Callable[..., FixtureValue], Callable[..., Generator[FixtureValue, None, None]]
8888
]
8989
# The type of FixtureDef.cached_result (type alias generic in fixture value).
9090
_FixtureCachedResult = Union[
9191
Tuple[
9292
# The result.
93-
_FixtureValue,
93+
FixtureValue,
9494
# Cache key.
9595
object,
9696
None,
@@ -106,8 +106,8 @@
106106

107107

108108
@attr.s(frozen=True)
109-
class PseudoFixtureDef(Generic[_FixtureValue]):
110-
cached_result = attr.ib(type="_FixtureCachedResult[_FixtureValue]")
109+
class PseudoFixtureDef(Generic[FixtureValue]):
110+
cached_result = attr.ib(type="_FixtureCachedResult[FixtureValue]")
111111
scope = attr.ib(type="_Scope")
112112

113113

@@ -928,11 +928,11 @@ def fail_fixturefunc(fixturefunc, msg: str) -> "NoReturn":
928928

929929

930930
def call_fixture_func(
931-
fixturefunc: "_FixtureFunc[_FixtureValue]", request: FixtureRequest, kwargs
932-
) -> _FixtureValue:
931+
fixturefunc: "_FixtureFunc[FixtureValue]", request: FixtureRequest, kwargs
932+
) -> FixtureValue:
933933
if is_generator(fixturefunc):
934934
fixturefunc = cast(
935-
Callable[..., Generator[_FixtureValue, None, None]], fixturefunc
935+
Callable[..., Generator[FixtureValue, None, None]], fixturefunc
936936
)
937937
generator = fixturefunc(**kwargs)
938938
try:
@@ -942,7 +942,7 @@ def call_fixture_func(
942942
finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, generator)
943943
request.addfinalizer(finalizer)
944944
else:
945-
fixturefunc = cast(Callable[..., _FixtureValue], fixturefunc)
945+
fixturefunc = cast(Callable[..., FixtureValue], fixturefunc)
946946
fixture_result = fixturefunc(**kwargs)
947947
return fixture_result
948948

@@ -985,15 +985,15 @@ def _eval_scope_callable(
985985

986986

987987
@final
988-
class FixtureDef(Generic[_FixtureValue]):
988+
class FixtureDef(Generic[FixtureValue]):
989989
"""A container for a factory definition."""
990990

991991
def __init__(
992992
self,
993993
fixturemanager: "FixtureManager",
994994
baseid: Optional[str],
995995
argname: str,
996-
func: "_FixtureFunc[_FixtureValue]",
996+
func: "_FixtureFunc[FixtureValue]",
997997
scope: "Union[_Scope, Callable[[str, Config], _Scope]]",
998998
params: Optional[Sequence[object]],
999999
unittest: bool = False,
@@ -1026,7 +1026,7 @@ def __init__(
10261026
)
10271027
self.unittest = unittest
10281028
self.ids = ids
1029-
self.cached_result: Optional[_FixtureCachedResult[_FixtureValue]] = None
1029+
self.cached_result: Optional[_FixtureCachedResult[FixtureValue]] = None
10301030
self._finalizers: List[Callable[[], object]] = []
10311031

10321032
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
@@ -1055,7 +1055,7 @@ def finish(self, request: SubRequest) -> None:
10551055
self.cached_result = None
10561056
self._finalizers = []
10571057

1058-
def execute(self, request: SubRequest) -> _FixtureValue:
1058+
def execute(self, request: SubRequest) -> FixtureValue:
10591059
# Get required arguments and register our own finish()
10601060
# with their finalization.
10611061
for argname in self.argnames:
@@ -1096,8 +1096,8 @@ def __repr__(self) -> str:
10961096

10971097

10981098
def resolve_fixture_function(
1099-
fixturedef: FixtureDef[_FixtureValue], request: FixtureRequest
1100-
) -> "_FixtureFunc[_FixtureValue]":
1099+
fixturedef: FixtureDef[FixtureValue], request: FixtureRequest
1100+
) -> "_FixtureFunc[FixtureValue]":
11011101
"""Get the actual callable that can be called to obtain the fixture
11021102
value, dealing with unittest-specific instances and bound methods."""
11031103
fixturefunc = fixturedef.func
@@ -1123,8 +1123,8 @@ def resolve_fixture_function(
11231123

11241124

11251125
def pytest_fixture_setup(
1126-
fixturedef: FixtureDef[_FixtureValue], request: SubRequest
1127-
) -> _FixtureValue:
1126+
fixturedef: FixtureDef[FixtureValue], request: SubRequest
1127+
) -> FixtureValue:
11281128
"""Execution of fixture setup."""
11291129
kwargs = {}
11301130
for argname in fixturedef.argnames:
@@ -1174,9 +1174,9 @@ def _params_converter(
11741174

11751175

11761176
def wrap_function_to_error_out_if_called_directly(
1177-
function: _FixtureFunction,
1177+
function: FixtureFunction,
11781178
fixture_marker: "FixtureFunctionMarker",
1179-
) -> _FixtureFunction:
1179+
) -> FixtureFunction:
11801180
"""Wrap the given fixture function so we can raise an error about it being called directly,
11811181
instead of used as an argument in a test function."""
11821182
message = (
@@ -1194,7 +1194,7 @@ def result(*args, **kwargs):
11941194
# further than this point and lose useful wrappings like @mock.patch (#3774).
11951195
result.__pytest_wrapped__ = _PytestWrapper(function) # type: ignore[attr-defined]
11961196

1197-
return cast(_FixtureFunction, result)
1197+
return cast(FixtureFunction, result)
11981198

11991199

12001200
@final
@@ -1213,7 +1213,7 @@ class FixtureFunctionMarker:
12131213
)
12141214
name = attr.ib(type=Optional[str], default=None)
12151215

1216-
def __call__(self, function: _FixtureFunction) -> _FixtureFunction:
1216+
def __call__(self, function: FixtureFunction) -> FixtureFunction:
12171217
if inspect.isclass(function):
12181218
raise ValueError("class fixtures not supported (maybe in the future)")
12191219

@@ -1241,7 +1241,7 @@ def __call__(self, function: _FixtureFunction) -> _FixtureFunction:
12411241

12421242
@overload
12431243
def fixture(
1244-
fixture_function: _FixtureFunction,
1244+
fixture_function: FixtureFunction,
12451245
*,
12461246
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = ...,
12471247
params: Optional[Iterable[object]] = ...,
@@ -1253,7 +1253,7 @@ def fixture(
12531253
]
12541254
] = ...,
12551255
name: Optional[str] = ...,
1256-
) -> _FixtureFunction:
1256+
) -> FixtureFunction:
12571257
...
12581258

12591259

@@ -1276,7 +1276,7 @@ def fixture(
12761276

12771277

12781278
def fixture(
1279-
fixture_function: Optional[_FixtureFunction] = None,
1279+
fixture_function: Optional[FixtureFunction] = None,
12801280
*,
12811281
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
12821282
params: Optional[Iterable[object]] = None,
@@ -1288,7 +1288,7 @@ def fixture(
12881288
]
12891289
] = None,
12901290
name: Optional[str] = None,
1291-
) -> Union[FixtureFunctionMarker, _FixtureFunction]:
1291+
) -> Union[FixtureFunctionMarker, FixtureFunction]:
12921292
"""Decorator to mark a fixture factory function.
12931293
12941294
This decorator can be used, with or without parameters, to define a

src/_pytest/python_api.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,31 +571,31 @@ def _as_numpy_array(obj: object) -> Optional["ndarray"]:
571571

572572
# builtin pytest.raises helper
573573

574-
_E = TypeVar("_E", bound=BaseException)
574+
E = TypeVar("E", bound=BaseException)
575575

576576

577577
@overload
578578
def raises(
579-
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]],
579+
expected_exception: Union[Type[E], Tuple[Type[E], ...]],
580580
*,
581581
match: Optional[Union[str, Pattern[str]]] = ...,
582-
) -> "RaisesContext[_E]":
582+
) -> "RaisesContext[E]":
583583
...
584584

585585

586586
@overload
587587
def raises(
588-
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]],
588+
expected_exception: Union[Type[E], Tuple[Type[E], ...]],
589589
func: Callable[..., Any],
590590
*args: Any,
591591
**kwargs: Any,
592-
) -> _pytest._code.ExceptionInfo[_E]:
592+
) -> _pytest._code.ExceptionInfo[E]:
593593
...
594594

595595

596596
def raises(
597-
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]], *args: Any, **kwargs: Any
598-
) -> Union["RaisesContext[_E]", _pytest._code.ExceptionInfo[_E]]:
597+
expected_exception: Union[Type[E], Tuple[Type[E], ...]], *args: Any, **kwargs: Any
598+
) -> Union["RaisesContext[E]", _pytest._code.ExceptionInfo[E]]:
599599
r"""Assert that a code block/function call raises ``expected_exception``
600600
or raise a failure exception otherwise.
601601
@@ -709,7 +709,7 @@ def raises(
709709
__tracebackhide__ = True
710710

711711
if isinstance(expected_exception, type):
712-
excepted_exceptions: Tuple[Type[_E], ...] = (expected_exception,)
712+
excepted_exceptions: Tuple[Type[E], ...] = (expected_exception,)
713713
else:
714714
excepted_exceptions = expected_exception
715715
for exc in excepted_exceptions:
@@ -750,19 +750,19 @@ def raises(
750750

751751

752752
@final
753-
class RaisesContext(Generic[_E]):
753+
class RaisesContext(Generic[E]):
754754
def __init__(
755755
self,
756-
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]],
756+
expected_exception: Union[Type[E], Tuple[Type[E], ...]],
757757
message: str,
758758
match_expr: Optional[Union[str, Pattern[str]]] = None,
759759
) -> None:
760760
self.expected_exception = expected_exception
761761
self.message = message
762762
self.match_expr = match_expr
763-
self.excinfo: Optional[_pytest._code.ExceptionInfo[_E]] = None
763+
self.excinfo: Optional[_pytest._code.ExceptionInfo[E]] = None
764764

765-
def __enter__(self) -> _pytest._code.ExceptionInfo[_E]:
765+
def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
766766
self.excinfo = _pytest._code.ExceptionInfo.for_later()
767767
return self.excinfo
768768

@@ -779,7 +779,7 @@ def __exit__(
779779
if not issubclass(exc_type, self.expected_exception):
780780
return False
781781
# Cast to narrow the exception type now that it's verified.
782-
exc_info = cast(Tuple[Type[_E], _E, TracebackType], (exc_type, exc_val, exc_tb))
782+
exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
783783
self.excinfo.fill_unfilled(exc_info)
784784
if self.match_expr is not None:
785785
self.excinfo.match(self.match_expr)

0 commit comments

Comments
 (0)