-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fixtures: replace PseudoFixtureDef with RequestFixtureDef which is a real FixtureDef #13801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,7 @@ | |
|
||
|
||
# The value of the fixture -- return/yield of the fixture function (type variable). | ||
FixtureValue = TypeVar("FixtureValue") | ||
FixtureValue = TypeVar("FixtureValue", covariant=True) | ||
# The type of the fixture function (type variable). | ||
FixtureFunction = TypeVar("FixtureFunction", bound=Callable[..., object]) | ||
# The type of a fixture function (type alias generic in fixture value). | ||
|
@@ -106,12 +106,6 @@ | |
) | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class PseudoFixtureDef(Generic[FixtureValue]): | ||
cached_result: _FixtureCachedResult[FixtureValue] | ||
_scope: Scope | ||
|
||
|
||
def pytest_sessionstart(session: Session) -> None: | ||
session._fixturemanager = FixtureManager(session) | ||
|
||
|
@@ -420,7 +414,7 @@ def scope(self) -> _ScopeName: | |
@abc.abstractmethod | ||
def _check_scope( | ||
self, | ||
requested_fixturedef: FixtureDef[object] | PseudoFixtureDef[object], | ||
requested_fixturedef: FixtureDef[object], | ||
requested_scope: Scope, | ||
) -> None: | ||
raise NotImplementedError() | ||
|
@@ -559,12 +553,9 @@ def _iter_chain(self) -> Iterator[SubRequest]: | |
yield current | ||
current = current._parent_request | ||
|
||
def _get_active_fixturedef( | ||
self, argname: str | ||
) -> FixtureDef[object] | PseudoFixtureDef[object]: | ||
def _get_active_fixturedef(self, argname: str) -> FixtureDef[object]: | ||
if argname == "request": | ||
cached_result = (self, [0], None) | ||
return PseudoFixtureDef(cached_result, Scope.Function) | ||
return RequestFixtureDef(self) | ||
|
||
# If we already finished computing a fixture by this name in this item, | ||
# return it. | ||
|
@@ -696,7 +687,7 @@ def _scope(self) -> Scope: | |
|
||
def _check_scope( | ||
self, | ||
requested_fixturedef: FixtureDef[object] | PseudoFixtureDef[object], | ||
requested_fixturedef: FixtureDef[object], | ||
requested_scope: Scope, | ||
) -> None: | ||
# TopRequest always has function scope so always valid. | ||
|
@@ -775,11 +766,9 @@ def node(self): | |
|
||
def _check_scope( | ||
self, | ||
requested_fixturedef: FixtureDef[object] | PseudoFixtureDef[object], | ||
requested_fixturedef: FixtureDef[object], | ||
requested_scope: Scope, | ||
) -> None: | ||
if isinstance(requested_fixturedef, PseudoFixtureDef): | ||
return | ||
if self._scope > requested_scope: | ||
# Try to report something helpful. | ||
argname = requested_fixturedef.argname | ||
|
@@ -968,7 +957,6 @@ def _eval_scope_callable( | |
return result | ||
|
||
|
||
@final | ||
class FixtureDef(Generic[FixtureValue]): | ||
"""A container for a fixture definition. | ||
|
||
|
@@ -1083,8 +1071,7 @@ def execute(self, request: SubRequest) -> FixtureValue: | |
# down first. This is generally handled by SetupState, but still currently | ||
# needed when this fixture is not parametrized but depends on a parametrized | ||
# fixture. | ||
if not isinstance(fixturedef, PseudoFixtureDef): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove this special case, I made RequestFixtureDef just ignore addfinalizer calls. |
||
requested_fixtures_that_should_finalize_us.append(fixturedef) | ||
requested_fixtures_that_should_finalize_us.append(fixturedef) | ||
|
||
# Check for (and return) cached value/exception. | ||
if self.cached_result is not None: | ||
|
@@ -1136,6 +1123,28 @@ def __repr__(self) -> str: | |
return f"<FixtureDef argname={self.argname!r} scope={self.scope!r} baseid={self.baseid!r}>" | ||
|
||
|
||
class RequestFixtureDef(FixtureDef[FixtureRequest]): | ||
"""A custom FixtureDef for the special "request" fixture. | ||
|
||
A new one is generated on-demand whenever "request" is requested. | ||
""" | ||
|
||
def __init__(self, request: FixtureRequest) -> None: | ||
super().__init__( | ||
config=request.config, | ||
baseid=None, | ||
argname="request", | ||
func=lambda: request, | ||
scope=Scope.Function, | ||
params=None, | ||
_ispytest=True, | ||
) | ||
self.cached_result = (request, [0], None) | ||
|
||
def addfinalizer(self, finalizer: Callable[[], object]) -> None: | ||
pass | ||
|
||
|
||
def resolve_fixture_function( | ||
fixturedef: FixtureDef[FixtureValue], request: FixtureRequest | ||
) -> _FixtureFunc[FixtureValue]: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed, request always has
Function
scope so will never fail this check.