Skip to content

Commit a3d86bd

Browse files
committed
Do not crash with SyntaxError when parsing namedtuples with invalid label
Close pylint-dev/pylint#3549
1 parent 370459d commit a3d86bd

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Release Date: TBA
1919

2020
Close #779
2121

22+
* Do not crash with SyntaxError when parsing namedtuples with invalid label
23+
24+
Close PyCQA/pylint#3549
25+
2226

2327
What's New in astroid 2.4.0?
2428
============================

astroid/brain/brain_namedtuple_enum.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def infer_func_form(node, base_type, context=None, enum=False):
123123
except (AttributeError, exceptions.InferenceError):
124124
raise UseInferenceDefault()
125125

126+
attributes = [attr for attr in attributes if " " not in attr]
127+
126128
# If we can't infer the name of the class, don't crash, up to this point
127129
# we know it is a namedtuple anyway.
128130
name = name or "Uninferable"

tests/unittest_brain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,18 @@ def test_namedtuple_bases_are_actually_names_not_nodes(self):
331331
self.assertIsInstance(inferred.bases[0], astroid.Name)
332332
self.assertEqual(inferred.bases[0].name, "tuple")
333333

334+
def test_invalid_label_does_not_crash_inference(self):
335+
code = """
336+
import collections
337+
a = collections.namedtuple( 'a', ['b c'] )
338+
a
339+
"""
340+
node = builder.extract_node(code)
341+
inferred = next(node.infer())
342+
assert isinstance(inferred, astroid.ClassDef)
343+
assert "b" not in inferred.locals
344+
assert "c" not in inferred.locals
345+
334346

335347
class DefaultDictTest(unittest.TestCase):
336348
def test_1(self):

0 commit comments

Comments
 (0)