MCPcopy
hub / github.com/pytest-dev/pytest / get_scope_node

Function get_scope_node

src/_pytest/fixtures.py:163–185  ·  view source on GitHub ↗

Get the closest parent node (including self) which matches the given scope. If there is no parent node for the scope (e.g. asking for class scope on a Module, or on a Function when not defined in a class), returns None.

(node: nodes.Node, scope: Scope)

Source from the content-addressed store, hash-verified

161
162
163def get_scope_node(node: nodes.Node, scope: Scope) -> nodes.Node | None:
164 """Get the closest parent node (including self) which matches the given
165 scope.
166
167 If there is no parent node for the scope (e.g. asking for class scope on a
168 Module, or on a Function when not defined in a class), returns None.
169 """
170 import _pytest.python
171
172 if scope is Scope.Function:
173 # Type ignored because this is actually safe, see:
174 # https://github.com/python/mypy/issues/4717
175 return node.getparent(nodes.Item) # type: ignore[type-abstract]
176 elif scope is Scope.Class:
177 return node.getparent(_pytest.python.Class)
178 elif scope is Scope.Module:
179 return node.getparent(_pytest.python.Module)
180 elif scope is Scope.Package:
181 return node.getparent(_pytest.python.Package)
182 elif scope is Scope.Session:
183 return node.getparent(_pytest.main.Session)
184 else:
185 assert_never(scope)
186
187
188# TODO: Try to use FixtureFunctionDefinition instead of the marker

Callers 2

parametrizeMethod · 0.90
__init__Method · 0.85

Calls 2

assert_neverFunction · 0.90
getparentMethod · 0.80

Tested by

no test coverage detected