fixtures: more tweaks#11416
Merged
RonnyPfannschmidt merged 6 commits intopytest-dev:mainfrom Sep 8, 2023
Merged
Conversation
Only used for containment checks so a Set is more appropriate than a list.
Some code cleanups - no functional changes.
Since we already broke plugins using this (private) interface in this version (pytest-play, pytest-wdl), might as well do a cleanup.
There used to be two callers to `_setup_fixtures()`, now there's only one, so inline it and make `DoctestItem` more similar to `Function`. (Eventually we may want to generalize `TopRequest` from taking `Function` directly to some "fixture-supporting item", removing the remaining `type: ignore` here and allowing plugins to do it in a stable manner).
Member
Author
|
I also wrote another commit, making patchcommit 95ca3bbe7f03380470092c1f3446b921cd93ddac
Author: Ran Benita <ran@unusedvar.com>
Date: Tue Sep 5 00:30:36 2023 +0300
fixtures: avoid linear searches in getfixtureclosure
Previously did an inefficient fixpoint algorithm. Replace it with a work
queue so we only check "new" argnames.
diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py
index 80b965b81..ce006151c 100644
--- a/src/_pytest/fixtures.py
+++ b/src/_pytest/fixtures.py
@@ -1544,23 +1544,23 @@ class FixtureManager:
# (discovering matching fixtures for a given name/node is expensive).
parentid = parentnode.nodeid
- fixturenames_closure = list(initialnames)
+ closure: dict[str, None] = dict.fromkeys(initialnames)
arg2fixturedefs: Dict[str, Sequence[FixtureDef[Any]]] = {}
- lastlen = -1
- while lastlen != len(fixturenames_closure):
- lastlen = len(fixturenames_closure)
- for argname in fixturenames_closure:
- if argname in ignore_args:
- continue
- if argname in arg2fixturedefs:
- continue
- fixturedefs = self.getfixturedefs(argname, parentid)
- if fixturedefs:
- arg2fixturedefs[argname] = fixturedefs
- for arg in fixturedefs[-1].argnames:
- if arg not in fixturenames_closure:
- fixturenames_closure.append(arg)
+ work = deque(initialnames)
+ while work:
+ argname = work.popleft()
+ if argname in ignore_args:
+ continue
+ if argname in arg2fixturedefs:
+ continue
+ fixturedefs = self.getfixturedefs(argname, parentid)
+ if fixturedefs:
+ arg2fixturedefs[argname] = fixturedefs
+ for arg in fixturedefs[-1].argnames:
+ if arg not in closure:
+ closure[arg] = None
+ work.append(arg)
def sort_by_scope(arg_name: str) -> Scope:
try:
@@ -1570,7 +1570,7 @@ class FixtureManager:
else:
return fixturedefs[-1]._scope
- fixturenames_closure.sort(key=sort_by_scope, reverse=True)
+ fixturenames_closure = sorted(closure, key=sort_by_scope, reverse=True)
return fixturenames_closure, arg2fixturedefs
def pytest_generate_tests(self, metafunc: "Metafunc") -> None: |
sadra-barikbin
approved these changes
Sep 8, 2023
Contributor
sadra-barikbin
left a comment
There was a problem hiding this comment.
Thanks @bluetech. Looks good to me.
Member
|
Love to see this land, it's just awesome to see all the collaboration on enhancing those details happen, thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A few more small fixture cleanups/improvements from looking at #11243.
The second commit is picked from #11243 authored by @sadra-barikbin. The idea is to make #11243 a bit smaller after it's rebased on top of this (which I'll be happy to do if @sadra-barikbin prefers).
The last commits are some doctest cleanups after checking its fixture handling. If we ever want to provide a proper interface for (non-
Function) items to support fixtures,DoctestItemcan be our model case.