Commit e28cdc1d authored by isidentical's avatar isidentical
Browse files

pyparser: add the ability to clear type_ignore cache

It seems like compile() command preserves the parser since it uses
the same execution context on the same session, so if you parse again
and again the number of type_ignores in the parser object grows.

--HG--
branch : py3.8-isidentical-ast-feature-version
parent aecc30d586d3
Pipeline #25188 failed with stage
in 14 minutes and 13 seconds
......@@ -158,6 +158,7 @@ class PythonAstCompiler(PyCodeCompiler):
def _compile_to_ast(self, source, info):
space = self.space
self.parser.reset()
try:
parse_tree = self.parser.parse_source(source, info)
mod = astbuilder.ast_from_node(space, parse_tree, info,
......
......@@ -463,3 +463,7 @@ class Parser(object):
self.stack.node_append_child(node)
else:
self.root = node
def reset(self):
"""Reset the state-bound data"""
......@@ -110,6 +110,9 @@ class PythonParser(parser.Parser):
self.future_flags = future_flags
self.type_ignores = []
def reset(self):
self.type_ignores = []
def parse_source(self, bytessrc, compile_info):
"""Main entry point for parsing Python source.
......@@ -214,8 +217,7 @@ class PythonParser(parser.Parser):
next_token_seen = token
# Special handling for TYPE_IGNOREs
if token.token_type == pygram.tokens.TYPE_IGNORE:
if token not in self.type_ignores:
self.type_ignores.append(token)
self.type_ignores.append(token)
elif self.add_token(token):
break
last_token_seen = token
......
......@@ -558,3 +558,29 @@ from __future__ import generators""")
tree_35 = self.get_ast("await = x", feature_version=5)
assert tree_35.body[0].targets[0].id == 'await'
def test_ast_feature_version_with_type_comments(self):
import ast
import textwrap
ignores = textwrap.dedent("""\
def foo():
pass # type: ignore
def bar():
x = 1 # type: ignore
def baz():
pass # type: ignore[excuse]
pass # type: ignore=excuse
pass # type: ignore [excuse]
x = 1 # type: ignore whatever
""")
for version in range(9):
tree_1 = ast.parse(ignores, type_comments=True, feature_version=version)
tree_2 = ast.parse(ignores, type_comments=True, feature_version=(3, version))
assert len(tree_1.type_ignores) == 6
assert len(tree_2.type_ignores) == 6
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment