MCPcopy Create free account
hub / github.com/pytest-dev/pytest / getfixtureclosure

Method getfixtureclosure

src/_pytest/fixtures.py:1499–1550  ·  view source on GitHub ↗
(
        self,
        fixturenames: Tuple[str, ...],
        parentnode: nodes.Node,
        ignore_args: Sequence[str] = (),
    )

Source from the content-addressed store, hash-verified

1497 yield from basenames
1498
1499 def getfixtureclosure(
1500 self,
1501 fixturenames: Tuple[str, ...],
1502 parentnode: nodes.Node,
1503 ignore_args: Sequence[str] = (),
1504 ) -> Tuple[Tuple[str, ...], List[str], Dict[str, Sequence[FixtureDef[Any]]]]:
1505 # Collect the closure of all fixtures, starting with the given
1506 # fixturenames as the initial set. As we have to visit all
1507 # factory definitions anyway, we also return an arg2fixturedefs
1508 # mapping so that the caller can reuse it and does not have
1509 # to re-discover fixturedefs again for each fixturename
1510 # (discovering matching fixtures for a given name/node is expensive).
1511
1512 parentid = parentnode.nodeid
1513 fixturenames_closure = list(self._getautousenames(parentid))
1514
1515 def merge(otherlist: Iterable[str]) -> None:
1516 for arg in otherlist:
1517 if arg not in fixturenames_closure:
1518 fixturenames_closure.append(arg)
1519
1520 merge(fixturenames)
1521
1522 # At this point, fixturenames_closure contains what we call "initialnames",
1523 # which is a set of fixturenames the function immediately requests. We
1524 # need to return it as well, so save this.
1525 initialnames = tuple(fixturenames_closure)
1526
1527 arg2fixturedefs: Dict[str, Sequence[FixtureDef[Any]]] = {}
1528 lastlen = -1
1529 while lastlen != len(fixturenames_closure):
1530 lastlen = len(fixturenames_closure)
1531 for argname in fixturenames_closure:
1532 if argname in ignore_args:
1533 continue
1534 if argname in arg2fixturedefs:
1535 continue
1536 fixturedefs = self.getfixturedefs(argname, parentid)
1537 if fixturedefs:
1538 arg2fixturedefs[argname] = fixturedefs
1539 merge(fixturedefs[-1].argnames)
1540
1541 def sort_by_scope(arg_name: str) -> Scope:
1542 try:
1543 fixturedefs = arg2fixturedefs[arg_name]
1544 except KeyError:
1545 return Scope.Function
1546 else:
1547 return fixturedefs[-1]._scope
1548
1549 fixturenames_closure.sort(key=sort_by_scope, reverse=True)
1550 return initialnames, fixturenames_closure, arg2fixturedefs
1551
1552 def pytest_generate_tests(self, metafunc: "Metafunc") -> None:
1553 """Generate new tests based on parametrized fixtures used by the given metafunc"""

Callers 1

getfixtureinfoMethod · 0.80

Calls 2

_getautousenamesMethod · 0.95
getfixturedefsMethod · 0.95

Tested by

no test coverage detected