Description
Bug Report
Attempting to annotate something as any Protocol
causes an error.
To Reproduce
from typing import Protocol
def f(p: type[Protocol]) -> None: ...
Expected Behavior
No problem.
Actual Behavior
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.