Fix crash on nested generic callable#14093
Merged
ilevkivskyi merged 2 commits intopython:masterfrom Nov 16, 2022
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
Member
Author
|
Oh, it looks like it (accidentally) fixes another crash: #13515 |
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Member
Author
|
If there are no objections, I am going to merge this soon (this is a low risk fox for a crash). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10244
Fixes #13515
This fixes only the crash part, I am going to fix also the embarrassing type variable clash in a separate PR, since it is completely unrelated issue.
The crash happens because solver can call
is_suptype()on the constraint bounds, and those can contain<Erased>. Then if it is a generic callable type (e.g.def [S] (S) -> Twhen used as a context is erased todef [S] (S) -> <Erased>),is_subtype()will try unifying them, causing the crash when applying unified arguments.My fix is to simply allow subtyping between callable types that contain
<Erased>, we anyway allow checking subtpying between all other types with<Erased>components. And this technically can be useful, e.g.[T <: DerivedGen1[<Erased>], T <: DerivedGen2[<Erased>]]will be solved asT <: NonGenBase.Btw this crash technically has nothing to do with dataclasses, but it looks like there is no other way in mypy to define a callable with generic callable as argument type, if I try:
to repro the crash, mypy instead interprets
fooasdef [S, T] (x: Callable[[S], T]) -> T, i.e. the argument type is not generic. I also tried callback protocols, but they also don't repro the crash (at least I can't find a repro), because protocols use variance for subtyping, before actually checking member types.