From 21054e2caa30c1e5bcde6c035f0a552dad500437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 7 Sep 2022 09:34:55 +0200 Subject: [PATCH 1/4] Fix a crash on ``namedtuples`` that use ``typename`` --- ChangeLog | 2 ++ astroid/brain/brain_namedtuple_enum.py | 12 ++++++++++++ tests/unittest_brain.py | 17 +++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7140d3e76..f5f01b4b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,7 +17,9 @@ What's New in astroid 2.12.9? ============================= Release date: TBA +* Fixed a crash on ``namedtuples`` that use ``typename`` to specify their name. + Closes PyCQA/pylint#7429 What's New in astroid 2.12.8? ============================= diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index acc20c516..88ef43e34 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -538,10 +538,22 @@ def _get_namedtuple_fields(node: nodes.Call) -> str: extract a node from them later on. """ names = [] + container = None try: container = next(node.args[1].infer()) except (InferenceError, StopIteration) as exc: raise UseInferenceDefault from exc + # We pass on IndexError as well try to infer 'field_names' from the keywords + except IndexError: + pass + if not container: + for keyword_node in node.keywords: + if keyword_node.arg == "field_names": + try: + container = next(keyword_node.value.infer()) + except (InferenceError, StopIteration) as exc: + raise UseInferenceDefault from exc + break if not isinstance(container, nodes.BaseContainer): raise UseInferenceDefault for elt in container.elts: diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index bcd92c02d..07d17c4ce 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -434,6 +434,23 @@ def __str__(self): inferred = next(node.infer()) self.assertIs(util.Uninferable, inferred) + def test_name_as_typename(self) -> None: + """Reported in https://github.com/PyCQA/pylint/issues/7429 as a crash.""" + good_node, good_node_two, bad_node = builder.extract_node( + """ + import collections + collections.namedtuple(typename="MyTuple", field_names=["birth_date", "city"]) #@ + collections.namedtuple("MyTuple", field_names=["birth_date", "city"]) #@ + collections.namedtuple(["birth_date", "city"], typename="MyTuple") #@ + """ + ) + good_inferred = next(good_node.infer()) + assert isinstance(good_inferred, nodes.ClassDef) + good_node_two_inferred = next(good_node_two.infer()) + assert isinstance(good_node_two_inferred, nodes.ClassDef) + bad_node_inferred = next(bad_node.infer()) + assert bad_node_inferred == util.Uninferable + class DefaultDictTest(unittest.TestCase): def test_1(self) -> None: From 72a378b1c407d74eca8f8ac672fa9926f4f36267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 7 Sep 2022 09:36:48 +0200 Subject: [PATCH 2/4] Update astroid/brain/brain_namedtuple_enum.py --- astroid/brain/brain_namedtuple_enum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 88ef43e34..aa2ff3cec 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -543,7 +543,7 @@ def _get_namedtuple_fields(node: nodes.Call) -> str: container = next(node.args[1].infer()) except (InferenceError, StopIteration) as exc: raise UseInferenceDefault from exc - # We pass on IndexError as well try to infer 'field_names' from the keywords + # We pass on IndexError as we'll try to infer 'field_names' from the keywords except IndexError: pass if not container: From 8e462bb87a25bb855978ddf725c5a9d817204146 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 7 Sep 2022 09:42:11 +0200 Subject: [PATCH 3/4] Update ChangeLog --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index f5f01b4b4..88a6d6087 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,7 @@ Release date: TBA * Fixed a crash on ``namedtuples`` that use ``typename`` to specify their name. + Closes PyCQA/pylint#7429 What's New in astroid 2.12.8? From 97ef3b77892e0c1544018c74b2201a4641fe069c Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 7 Sep 2022 09:42:50 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 88a6d6087..f20ac1ac0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,9 +19,9 @@ Release date: TBA * Fixed a crash on ``namedtuples`` that use ``typename`` to specify their name. - Closes PyCQA/pylint#7429 + What's New in astroid 2.12.8? ============================= Release date: 2022-09-06