summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-08-25 11:28:59 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-08-25 12:14:40 +0200
commitd463bd2de2c4565e7e630bc61aa4685acc1f5183 (patch)
tree19f548f1c892dd6bc787498a717ee2e87c426014
parent060cefa51884d176fdacf1b8ea18cee3ae0b0948 (diff)
downloadastroid-git-d463bd2de2c4565e7e630bc61aa4685acc1f5183.tar.gz
Fix crash involving non-standard type comments (#1753)
-rw-r--r--ChangeLog3
-rw-r--r--astroid/rebuilder.py5
-rw-r--r--tests/unittest_builder.py8
3 files changed, 16 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b2b33617..d25ecb25 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,9 @@ What's New in astroid 2.12.4?
=============================
Release date: TBA
+* Fixed a crash involving non-standard type comments such as ``# type: # any comment``.
+
+ Refs PyCQA/pylint#7347
What's New in astroid 2.12.3?
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index a6589713..070b9db8 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -734,6 +734,11 @@ class TreeRebuilder:
# Invalid type comment, just skip it.
return None
+ # For '# type: # any comment' ast.parse returns a Module node,
+ # without any nodes in the body.
+ if not type_comment_ast.body:
+ return None
+
type_object = self.visit(type_comment_ast.body[0], parent=parent)
if not isinstance(type_object, nodes.Expr):
return None
diff --git a/tests/unittest_builder.py b/tests/unittest_builder.py
index 61ef1bba..cc1cb28b 100644
--- a/tests/unittest_builder.py
+++ b/tests/unittest_builder.py
@@ -737,6 +737,14 @@ class BuilderTest(unittest.TestCase):
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, NotImplemented)
+ def test_type_comments_without_content(self) -> None:
+ node = builder.parse(
+ """
+ a = 1 # type: # any comment
+ """
+ )
+ assert node
+
class FileBuildTest(unittest.TestCase):
def setUp(self) -> None: