parent
35771f643d
commit
1da54b4546
|
@ -14,7 +14,6 @@ import sys
|
||||||
import tokenize
|
import tokenize
|
||||||
import types
|
import types
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from enum import Enum
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pathlib import PurePath
|
from pathlib import PurePath
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
@ -54,11 +53,7 @@ PYTEST_TAG = f"{sys.implementation.cache_tag}-pytest-{version}"
|
||||||
PYC_EXT = ".py" + (__debug__ and "c" or "o")
|
PYC_EXT = ".py" + (__debug__ and "c" or "o")
|
||||||
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
|
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
|
||||||
|
|
||||||
|
_SCOPE_END_MARKER = object()
|
||||||
class ScopeEndMarkerType(Enum):
|
|
||||||
"""Special marker that denotes we have just left a function or class definition."""
|
|
||||||
|
|
||||||
ScopeEndMarker = 1
|
|
||||||
|
|
||||||
|
|
||||||
class AssertionRewritingHook(importlib.abc.MetaPathFinder, importlib.abc.Loader):
|
class AssertionRewritingHook(importlib.abc.MetaPathFinder, importlib.abc.Loader):
|
||||||
|
@ -733,13 +728,13 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
|
|
||||||
# Collect asserts.
|
# Collect asserts.
|
||||||
self.scope = (mod,)
|
self.scope = (mod,)
|
||||||
nodes: List[Union[ast.AST, ScopeEndMarkerType]] = [mod]
|
nodes: List[Union[ast.AST, object]] = [mod]
|
||||||
while nodes:
|
while nodes:
|
||||||
node = nodes.pop()
|
node = nodes.pop()
|
||||||
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
||||||
self.scope = tuple((*self.scope, node))
|
self.scope = tuple((*self.scope, node))
|
||||||
nodes.append(ScopeEndMarkerType.ScopeEndMarker)
|
nodes.append(_SCOPE_END_MARKER)
|
||||||
if node is ScopeEndMarkerType.ScopeEndMarker:
|
if node == _SCOPE_END_MARKER:
|
||||||
self.scope = self.scope[:-1]
|
self.scope = self.scope[:-1]
|
||||||
continue
|
continue
|
||||||
assert isinstance(node, ast.AST)
|
assert isinstance(node, ast.AST)
|
||||||
|
|
Loading…
Reference in New Issue