Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing.TypedDict inference #915

Merged
merged 4 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Release Date: TBA

Closes #895 #899

* Improve typing.TypedDict inference


What's New in astroid 2.5?
============================
Release Date: 2021-02-15
Expand Down
30 changes: 30 additions & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>

"""Astroid hooks for typing.py support."""
import sys
import typing

from astroid import (
Expand All @@ -12,9 +13,11 @@
extract_node,
inference_tip,
nodes,
context,
InferenceError,
)

PY39 = sys.version_info[:2] >= (3, 9)

TYPING_NAMEDTUPLE_BASENAMES = {"NamedTuple", "typing.NamedTuple"}
TYPING_TYPEVARS = {"TypeVar", "NewType"}
Expand Down Expand Up @@ -85,6 +88,28 @@ def infer_typing_attr(node, context=None):
return node.infer(context=context)


def _looks_like_typedDict( # pylint: disable=invalid-name
node: nodes.FunctionDef,
) -> bool:
"""Check if node is TypedDict FunctionDef."""
return isinstance(node, nodes.FunctionDef) and node.name == "TypedDict"


def infer_typedDict( # pylint: disable=invalid-name
node: nodes.FunctionDef, ctx: context.InferenceContext = None
) -> None:
"""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]


MANAGER.register_transform(
nodes.Call,
inference_tip(infer_typing_typevar_or_newtype),
Expand All @@ -93,3 +118,8 @@ def infer_typing_attr(node, context=None):
MANAGER.register_transform(
nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript
)

if PY39:
MANAGER.register_transform(
nodes.FunctionDef, infer_typedDict, _looks_like_typedDict
)
16 changes: 16 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,22 @@ def test_typing_namedtuple_dont_crash_on_no_fields(self):
inferred = next(node.infer())
self.assertIsInstance(inferred, astroid.Instance)

@test_utils.require_version("3.8")
def test_typedDict(self):
node = builder.extract_node(
"""
from typing import TypedDict
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]


class ReBrainTest(unittest.TestCase):
def test_regex_flags(self):
Expand Down