Skip to content

Should we use _ProtocolMeta in typing.py? #8998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sobolevn opened this issue Oct 27, 2022 · 1 comment
Closed

Should we use _ProtocolMeta in typing.py? #8998

sobolevn opened this issue Oct 27, 2022 · 1 comment

Comments

@sobolevn
Copy link
Member

Right now _ProtocolMeta is never used:

class _ProtocolMeta(ABCMeta): ...

But, it is a base metaclass for Protocol type in typing.py: https://github.com/python/cpython/blame/723ebe76e787cfa6b08cc9587dd679f3234a1025/Lib/typing.py#L1965

It is also used for all Protocol types (we currently just use ABCMeta for them`):

typeshed/stdlib/typing.pyi

Lines 287 to 305 in 739711e

@runtime_checkable
class SupportsInt(Protocol, metaclass=ABCMeta):
@abstractmethod
def __int__(self) -> int: ...
@runtime_checkable
class SupportsFloat(Protocol, metaclass=ABCMeta):
@abstractmethod
def __float__(self) -> float: ...
@runtime_checkable
class SupportsComplex(Protocol, metaclass=ABCMeta):
@abstractmethod
def __complex__(self) -> complex: ...
@runtime_checkable
class SupportsBytes(Protocol, metaclass=ABCMeta):
@abstractmethod
def __bytes__(self) -> bytes: ...

CPython: https://github.com/python/cpython/blame/723ebe76e787cfa6b08cc9587dd679f3234a1025/Lib/typing.py#L2745

So, the question is:

  1. Should we use for Protocol class? It is rather complex, because right now Protocol is _SpecialForm
  2. Should we use it for Protocol types in typing.pyi?
@JelleZijlstra
Copy link
Member

typing.pyi is central to type checking and typecheckers need to heavily special case for the contents of the stub. Therefore, I think we should only make changes here if they fix a concrete problem for users, not out of mere perfectionism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants