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
error: Variable "typing.Protocol" is not valid as a type [valid-type]
Your Environment
Mypy version used: 0.991
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files): strict = True
Python version used: 3.11
Context
I realize that declaring x: Protocol is nonsensical because the base Protocol does not define any interface, so I might as well use Any. But I was expecting that I could express type[Protocol] to indicate that x should be a subclass (so nominal here) of Protocol, i.e. not a class that is a structural subtype of some protocol, but just one that inherits from Protocol.
I noticed that this may related to the request for higher-kinded types and the error when trying to express type[Generic] (#7791) is the same.
Is there a reason, why annotating with type[Protocol] (or type[Generic]) is not allowed? Also, the error message seems strange to me since Protocol (and Generic) are clearly not variables but regular old classes, even though they are also special typing constructs.
Not a big issue IMO, just thought I'd mention it.
The text was updated successfully, but these errors were encountered:
Thanks! I don't think there is really behaviour here that makes sense (e.g. easy to come up with examples here that would break transitivity or other properties that are important for reasoning about types). In general, static type checkers don't try to mirror the implementation details of the runtime (as evidenced by the fact that mypy doesn't know the details of what Protocol is). The runtime implementations of typing features are liable to change and highly complex, modelling them isn't useful for most users (and can result in usability regressions). The users who want to do this kind of thing are typically already doing a lot of very magical runtime type checking and can just add more runtime type checks.
Bug Report
Attempting to annotate something as any
Protocol
causes an error.To Reproduce
Expected Behavior
No problem.
Actual Behavior
error: Variable "typing.Protocol" is not valid as a type [valid-type]
Your Environment
0.991
mypy.ini
(and other config files):strict = True
3.11
Context
I realize that declaring
x: Protocol
is nonsensical because the baseProtocol
does not define any interface, so I might as well useAny
. But I was expecting that I could expresstype[Protocol]
to indicate thatx
should be a subclass (so nominal here) ofProtocol
, i.e. not a class that is a structural subtype of some protocol, but just one that inherits fromProtocol
.I noticed that this may related to the request for higher-kinded types and the error when trying to express
type[Generic]
(#7791) is the same.Is there a reason, why annotating with
type[Protocol]
(ortype[Generic]
) is not allowed? Also, the error message seems strange to me sinceProtocol
(andGeneric
) are clearly not variables but regular old classes, even though they are also special typing constructs.Not a big issue IMO, just thought I'd mention it.
The text was updated successfully, but these errors were encountered: