Fix crash on nested generic callable #14093
Merged
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.