mypy
mypy copied to clipboard
Disallow type(x) if x has a protocol type
Modules can implement protocols, so type(x) is not necessarily a type object if x has a protocol type.
from typing import Protocol
class P(Protocol):
def f(self) -> None: ...
def f(p: P) -> None:
reveal_type(type(p)) # type[P] (incorrect!)
It's probably best to to generate an error if type is called on an object with a protocol type, as suggested by @hauntsaninja in https://github.com/python/mypy/issues/16890#issuecomment-1937529825.
See #16890 for more context.