8000 Optimize implementation of TypedDict types for **kwds by JukkaL · Pull Request #14316 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Optimize implementation of TypedDict types for **kwds #14316

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

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Changes from 1 commit
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
Next Next commit
Optimize implementation of TypedDict types for **kwds
The implementation copied lots of callable types even when not using
the new feature, which was expensive. Now we only generate a copy if a
callable actually uses TypedDict types for **kwds.

This made self check 7-8% faster (when compiled with -O0).

The original implementation was in #13471.
  • Loading branch information
JukkaL committed Dec 19, 2022
commit 9bfc6ae09e4418a539f45c9297ae61b535b78e46
6 changes: 4 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,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 +2126,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
0