Skip to content

Commit

Permalink
Do not crash with SyntaxError when parsing namedtuples with invalid l…
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed May 1, 2020
1 parent 370459d commit a3d86bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Release Date: TBA

Close #779

* Do not crash with SyntaxError when parsing namedtuples with invalid label

Close PyCQA/pylint#3549


What's New in astroid 2.4.0?
============================
Expand Down
2 changes: 2 additions & 0 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def infer_func_form(node, base_type, context=None, enum=False):
except (AttributeError, exceptions.InferenceError):
raise UseInferenceDefault()

attributes = [attr for attr in attributes if " " not in attr]

# If we can't infer the name of the class, don't crash, up to this point
# we know it is a namedtuple anyway.
name = name or "Uninferable"
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ def test_namedtuple_bases_are_actually_names_not_nodes(self):
self.assertIsInstance(inferred.bases[0], astroid.Name)
self.assertEqual(inferred.bases[0].name, "tuple")

def test_invalid_label_does_not_crash_inference(self):
code = """
import collections
a = collections.namedtuple( 'a', ['b c'] )
a
"""
node = builder.extract_node(code)
inferred = next(node.infer())
assert isinstance(inferred, astroid.ClassDef)
assert "b" not in inferred.locals
assert "c" not in inferred.locals


class DefaultDictTest(unittest.TestCase):
def test_1(self):
Expand Down

0 comments on commit a3d86bd

Please sign in to comment.