Skip to content

Commit bc43da8

Browse files
committed
fixup! perf: Prune function bodies from source before building AST
1 parent 9ffbd4c commit bc43da8

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

packages/griffelib/tests/test_parser.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def prune(code: str, python_minor: int) -> str: # noqa: ARG001
5959
calls += 1
6060
return code
6161

62-
monkeypatch.setattr(parser_module, "_native_prune_source", prune)
62+
monkeypatch.setattr(parser_module, "prune_source", prune)
6363
parser_module._compile_module("def f(): pass", filename="module.py", extensions=Extensions(_PostLoadExtension()))
6464

6565
assert calls == 1
@@ -74,7 +74,7 @@ def prune(code: str, python_minor: int) -> str: # noqa: ARG001
7474
calls += 1
7575
return code
7676

77-
monkeypatch.setattr(parser_module, "_native_prune_source", prune)
77+
monkeypatch.setattr(parser_module, "prune_source", prune)
7878
parser_module._compile_module("value = 1", filename="module.py", extensions=Extensions())
7979

8080
assert calls == 0
@@ -89,7 +89,7 @@ def prune(code: str, python_minor: int) -> str: # noqa: ARG001
8989
calls += 1
9090
return code
9191

92-
monkeypatch.setattr(parser_module, "_native_prune_source", prune)
92+
monkeypatch.setattr(parser_module, "prune_source", prune)
9393
parser_module._compile_module("def f(): ...", filename="module.pyi", extensions=Extensions())
9494

9595
assert calls == 0
@@ -104,7 +104,7 @@ def prune(code: str, python_minor: int) -> str: # noqa: ARG001
104104
calls += 1
105105
return code
106106

107-
monkeypatch.setattr(parser_module, "_native_prune_source", prune)
107+
monkeypatch.setattr(parser_module, "prune_source", prune)
108108
parser_module._compile_module("def f(): pass", filename="module.py", extensions=Extensions(_NodeExtension()))
109109

110110
assert calls == 0
@@ -119,7 +119,7 @@ def prune(code: str, python_minor: int) -> str: # noqa: ARG001
119119
calls += 1
120120
return code
121121

122-
monkeypatch.setattr(parser_module, "_native_prune_source", prune)
122+
monkeypatch.setattr(parser_module, "prune_source", prune)
123123

124124
class CustomVisitor(Visitor):
125125
pass
@@ -131,7 +131,7 @@ class CustomVisitor(Visitor):
131131

132132
def test_cpython_retries_an_invalid_native_result(monkeypatch: pytest.MonkeyPatch) -> None:
133133
"""Never expose a diagnostic caused by native source pruning."""
134-
monkeypatch.setattr(parser_module, "_native_prune_source", lambda code, python_minor: "def invalid(: pass")
134+
monkeypatch.setattr(parser_module, "prune_source", lambda code, python_minor: "def invalid(: pass")
135135

136136
node = parser_module._compile_module("def valid(): pass", filename="module.py", extensions=Extensions())
137137

@@ -140,14 +140,14 @@ def test_cpython_retries_an_invalid_native_result(monkeypatch: pytest.MonkeyPatc
140140

141141
def test_cpython_compiles_untouched_source_when_native_declines(monkeypatch: pytest.MonkeyPatch) -> None:
142142
"""Use the normal source when Ruff cannot prune it."""
143-
monkeypatch.setattr(parser_module, "_native_prune_source", lambda code, python_minor: None)
143+
monkeypatch.setattr(parser_module, "prune_source", lambda code, python_minor: None)
144144

145145
node = parser_module._compile_module("def valid(): pass", filename="module.py", extensions=Extensions())
146146

147147
assert isinstance(node.body[0], ast.FunctionDef)
148148

149149

150-
@pytest.mark.skipif(parser_module._native_prune_source is None, reason="prune-source is not installed")
150+
@pytest.mark.skipif(parser_module.prune_source is None, reason="prune-source is not installed")
151151
def test_native_and_cpython_visitors_are_equivalent(monkeypatch: pytest.MonkeyPatch) -> None:
152152
"""Produce the same public model from the native and compatibility parser paths."""
153153
source = dedent(
@@ -184,11 +184,11 @@ def doubled(self) -> int:
184184
return intermediate
185185
''',
186186
)
187-
native_prune_source = parser_module._native_prune_source
187+
native_prune_source = parser_module.prune_source
188188
native = visit("module", Path("module.py"), source, extensions=Extensions())
189189

190-
monkeypatch.setattr(parser_module, "_native_prune_source", None)
190+
monkeypatch.setattr(parser_module, "prune_source", None)
191191
cpython = visit("module", Path("module.py"), source, extensions=Extensions())
192-
monkeypatch.setattr(parser_module, "_native_prune_source", native_prune_source)
192+
monkeypatch.setattr(parser_module, "prune_source", native_prune_source)
193193

194194
assert native.as_json(full=True, sort_keys=True) == cpython.as_json(full=True, sort_keys=True)

0 commit comments

Comments
 (0)