Skip to content

Commit

Permalink
Use inference_tip for typing.TypedDict brain (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Apr 7, 2021
1 parent 8e28720 commit 76172d4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Release Date: TBA

Closes PyCQA/pylint#4206

* Use ``inference_tip`` for ``typing.TypedDict`` brain.


What's New in astroid 2.5.2?
============================
Release Date: 2021-03-28
Expand Down
8 changes: 3 additions & 5 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,15 @@ def _looks_like_typedDict( # pylint: disable=invalid-name

def infer_typedDict( # pylint: disable=invalid-name
node: nodes.FunctionDef, ctx: context.InferenceContext = None
) -> None:
) -> typing.Iterator[nodes.ClassDef]:
"""Replace TypedDict FunctionDef with ClassDef."""
class_def = nodes.ClassDef(
name="TypedDict",
doc=node.doc,
lineno=node.lineno,
col_offset=node.col_offset,
parent=node.parent,
)
class_def.postinit(bases=[], body=[], decorators=None)
node.root().locals["TypedDict"] = [class_def]
return iter([class_def])


CLASS_GETITEM_TEMPLATE = """
Expand Down Expand Up @@ -238,7 +236,7 @@ def infer_typing_alias(

if PY39:
MANAGER.register_transform(
nodes.FunctionDef, infer_typedDict, _looks_like_typedDict
nodes.FunctionDef, inference_tip(infer_typedDict), _looks_like_typedDict
)

if PY37:
Expand Down
7 changes: 2 additions & 5 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,12 +1372,9 @@ class CustomTD(TypedDict):
var: int
"""
)
assert len(node.bases) == 1
inferred_base = next(node.bases[0].infer())
self.assertIsInstance(inferred_base, nodes.ClassDef, node.as_string())
typing_module = inferred_base.root()
assert len(typing_module.locals["TypedDict"]) == 1
assert inferred_base == typing_module.locals["TypedDict"][0]
assert isinstance(inferred_base, nodes.ClassDef)
assert inferred_base.qname() == "typing.TypedDict"

@test_utils.require_version(minver="3.7")
def test_typing_alias_type(self):
Expand Down

0 comments on commit 76172d4

Please sign in to comment.