diff --git a/ChangeLog b/ChangeLog index b2b336170b..d25ecb25fe 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 a658971352..070b9db84c 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -734,6 +734,11 @@ def check_type_comment( # 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 61ef1bba3a..cc1cb28bfd 100644 --- a/tests/unittest_builder.py +++ b/tests/unittest_builder.py @@ -737,6 +737,14 @@ def test_not_implemented(self) -> None: 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: