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
I almost fixed it inside #3070, but then I realized that the required change to typeshed (from Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]] to Union[object, Tuple[Union[object, Tuple[Any, ...]], ...]]) effectively eliminates type checking of the 2nd argument to isinstance. That would be quite disappointing.
What else can we do?
Map Type to be something other than object during type inference? Perhaps a special singleton class TypeObject?
Yeah, that would be equivalent to making the type object. But there's special-casing for isinstance() calls in mypy -- maybe we can expand that to manually check that the recursive tuple is valid? Or maybe we can implement recursive type aliases, at last (#731).
I guess a narrow fix could be to redefine Type in typing.pyi as class Type: ... instead of Type = object(). This would pass the type check for isinstance(x, Type).
We would still have to add some code elsewhere to make @JukkaL example pass, since at present Type[int] is not recognized as a subtype of Type, so mypy thinks that everything after isinstance(x, Type) is unreachable.
I think mypy. It seems Python runtime returns True on isinstance(c, Type) whenever c is of type type. This is precisely what mypy should do as well (of course, assuming it can statically infer that c of type type).
Argument 2 to "isinstance" has incompatible type "_SpecialForm";
expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"
Reacted by Dzhoshkun Ismail Shakir, Shichao Wang, David D. Riddle, remort, Patrick Gebert, nuiva, Sheila Porter, Rafael García Cuéllar, David Dobrinskiy, Vladimir Vargas-Calderón and 2 more
Activity
pkch commentedon Mar 28, 2017
I almost fixed it inside #3070, but then I realized that the required change to
typeshed(fromUnion[type, Tuple[Union[type, Tuple[Any, ...]], ...]]toUnion[object, Tuple[Union[object, Tuple[Any, ...]], ...]]) effectively eliminates type checking of the 2nd argument toisinstance. That would be quite disappointing.What else can we do?
Map
Typeto be something other thanobjectduring type inference? Perhaps a special singleton classTypeObject?gvanrossum commentedon Mar 28, 2017
Yeah, that would be equivalent to making the type
object. But there's special-casing forisinstance()calls in mypy -- maybe we can expand that to manually check that the recursive tuple is valid? Or maybe we can implement recursive type aliases, at last (#731).pkch commentedon Mar 29, 2017
I guess a narrow fix could be to redefine
Typeintyping.pyiasclass Type: ...instead ofType = object(). This would pass the type check forisinstance(x, Type).We would still have to add some code elsewhere to make @JukkaL example pass, since at present
Type[int]is not recognized as a subtype ofType, somypythinks that everything afterisinstance(x, Type)is unreachable.gvanrossum commentedon Mar 29, 2017
Is this about mypy or runtime? Making the behaviors of isinstance()/issubclass() match between static checks and runtime can be tricky.
pkch commentedon Mar 29, 2017
I think mypy. It seems Python runtime returns
Trueonisinstance(c, Type)whenevercis of typetype. This is precisely what mypy should do as well (of course, assuming it can statically infer thatcof typetype).ilevkivskyi commentedon Jun 15, 2019
On current master the error is even worse:
Add support for Type[T] typehints when arbitrary_types_allowed==True. (…
ignoring mypy _SpecialForm error
Use `callable()` builtin function over `isinstance(..., Callable)`
11 remaining items
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence
Register Map and Sequence