Open Dunes opened 1 week ago
Hm, FWIW such use case is explicitly prohibited for old-style type alias syntax (because in old syntax the intent was ambiguous). With new-style syntax this may be OK. In the meantime you can use an easy workaround:
class Gen[T]:
type Alias[S] = Some[Complex[Type[Gen[S]]]]
var: Alias[T] # OK
cc @JukkaL
@ilevkivskyi Thanks, that was really helpful.
It should be noted that pyright is able to infer the types of Y
and therefore Z
. See https://pyright-play.net/?code=MYGwhgzhAEAaDaBNAugLgFDS9ALgTwAcBTaALWgF5oQBLCHJZTbZrAEyIDNoB9HmgHY0cfABQQiITgBpoANzAhU0RAEpoAWgB80AHIB7AUQzZT0CVIB0CkJXmL0raB24BbMAGsiPWvXGTOdW0yEzMsACciHABXcIFoeAtOa0UmdEi5IkUefGJRWFEARgAmAGZVS3cvHzocUVVVIA
Bug Report
Given a class context (eg. class
class Foo[T]
), mypy fails to infer the generic parameter when used as part of a type alias that is declared in the class context.Using type aliases within a class can help avoid repetition when complex generics are used throughout the class.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=845d9b12620d3febc625406e2a093ca2
Expected Behavior
The generic parameter
Y
should be inferred from the context of the class where it was specified.It should be noted that pyright is able to deduce
Y
and thereforeZ
. See https://pyright-play.net/?code=MYGwhgzhAEAaDaBNAugLgFDS9ALgTwAcBTaALWgF5oQBLCHJZTbZrAEyIDNoB9HmgHY0cfABQQiITgBpoANzAhU0RAEpoAWgB80AHIB7AUQzZT0CVIB0CkJXmL0raB24BbMAGsiPWvXGTOdW0yEzMsACciHABXcIFoeAtOa0UmdEi5IkUefGJRWFEARgAmAGZVS3cvHzocUVVVIAActual Behavior
Your Environment
--strict
mypy.ini
(and other config files): none