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)
| 161 | |
| 162 | |
| 163 | def 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 |
no test coverage detected