Skip to content

Commit

Permalink
Fix a crash involving Uninferable args to namedtuple (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Sep 4, 2022
1 parent 830a9a6 commit daa6a44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -17,7 +17,9 @@ What's New in astroid 2.12.6?
=============================
Release date: TBA

* Fix a crash involving ``Uninferable`` arguments to ``namedtuple()``.

Closes PyCQA/pylint#7375


What's New in astroid 2.12.5?
Expand Down
8 changes: 7 additions & 1 deletion astroid/brain/brain_namedtuple_enum.py
Expand Up @@ -538,7 +538,13 @@ def _get_namedtuple_fields(node: nodes.Call) -> str:
extract a node from them later on.
"""
names = []
for elt in next(node.args[1].infer()).elts:
try:
container = next(node.args[1].infer())
except (InferenceError, StopIteration) as exc:
raise UseInferenceDefault from exc
if not isinstance(container, nodes.BaseContainer):
raise UseInferenceDefault
for elt in container.elts:
if isinstance(elt, nodes.Const):
names.append(elt.as_string())
continue
Expand Down
20 changes: 20 additions & 0 deletions tests/unittest_brain.py
Expand Up @@ -18,6 +18,7 @@
import astroid
from astroid import MANAGER, bases, builder, nodes, objects, test_utils, util
from astroid.bases import Instance
from astroid.brain.brain_namedtuple_enum import _get_namedtuple_fields
from astroid.const import PY39_PLUS
from astroid.exceptions import (
AttributeInferenceError,
Expand Down Expand Up @@ -1644,6 +1645,25 @@ def NamedTuple():
)
next(node.infer())

def test_namedtuple_uninferable_member(self) -> None:
call = builder.extract_node(
"""
from typing import namedtuple
namedtuple('uninf', {x: x for x in range(0)}) #@"""
)
with pytest.raises(UseInferenceDefault):
_get_namedtuple_fields(call)

call = builder.extract_node(
"""
from typing import namedtuple
uninferable = {x: x for x in range(0)}
namedtuple('uninferable', uninferable) #@
"""
)
with pytest.raises(UseInferenceDefault):
_get_namedtuple_fields(call)

def test_typing_types(self) -> None:
ast_nodes = builder.extract_node(
"""
Expand Down

0 comments on commit daa6a44

Please sign in to comment.