From f3d2e8a980c99252000bea2608969c87bde06989 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 18 Jan 2022 19:54:25 +0000 Subject: [PATCH 1/2] Use `_typeshed.Self` in `protobuf/google/protobuf/message.pyi` --- stubs/protobuf/google/protobuf/message.pyi | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stubs/protobuf/google/protobuf/message.pyi b/stubs/protobuf/google/protobuf/message.pyi index 55f087701af3..fe67d97fa2b0 100644 --- a/stubs/protobuf/google/protobuf/message.pyi +++ b/stubs/protobuf/google/protobuf/message.pyi @@ -1,4 +1,5 @@ -from typing import Any, Sequence, TypeVar +from _typeshed import Self +from typing import Any, Sequence from .descriptor import Descriptor, FieldDescriptor from .internal.extension_dict import _ExtensionDict, _ExtensionFieldDescriptor @@ -7,15 +8,13 @@ class Error(Exception): ... class DecodeError(Error): ... class EncodeError(Error): ... -_M = TypeVar("_M", bound=Message) # message type (of self) - class Message: DESCRIPTOR: Descriptor def __deepcopy__(self, memo=...): ... def __eq__(self, other_msg): ... def __ne__(self, other_msg): ... - def MergeFrom(self: _M, other_msg: _M) -> None: ... - def CopyFrom(self: _M, other_msg: _M) -> None: ... + def MergeFrom(self: Self, other_msg: Self) -> None: ... + def CopyFrom(self: Self, other_msg: Self) -> None: ... def Clear(self) -> None: ... def SetInParent(self) -> None: ... def IsInitialized(self) -> bool: ... @@ -24,13 +23,13 @@ class Message: def SerializeToString(self, deterministic: bool = ...) -> bytes: ... def SerializePartialToString(self, deterministic: bool = ...) -> bytes: ... def ListFields(self) -> Sequence[tuple[FieldDescriptor, Any]]: ... - def HasExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> bool: ... - def ClearExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> None: ... + def HasExtension(self: Self, extension_handle: _ExtensionFieldDescriptor[Self, Any]) -> bool: ... + def ClearExtension(self: Self, extension_handle: _ExtensionFieldDescriptor[Self, Any]) -> None: ... def ByteSize(self) -> int: ... @classmethod - def FromString(cls: type[_M], s: bytes) -> _M: ... + def FromString(cls: type[Self], s: bytes) -> Self: ... @property - def Extensions(self: _M) -> _ExtensionDict[_M]: ... + def Extensions(self: Self) -> _ExtensionDict[Self]: ... # Intentionally left out typing on these three methods, because they are # stringly typed and it is not useful to call them on a Message directly. # We prefer more specific typing on individual subclasses of Message From 553d38ace6694523df9ad7c53a2c11dbcec3a439 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 18 Jan 2022 20:01:32 +0000 Subject: [PATCH 2/2] Fix --- stubs/protobuf/google/protobuf/message.pyi | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/stubs/protobuf/google/protobuf/message.pyi b/stubs/protobuf/google/protobuf/message.pyi index fe67d97fa2b0..209c4a189099 100644 --- a/stubs/protobuf/google/protobuf/message.pyi +++ b/stubs/protobuf/google/protobuf/message.pyi @@ -1,5 +1,5 @@ from _typeshed import Self -from typing import Any, Sequence +from typing import Any, Sequence, TypeVar from .descriptor import Descriptor, FieldDescriptor from .internal.extension_dict import _ExtensionDict, _ExtensionFieldDescriptor @@ -8,6 +8,8 @@ class Error(Exception): ... class DecodeError(Error): ... class EncodeError(Error): ... +_M = TypeVar("_M", bound=Message) # message type (of self) + class Message: DESCRIPTOR: Descriptor def __deepcopy__(self, memo=...): ... @@ -23,13 +25,15 @@ class Message: def SerializeToString(self, deterministic: bool = ...) -> bytes: ... def SerializePartialToString(self, deterministic: bool = ...) -> bytes: ... def ListFields(self) -> Sequence[tuple[FieldDescriptor, Any]]: ... - def HasExtension(self: Self, extension_handle: _ExtensionFieldDescriptor[Self, Any]) -> bool: ... - def ClearExtension(self: Self, extension_handle: _ExtensionFieldDescriptor[Self, Any]) -> None: ... + # The TypeVar must be bound to `Message` or we get mypy errors, so we cannot use `Self` for `HasExtension` & `ClearExtension` + def HasExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> bool: ... + def ClearExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> None: ... def ByteSize(self) -> int: ... @classmethod def FromString(cls: type[Self], s: bytes) -> Self: ... + # The TypeVar must be bound to `Message` or we get mypy errors, so we cannot use `Self` for `Extensions` @property - def Extensions(self: Self) -> _ExtensionDict[Self]: ... + def Extensions(self: _M) -> _ExtensionDict[_M]: ... # Intentionally left out typing on these three methods, because they are # stringly typed and it is not useful to call them on a Message directly. # We prefer more specific typing on individual subclasses of Message