summaryrefslogtreecommitdiff
path: root/astroid/builder.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-11-26 09:17:10 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-11-26 09:17:10 +0100
commit2cb6818ad5db25fd0f32a193ab90425e7dfcbee0 (patch)
tree3d8666f65a776dbdd81c61b20b1c6404d09d0582 /astroid/builder.py
parent5cdefb0d278dfe80a662156a942472794df5ca67 (diff)
downloadastroid-git-2cb6818ad5db25fd0f32a193ab90425e7dfcbee0.tar.gz
Retry parsing a module that has invalid type comments
It is possible for a module to use comments that might be interpreted as type comments by the `ast` library. We do not want to completely crash on those invalid type comments. Close #708
Diffstat (limited to 'astroid/builder.py')
-rw-r--r--astroid/builder.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/astroid/builder.py b/astroid/builder.py
index 34bde0a1..7f30aaeb 100644
--- a/astroid/builder.py
+++ b/astroid/builder.py
@@ -165,7 +165,7 @@ class AstroidBuilder(raw_building.InspectBuilder):
def _data_build(self, data, modname, path):
"""Build tree node from data and add some informations"""
try:
- node = _parse(data + "\n")
+ node = _parse_string(data)
except (TypeError, ValueError, SyntaxError) as exc:
raise exceptions.AstroidSyntaxError(
"Parsing Python code failed:\n{error}",
@@ -436,3 +436,19 @@ def extract_node(code, module_name=""):
if len(extracted) == 1:
return extracted[0]
return extracted
+
+
+MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation"
+
+
+def _parse_string(data, type_comments=True):
+ try:
+ node = _parse(data + "\n", type_comments=type_comments)
+ except SyntaxError as exc:
+ # If the type annotations are misplaced for some reason, we do not want
+ # to fail the entire parsing of the file, so we need to retry the parsing without
+ # type comment support.
+ if exc.args[0] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments:
+ raise
+ node = _parse(data + "\n", type_comments=False)
+ return node