Skip to content

Commit 9bfc6ae

Browse files
committed
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.
1 parent df6e828 commit 9bfc6ae

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

mypy/types.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ def expand_param_spec(
19761976

19771977
def with_unpacked_kwargs(self) -> NormalizedCallableType:
19781978
if not self.unpack_kwargs:
1979-
return NormalizedCallableType(self.copy_modified())
1979+
return cast(NormalizedCallableType, self)
19801980
last_type = get_proper_type(self.arg_types[-1])
19811981
assert isinstance(last_type, TypedDictType)
19821982
extra_kinds = [
@@ -2126,7 +2126,9 @@ def get_name(self) -> str | None:
21262126
return self._items[0].name
21272127

21282128
def with_unpacked_kwargs(self) -> Overloaded:
2129-
return Overloaded([i.with_unpacked_kwargs() for i in self.items])
2129+
if any(i.unpack_kwargs for i in self.items):
2130+
return Overloaded([i.with_unpacked_kwargs() for i in self.items])
2131+
return self
21302132

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

0 commit comments

Comments
 (0)