Skip to content

Commit

Permalink
[mypyc] Fix TypeError in lambda argument to overloaded function (#12780)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL committed May 13, 2022
1 parent 8faf44a commit 5ceaf3d
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 128 deletions.
5 changes: 4 additions & 1 deletion mypy/build.py
Expand Up @@ -2217,7 +2217,10 @@ def type_checker(self) -> TypeChecker:
return self._type_checker

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

def type_check_second_pass(self) -> bool:
if self.options.semantic_analysis_only:
Expand Down

0 comments on commit 5ceaf3d

Please sign in to comment.