Skip to content

Commit 5ceaf3d

Browse files
authored
[mypyc] Fix TypeError in lambda argument to overloaded function (#12780)
Previously we could export an invalid type for a lambda passed to an overloaded function. We find the matching overload variant by type checking the arguments against all overload items. We exported types for lambdas always from the final potential overload item, even if that wasn't the matching one. This could result in mypyc adding invalid type coercions that could result in bogus TypeErrors. The fix is to store types inferred when looking for matching overload items into temporary type maps, and only the type map from the matching item gets merged into the exported types. This doesn't fully solve the problem -- if there are Any types in the arguments, we can still export invalid types. This should be enough to unblock syncing typeshed (#12766). Typeshed started triggering the issue in compiled mypy when re.sub was changed to an overloaded function. Work on #12773.
1 parent 8faf44a commit 5ceaf3d

File tree

6 files changed

+238
-128
lines changed

6 files changed

+238
-128
lines changed

mypy/build.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,10 @@ def type_checker(self) -> TypeChecker:
22172217
return self._type_checker
22182218

22192219
def type_map(self) -> Dict[Expression, Type]:
2220-
return self.type_checker().type_map
2220+
# We can extract the master type map directly since at this
2221+
# point no temporary type maps can be active.
2222+
assert len(self.type_checker()._type_maps) == 1
2223+
return self.type_checker()._type_maps[0]
22212224

22222225
def type_check_second_pass(self) -> bool:
22232226
if self.options.semantic_analysis_only:

0 commit comments

Comments
 (0)