mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Disallow type(x) if x has a protocol type

Open JukkaL opened this issue 1 year ago • 0 comments

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.

JukkaL avatar Feb 15 '24 10:02 JukkaL