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.
This commit is contained in:
Ran Benita 2021-03-13 10:54:40 +02:00
parent 146eda93e7
commit 5bbfb4e058
3 changed files with 48 additions and 48 deletions

View File

@ -436,26 +436,26 @@ co_equal = compile(
) )
_E = TypeVar("_E", bound=BaseException, covariant=True) E = TypeVar("E", bound=BaseException, covariant=True)
@final @final
@attr.s(repr=False) @attr.s(repr=False)
class ExceptionInfo(Generic[_E]): class ExceptionInfo(Generic[E]):
"""Wraps sys.exc_info() objects and offers help for navigating the traceback.""" """Wraps sys.exc_info() objects and offers help for navigating the traceback."""
_assert_start_repr = "AssertionError('assert " _assert_start_repr = "AssertionError('assert "
_excinfo = attr.ib(type=Optional[Tuple[Type["_E"], "_E", TracebackType]]) _excinfo = attr.ib(type=Optional[Tuple[Type["E"], "E", TracebackType]])
_striptext = attr.ib(type=str, default="") _striptext = attr.ib(type=str, default="")
_traceback = attr.ib(type=Optional[Traceback], default=None) _traceback = attr.ib(type=Optional[Traceback], default=None)
@classmethod @classmethod
def from_exc_info( def from_exc_info(
cls, cls,
exc_info: Tuple[Type[_E], _E, TracebackType], exc_info: Tuple[Type[E], E, TracebackType],
exprinfo: Optional[str] = None, exprinfo: Optional[str] = None,
) -> "ExceptionInfo[_E]": ) -> "ExceptionInfo[E]":
"""Return an ExceptionInfo for an existing exc_info tuple. """Return an ExceptionInfo for an existing exc_info tuple.
.. warning:: .. warning::
@ -500,17 +500,17 @@ class ExceptionInfo(Generic[_E]):
return ExceptionInfo.from_exc_info(exc_info, exprinfo) return ExceptionInfo.from_exc_info(exc_info, exprinfo)
@classmethod @classmethod
def for_later(cls) -> "ExceptionInfo[_E]": def for_later(cls) -> "ExceptionInfo[E]":
"""Return an unfilled ExceptionInfo.""" """Return an unfilled ExceptionInfo."""
return cls(None) return cls(None)
def fill_unfilled(self, exc_info: Tuple[Type[_E], _E, TracebackType]) -> None: def fill_unfilled(self, exc_info: Tuple[Type[E], E, TracebackType]) -> None:
"""Fill an unfilled ExceptionInfo created with ``for_later()``.""" """Fill an unfilled ExceptionInfo created with ``for_later()``."""
assert self._excinfo is None, "ExceptionInfo was already filled" assert self._excinfo is None, "ExceptionInfo was already filled"
self._excinfo = exc_info self._excinfo = exc_info
@property @property
def type(self) -> Type[_E]: def type(self) -> Type[E]:
"""The exception class.""" """The exception class."""
assert ( assert (
self._excinfo is not None self._excinfo is not None
@ -518,7 +518,7 @@ class ExceptionInfo(Generic[_E]):
return self._excinfo[0] return self._excinfo[0]
@property @property
def value(self) -> _E: def value(self) -> E:
"""The exception value.""" """The exception value."""
assert ( assert (
self._excinfo is not None self._excinfo is not None

View File

@ -79,18 +79,18 @@ if TYPE_CHECKING:
# The value of the fixture -- return/yield of the fixture function (type variable). # The value of the fixture -- return/yield of the fixture function (type variable).
_FixtureValue = TypeVar("_FixtureValue") FixtureValue = TypeVar("FixtureValue")
# The type of the fixture function (type variable). # The type of the fixture function (type variable).
_FixtureFunction = TypeVar("_FixtureFunction", bound=Callable[..., object]) FixtureFunction = TypeVar("FixtureFunction", bound=Callable[..., object])
# The type of a fixture function (type alias generic in fixture value). # The type of a fixture function (type alias generic in fixture value).
_FixtureFunc = Union[ _FixtureFunc = Union[
Callable[..., _FixtureValue], Callable[..., Generator[_FixtureValue, None, None]] Callable[..., FixtureValue], Callable[..., Generator[FixtureValue, None, None]]
] ]
# The type of FixtureDef.cached_result (type alias generic in fixture value). # The type of FixtureDef.cached_result (type alias generic in fixture value).
_FixtureCachedResult = Union[ _FixtureCachedResult = Union[
Tuple[ Tuple[
# The result. # The result.
_FixtureValue, FixtureValue,
# Cache key. # Cache key.
object, object,
None, None,
@ -106,8 +106,8 @@ _FixtureCachedResult = Union[
@attr.s(frozen=True) @attr.s(frozen=True)
class PseudoFixtureDef(Generic[_FixtureValue]): class PseudoFixtureDef(Generic[FixtureValue]):
cached_result = attr.ib(type="_FixtureCachedResult[_FixtureValue]") cached_result = attr.ib(type="_FixtureCachedResult[FixtureValue]")
scope = attr.ib(type="_Scope") scope = attr.ib(type="_Scope")
@ -928,11 +928,11 @@ def fail_fixturefunc(fixturefunc, msg: str) -> "NoReturn":
def call_fixture_func( def call_fixture_func(
fixturefunc: "_FixtureFunc[_FixtureValue]", request: FixtureRequest, kwargs fixturefunc: "_FixtureFunc[FixtureValue]", request: FixtureRequest, kwargs
) -> _FixtureValue: ) -> FixtureValue:
if is_generator(fixturefunc): if is_generator(fixturefunc):
fixturefunc = cast( fixturefunc = cast(
Callable[..., Generator[_FixtureValue, None, None]], fixturefunc Callable[..., Generator[FixtureValue, None, None]], fixturefunc
) )
generator = fixturefunc(**kwargs) generator = fixturefunc(**kwargs)
try: try:
@ -942,7 +942,7 @@ def call_fixture_func(
finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, generator) finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, generator)
request.addfinalizer(finalizer) request.addfinalizer(finalizer)
else: else:
fixturefunc = cast(Callable[..., _FixtureValue], fixturefunc) fixturefunc = cast(Callable[..., FixtureValue], fixturefunc)
fixture_result = fixturefunc(**kwargs) fixture_result = fixturefunc(**kwargs)
return fixture_result return fixture_result
@ -985,7 +985,7 @@ def _eval_scope_callable(
@final @final
class FixtureDef(Generic[_FixtureValue]): class FixtureDef(Generic[FixtureValue]):
"""A container for a factory definition.""" """A container for a factory definition."""
def __init__( def __init__(
@ -993,7 +993,7 @@ class FixtureDef(Generic[_FixtureValue]):
fixturemanager: "FixtureManager", fixturemanager: "FixtureManager",
baseid: Optional[str], baseid: Optional[str],
argname: str, argname: str,
func: "_FixtureFunc[_FixtureValue]", func: "_FixtureFunc[FixtureValue]",
scope: "Union[_Scope, Callable[[str, Config], _Scope]]", scope: "Union[_Scope, Callable[[str, Config], _Scope]]",
params: Optional[Sequence[object]], params: Optional[Sequence[object]],
unittest: bool = False, unittest: bool = False,
@ -1026,7 +1026,7 @@ class FixtureDef(Generic[_FixtureValue]):
) )
self.unittest = unittest self.unittest = unittest
self.ids = ids self.ids = ids
self.cached_result: Optional[_FixtureCachedResult[_FixtureValue]] = None self.cached_result: Optional[_FixtureCachedResult[FixtureValue]] = None
self._finalizers: List[Callable[[], object]] = [] self._finalizers: List[Callable[[], object]] = []
def addfinalizer(self, finalizer: Callable[[], object]) -> None: def addfinalizer(self, finalizer: Callable[[], object]) -> None:
@ -1055,7 +1055,7 @@ class FixtureDef(Generic[_FixtureValue]):
self.cached_result = None self.cached_result = None
self._finalizers = [] self._finalizers = []
def execute(self, request: SubRequest) -> _FixtureValue: def execute(self, request: SubRequest) -> FixtureValue:
# Get required arguments and register our own finish() # Get required arguments and register our own finish()
# with their finalization. # with their finalization.
for argname in self.argnames: for argname in self.argnames:
@ -1096,8 +1096,8 @@ class FixtureDef(Generic[_FixtureValue]):
def resolve_fixture_function( def resolve_fixture_function(
fixturedef: FixtureDef[_FixtureValue], request: FixtureRequest fixturedef: FixtureDef[FixtureValue], request: FixtureRequest
) -> "_FixtureFunc[_FixtureValue]": ) -> "_FixtureFunc[FixtureValue]":
"""Get the actual callable that can be called to obtain the fixture """Get the actual callable that can be called to obtain the fixture
value, dealing with unittest-specific instances and bound methods.""" value, dealing with unittest-specific instances and bound methods."""
fixturefunc = fixturedef.func fixturefunc = fixturedef.func
@ -1123,8 +1123,8 @@ def resolve_fixture_function(
def pytest_fixture_setup( def pytest_fixture_setup(
fixturedef: FixtureDef[_FixtureValue], request: SubRequest fixturedef: FixtureDef[FixtureValue], request: SubRequest
) -> _FixtureValue: ) -> FixtureValue:
"""Execution of fixture setup.""" """Execution of fixture setup."""
kwargs = {} kwargs = {}
for argname in fixturedef.argnames: for argname in fixturedef.argnames:
@ -1174,9 +1174,9 @@ def _params_converter(
def wrap_function_to_error_out_if_called_directly( def wrap_function_to_error_out_if_called_directly(
function: _FixtureFunction, function: FixtureFunction,
fixture_marker: "FixtureFunctionMarker", fixture_marker: "FixtureFunctionMarker",
) -> _FixtureFunction: ) -> FixtureFunction:
"""Wrap the given fixture function so we can raise an error about it being called directly, """Wrap the given fixture function so we can raise an error about it being called directly,
instead of used as an argument in a test function.""" instead of used as an argument in a test function."""
message = ( message = (
@ -1194,7 +1194,7 @@ def wrap_function_to_error_out_if_called_directly(
# further than this point and lose useful wrappings like @mock.patch (#3774). # further than this point and lose useful wrappings like @mock.patch (#3774).
result.__pytest_wrapped__ = _PytestWrapper(function) # type: ignore[attr-defined] result.__pytest_wrapped__ = _PytestWrapper(function) # type: ignore[attr-defined]
return cast(_FixtureFunction, result) return cast(FixtureFunction, result)
@final @final
@ -1213,7 +1213,7 @@ class FixtureFunctionMarker:
) )
name = attr.ib(type=Optional[str], default=None) name = attr.ib(type=Optional[str], default=None)
def __call__(self, function: _FixtureFunction) -> _FixtureFunction: def __call__(self, function: FixtureFunction) -> FixtureFunction:
if inspect.isclass(function): if inspect.isclass(function):
raise ValueError("class fixtures not supported (maybe in the future)") raise ValueError("class fixtures not supported (maybe in the future)")
@ -1241,7 +1241,7 @@ class FixtureFunctionMarker:
@overload @overload
def fixture( def fixture(
fixture_function: _FixtureFunction, fixture_function: FixtureFunction,
*, *,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = ..., scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = ...,
params: Optional[Iterable[object]] = ..., params: Optional[Iterable[object]] = ...,
@ -1253,7 +1253,7 @@ def fixture(
] ]
] = ..., ] = ...,
name: Optional[str] = ..., name: Optional[str] = ...,
) -> _FixtureFunction: ) -> FixtureFunction:
... ...
@ -1276,7 +1276,7 @@ def fixture(
def fixture( def fixture(
fixture_function: Optional[_FixtureFunction] = None, fixture_function: Optional[FixtureFunction] = None,
*, *,
scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function", scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
params: Optional[Iterable[object]] = None, params: Optional[Iterable[object]] = None,
@ -1288,7 +1288,7 @@ def fixture(
] ]
] = None, ] = None,
name: Optional[str] = None, name: Optional[str] = None,
) -> Union[FixtureFunctionMarker, _FixtureFunction]: ) -> Union[FixtureFunctionMarker, FixtureFunction]:
"""Decorator to mark a fixture factory function. """Decorator to mark a fixture factory function.
This decorator can be used, with or without parameters, to define a This decorator can be used, with or without parameters, to define a

View File

@ -571,31 +571,31 @@ def _as_numpy_array(obj: object) -> Optional["ndarray"]:
# builtin pytest.raises helper # builtin pytest.raises helper
_E = TypeVar("_E", bound=BaseException) E = TypeVar("E", bound=BaseException)
@overload @overload
def raises( def raises(
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]], expected_exception: Union[Type[E], Tuple[Type[E], ...]],
*, *,
match: Optional[Union[str, Pattern[str]]] = ..., match: Optional[Union[str, Pattern[str]]] = ...,
) -> "RaisesContext[_E]": ) -> "RaisesContext[E]":
... ...
@overload @overload
def raises( def raises(
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]], expected_exception: Union[Type[E], Tuple[Type[E], ...]],
func: Callable[..., Any], func: Callable[..., Any],
*args: Any, *args: Any,
**kwargs: Any, **kwargs: Any,
) -> _pytest._code.ExceptionInfo[_E]: ) -> _pytest._code.ExceptionInfo[E]:
... ...
def raises( def raises(
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]], *args: Any, **kwargs: Any expected_exception: Union[Type[E], Tuple[Type[E], ...]], *args: Any, **kwargs: Any
) -> Union["RaisesContext[_E]", _pytest._code.ExceptionInfo[_E]]: ) -> Union["RaisesContext[E]", _pytest._code.ExceptionInfo[E]]:
r"""Assert that a code block/function call raises ``expected_exception`` r"""Assert that a code block/function call raises ``expected_exception``
or raise a failure exception otherwise. or raise a failure exception otherwise.
@ -709,7 +709,7 @@ def raises(
__tracebackhide__ = True __tracebackhide__ = True
if isinstance(expected_exception, type): if isinstance(expected_exception, type):
excepted_exceptions: Tuple[Type[_E], ...] = (expected_exception,) excepted_exceptions: Tuple[Type[E], ...] = (expected_exception,)
else: else:
excepted_exceptions = expected_exception excepted_exceptions = expected_exception
for exc in excepted_exceptions: for exc in excepted_exceptions:
@ -750,19 +750,19 @@ raises.Exception = fail.Exception # type: ignore
@final @final
class RaisesContext(Generic[_E]): class RaisesContext(Generic[E]):
def __init__( def __init__(
self, self,
expected_exception: Union[Type[_E], Tuple[Type[_E], ...]], expected_exception: Union[Type[E], Tuple[Type[E], ...]],
message: str, message: str,
match_expr: Optional[Union[str, Pattern[str]]] = None, match_expr: Optional[Union[str, Pattern[str]]] = None,
) -> None: ) -> None:
self.expected_exception = expected_exception self.expected_exception = expected_exception
self.message = message self.message = message
self.match_expr = match_expr self.match_expr = match_expr
self.excinfo: Optional[_pytest._code.ExceptionInfo[_E]] = None self.excinfo: Optional[_pytest._code.ExceptionInfo[E]] = None
def __enter__(self) -> _pytest._code.ExceptionInfo[_E]: def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
self.excinfo = _pytest._code.ExceptionInfo.for_later() self.excinfo = _pytest._code.ExceptionInfo.for_later()
return self.excinfo return self.excinfo
@ -779,7 +779,7 @@ class RaisesContext(Generic[_E]):
if not issubclass(exc_type, self.expected_exception): if not issubclass(exc_type, self.expected_exception):
return False return False
# Cast to narrow the exception type now that it's verified. # Cast to narrow the exception type now that it's verified.
exc_info = cast(Tuple[Type[_E], _E, TracebackType], (exc_type, exc_val, exc_tb)) exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
self.excinfo.fill_unfilled(exc_info) self.excinfo.fill_unfilled(exc_info)
if self.match_expr is not None: if self.match_expr is not None:
self.excinfo.match(self.match_expr) self.excinfo.match(self.match_expr)