Skip to content

Commit

Permalink
Optimize implementation of TypedDict types for **kwds
Browse files Browse the repository at this point in the history
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
1 parent df6e828 commit 9bfc6ae
Showing 1 changed file with 4 additions and 2 deletions.
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)
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 comments on commit 9bfc6ae

Please sign in to comment.