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

Optimize implementation of TypedDict types for **kwds #14316

Merged
merged 2 commits into from
Dec 20, 2022
Merged
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
11 changes: 8 additions & 3 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ def copy_modified(
from_concatenate: Bogus[bool] = _dummy,
unpack_kwargs: Bogus[bool] = _dummy,
) -> CT:
return type(self)(
modified = CallableType(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypyc generated really bad code for type(self)(...).

arg_types=arg_types if arg_types is not _dummy else self.arg_types,
arg_kinds=arg_kinds if arg_kinds is not _dummy else self.arg_kinds,
arg_names=arg_names if arg_names is not _dummy else self.arg_names,
Expand All @@ -1782,6 +1782,9 @@ def copy_modified(
),
unpack_kwargs=unpack_kwargs if unpack_kwargs is not _dummy else self.unpack_kwargs,
)
# Optimization: Only NewTypes are supported as subtypes since
# the class is effectively final, so we can use a cast safely.
return cast(CT, modified)

def var_arg(self) -> FormalArgument | None:
"""The formal argument for *args."""
Expand Down Expand Up @@ -1976,7 +1979,7 @@ def expand_param_spec(

def with_unpacked_kwargs(self) -> NormalizedCallableType:
if not self.unpack_kwargs:
return NormalizedCallableType(self.copy_modified())
return cast(NormalizedCallableType, self)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast is because of mypyc/mypyc#958.

last_type = get_proper_type(self.arg_types[-1])
assert isinstance(last_type, TypedDictType)
extra_kinds = [
Expand Down Expand Up @@ -2126,7 +2129,9 @@ def get_name(self) -> str | None:
return self._items[0].name

def with_unpacked_kwargs(self) -> Overloaded:
return Overloaded([i.with_unpacked_kwargs() for i in self.items])
if any(i.unpack_kwargs for i in self.items):
return Overloaded([i.with_unpacked_kwargs() for i in self.items])
return self

def accept(self, visitor: TypeVisitor[T]) -> T:
return visitor.visit_overloaded(self)
Expand Down