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
{{ message }}
Repository navigation
Incorrect resolution of type[TypeVar] returning TypeVar with numpy #20354
Could you explain further why you expect different output? Any is compatible with any bound, type passed as argument is not parameterized hence defaults all tvars to any, why should it not be Any?
type passed as argument is not parameterized hence defaults all tvars to any,
As my example shows, this also occurs when its fully parametrized.
Also note that the type parameter defaults of np.ndarray are tuple[Any, ...] and np.dtype[Any]. So np.ndarray (as type expression) is therefore exactly equivalent to np.ndarray[tuple[Any, ...], np.dtype[Any]].
Nah, this is the correct use for type - TypeForm is not needed when you have an actual class. TypeForm is compatible with things like TypeVar itself, should be irrelevant here - the problem is not evaluating Arr in f(Arr) as if it was in a type context (failure to resolve the typevar to its default). The snippet is correct, rejecting it is a defect in mypy.
Activity
jorenham commentedon Dec 2, 2025
I thought this might be related to the PEP 696 type parameter defaults, but that doesn't seem to be the case:
In fact, any type arguments to
np.ndarrayseem to be swallowed this way 🤔sterliakov commentedon Dec 2, 2025
Could you explain further why you expect different output?
Anyis compatible with any bound, type passed as argument is not parameterized hence defaults all tvars to any, why should it not be Any?Zero-numpy example (playground):
jorenham commentedon Dec 2, 2025
As my example shows, this also occurs when its fully parametrized.
Also note that the type parameter defaults of
np.ndarrayaretuple[Any, ...]andnp.dtype[Any]. Sonp.ndarray(as type expression) is therefore exactly equivalent tonp.ndarray[tuple[Any, ...], np.dtype[Any]].sterliakov commentedon Dec 2, 2025
Ah, sorry, I missed that numpy already started using tvars defaults. So the problem reduces to the following, right?
Probably the default should have been applied here.
jorenham commentedon Dec 2, 2025
Yea pretty much. There's also where
fis parametrized withArr[int](so with type arg) that behaves unexpectedly:https://mypy-play.net/?mypy=latest&python=3.14&flags=strict&gist=b51972fc19e7c56363428c62dede27e7
jacopoabramo commentedon Dec 5, 2025
Just a hunch but wouldn't this be one of the cases covered by PEP 747?
sterliakov commentedon Dec 5, 2025
Nah, this is the correct use for
type-TypeFormis not needed when you have an actual class. TypeForm is compatible with things likeTypeVaritself, should be irrelevant here - the problem is not evaluatingArrinf(Arr)as if it was in a type context (failure to resolve the typevar to its default). The snippet is correct, rejecting it is a defect in mypy.