typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Add `NamedTupleMeta` metaclass

Open sobolevn opened this issue 2 years ago • 7 comments

Refs https://github.com/python/mypy/issues/16521

Source:

  • 3.8: https://github.com/python/cpython/blob/3.8/Lib/typing.py#L1613
  • 3.9: https://github.com/python/cpython/blob/3.9/Lib/typing.py#L1864
  • 3.10: https://github.com/python/cpython/blob/3.10/Lib/typing.py#L2265
  • 3.11: https://github.com/python/cpython/blob/3.11/Lib/typing.py#L2885
  • 3.12: https://github.com/python/cpython/blob/3.12/Lib/typing.py#L2739

sobolevn avatar Nov 19 '23 07:11 sobolevn

Mypy does not like this change:

stdlib/typing.pyi:865: error: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

sobolevn avatar Nov 19 '23 07:11 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Nov 19 '23 07:11 github-actions[bot]

I think the reason is that tuple inherits types that use ABCMeta, so, let's preted that NamedTupleMeta also has ABCMeta.

sobolevn avatar Nov 19 '23 08:11 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Nov 19 '23 08:11 github-actions[bot]

Now pyright finds some cases where NamedTuple is used together with Enum:

class _ASN1ObjectBase(NamedTuple):
    nid: int
    shortname: str
    longname: str
    oid: str

class _ASN1Object(_ASN1ObjectBase):
    def __new__(cls, oid: str) -> Self: ...
    @classmethod
    def fromnid(cls, nid: int) -> Self: ...
    @classmethod
    def fromname(cls, name: str) -> Self: ...

class Purpose(_ASN1Object, enum.Enum):
    SERVER_AUTH: _ASN1Object
    CLIENT_AUTH: _ASN1Object

Another option for us is to use # type: ignore[misc] on this definition. I will give this a try.

sobolevn avatar Nov 19 '23 08:11 sobolevn

:(

sobolevn avatar Nov 19 '23 08:11 sobolevn

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Nov 19 '23 08:11 github-actions[bot]