Skip to content

Commit

Permalink
Fix crash involving non-standard type comments (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Aug 25, 2022
1 parent ddfba9c commit 998ff34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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?
Expand Down
5 changes: 5 additions & 0 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/unittest_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 998ff34

Please sign in to comment.