Skip to content

Commit

Permalink
Merge 236316e into 7bb86ab
Browse files Browse the repository at this point in the history
  • Loading branch information
AWhetter committed May 19, 2019
2 parents 7bb86ab + 236316e commit 7e78089
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -55,6 +55,10 @@ Release Date: TBA

Close #665

* Fix being unable to access class attributes on a NamedTuple.

Close PyCQA/pylint#1628


What's New in astroid 2.2.0?
============================
Expand Down
9 changes: 9 additions & 0 deletions astroid/brain/brain_namedtuple_enum.py
Expand Up @@ -381,6 +381,15 @@ def infer_typing_namedtuple_class(class_node, context=None):
generated_class_node = next(infer_named_tuple(node, context))
for method in class_node.mymethods():
generated_class_node.locals[method.name] = [method]

for assign in class_node.body:
if not isinstance(assign, nodes.Assign):
continue

for target in assign.targets:
attr = target.name
generated_class_node.locals[attr] = class_node.locals[attr]

return iter((generated_class_node,))


Expand Down
6 changes: 6 additions & 0 deletions astroid/tests/unittest_brain.py
Expand Up @@ -1006,6 +1006,7 @@ def test_namedtuple_class_form(self):
from typing import NamedTuple
class Example(NamedTuple):
CLASS_ATTR = "class_attr"
mything: int
Example(mything=1)
Expand All @@ -1014,6 +1015,11 @@ class Example(NamedTuple):
inferred = next(result.infer())
self.assertIsInstance(inferred, astroid.Instance)

class_attr = inferred.getattr("CLASS_ATTR")[0]
self.assertIsInstance(class_attr, astroid.AssignName)
const = next(class_attr.infer())
self.assertEqual(const.value, "class_attr")

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

0 comments on commit 7e78089

Please sign in to comment.