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
but it's not specific to Type[C]; the same happens for List and so on. This comes from TypeAlias in typing.pyi. Is there any valid runtime use for Type[C] or List[int]? Maybe the stub should give these a more specific type?)
The only valid "runtime" use for those is as type aliases -- they are
executed by the runtime and when used in PEP 3107 annotations available for
introspection using `types.get_type_hints()`. You can't instantiate them.
class C: pass
C1: Type[C] = C
a: Callable[[], C]
reveal_type(C) # def () -> test.C
a = C # ok
reveal_type(C1) # Type[test.C]
a = C1 # ok
Presumably some recent PR fixed this issue and made Type[C] a subtype of Callable[[], C]). However, this is not sound: a constructor of subclass of C may have an incompatible signature.
It's ok to violate soundness, but in this case I think it can be avoided. My comment in #241 suggests an approach that I think would restore soundness. If that happens, Type[C] would store the signature of the C.__init__, and then we can stop using Callable[[], C] to represent class object C, and switch to Type[C]. This would make things cleaner, IMO.
Note that Callable[[], C] cannot be made a subtype of Type[C] because Type[C] has __mro__ and other things that a Callable isn't guaranteed to have. So if we want to avoid the Type vs Callable split, we have to standardize on Type, not on Callable.
Activity
rwbarton commentedon Jun 9, 2016
Actually the test would be
and that currently errors. I do think it should work, though, since
rwbarton commentedon Jun 9, 2016
(Interestingly
but it's not specific to
Type[C]; the same happens forListand so on. This comes fromTypeAliasintyping.pyi. Is there any valid runtime use forType[C]orList[int]? Maybe the stub should give these a more specific type?)gvanrossum commentedon Jun 9, 2016
JukkaL commentedon Jun 9, 2016
There is a related issue about type aliases having
Anytypes: #329pkch commentedon Apr 7, 2017
The code that was shown by @rwbarton earlier in this issue now passes type check. A simplified variation:
Presumably some recent PR fixed this issue and made
Type[C]a subtype ofCallable[[], C]). However, this is not sound: a constructor of subclass ofCmay have an incompatible signature.It's ok to violate soundness, but in this case I think it can be avoided. My comment in #241 suggests an approach that I think would restore soundness. If that happens,
Type[C]would store the signature of theC.__init__, and then we can stop usingCallable[[], C]to represent class objectC, and switch toType[C]. This would make things cleaner, IMO.Note that
Callable[[], C]cannot be made a subtype ofType[C]becauseType[C]has__mro__and other things that aCallableisn't guaranteed to have. So if we want to avoid theTypevsCallablesplit, we have to standardize onType, not onCallable.ilevkivskyi commentedon May 18, 2018
I think this issue is now fixed: