You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The invalid exported type for the lambda is (str) -> str. The NameExpr has type str, instead of int.
We attempt to infer a type for the lambda once for each overload item. (The type context is taken from the overload item.) The type based on the final overload item is the one that takes precedence, since it gets evaluated last, even if it's not the matching item.
This can cause mypyc to generate incorrect code (see test failures in #12766), since it assumes that the exported types are correct and checks runtime values against them.
I see a few plausible fixes:
Perform another type checking pass using the matching overload item as context. This has the drawback of being potentially quite expensive.
When type checking as part of a search for a matching overload item, store inferred types in a temporary location. Discard the inferred types from each unsuccessful search attempt, and only record the types from the successful attempt.
Option 2 sounds more appealing to me, but it sounds a bit more difficult to implement.
The text was updated successfully, but these errors were encountered:
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.
This
typexport-basic.test
test case fails, as an incorrect type is stored for the lambda and the finalNameExpr
:The invalid exported type for the lambda is
(str) -> str
. TheNameExpr
has typestr
, instead ofint
.We attempt to infer a type for the lambda once for each overload item. (The type context is taken from the overload item.) The type based on the final overload item is the one that takes precedence, since it gets evaluated last, even if it's not the matching item.
This can cause mypyc to generate incorrect code (see test failures in #12766), since it assumes that the exported types are correct and checks runtime values against them.
I see a few plausible fixes:
Option 2 sounds more appealing to me, but it sounds a bit more difficult to implement.
The text was updated successfully, but these errors were encountered: