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

Infer precise signatures for TypedDict types #487

Merged
merged 2 commits into from
Mar 3, 2022
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
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Infer precise call signatures for `TypedDict` types (#487)
- Add mechanism to prevent crashes on objects
with unusual `__getattr__` methods (#486)
- Infer callable signatures for objects with a
Expand Down
16 changes: 16 additions & 0 deletions pyanalyze/arg_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Extension,
GenericBases,
KVPair,
TypedDictValue,
TypedValue,
GenericValue,
NewTypeValue,
Expand Down Expand Up @@ -77,6 +78,7 @@
Tuple,
Union,
)
from typing_extensions import is_typeddict
import typing_inspect
from unittest import mock

Expand Down Expand Up @@ -645,6 +647,20 @@ def _uncached_get_argspec(
if argspec is not None:
return argspec

if is_typeddict(obj) and not is_typing_name(obj, "TypedDict"):
td_type = type_from_runtime(obj)
if isinstance(td_type, TypedDictValue):
params = [
SigParameter(
key,
ParameterKind.KEYWORD_ONLY,
default=None if required else KnownValue(...),
annotation=value,
)
for key, (required, value) in td_type.items.items()
]
return Signature.make(params, td_type)

if is_newtype(obj):
assert hasattr(obj, "__supertype__")
return Signature.make(
Expand Down
37 changes: 37 additions & 0 deletions pyanalyze/test_typeddict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# static analysis: ignore
from .implementation import assert_is_value
from .value import TypedDictValue, TypedValue
from .test_name_check_visitor import TestNameCheckVisitorBase
from .test_node_visitor import assert_passes


class TestTypedDict(TestNameCheckVisitorBase):
@assert_passes()
def test_constructor(self):
from typing_extensions import TypedDict, NotRequired

class Capybara(TypedDict):
x: int
y: str

class MaybeCapybara(TypedDict):
x: int
y: NotRequired[str]

def capybara():
cap = Capybara(x=1, y="2")
assert_is_value(
cap,
TypedDictValue(
{"x": (True, TypedValue(int)), "y": (True, TypedValue(str))}
),
)
Capybara(x=1) # E: incompatible_call

maybe_cap = MaybeCapybara(x=1)
assert_is_value(
maybe_cap,
TypedDictValue(
{"x": (True, TypedValue(int)), "y": (False, TypedValue(str))}
),
)