Skip to content

Commit 96a09f6

Browse files
authored
protobuf: Bring back Message.Extensions (#7154)
1 parent 494481a commit 96a09f6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

stubs/protobuf/@tests/stubtest_allowlist.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
# carefully in mypy-protobuf which internally runs stubtest. Skip those here.
33
google.protobuf\..*_pb2\..*
44

5-
google.protobuf.internal.containers.BaseContainer.sort
65
# While Message and Descriptor are both defined with a null DESCRIPTOR,
76
# subclasses of Message and instances of EnumTypeWrapper require this value to
87
# be set, and since these type stubs are intended for use with protoc-generated
98
# python it's more accurate to make them non-nullable.
109
google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper.DESCRIPTOR
1110
google.protobuf.message.Message.DESCRIPTOR
11+
12+
# Exists at runtime, but via a __getitem__/__setitem__ hack
13+
# See https://github.com/protocolbuffers/protobuf/blob/3ea30d80847cd9561db570ae7f673afc15523545/python/google/protobuf/message.py#L67
14+
google.protobuf.message.Message.Extensions
15+
16+
# These are typed as (self, **kwargs) at runtime as thin wrapper functions
17+
# around the underlying true typing. We prefer the true typing
18+
google.protobuf.internal.containers.BaseContainer.sort
1219
google.protobuf.message.Message.SerializePartialToString
1320
google.protobuf.message.Message.SerializeToString
14-
google.protobuf.service.Service.GetDescriptor
1521
google.protobuf.text_format.MessageToBytes
22+
23+
# Stubbed as static method, but actually exists as a property that's
24+
# a function. Typeshed's typing is more useful
25+
google.protobuf.service.Service.GetDescriptor

stubs/protobuf/google/protobuf/message.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from _typeshed import Self
22
from typing import Any, Sequence, TypeVar
33

44
from .descriptor import Descriptor, FieldDescriptor
5-
from .internal.extension_dict import _ExtensionFieldDescriptor
5+
from .internal.extension_dict import _ExtensionDict, _ExtensionFieldDescriptor
66

77
class Error(Exception): ...
88
class DecodeError(Error): ...
@@ -28,6 +28,9 @@ class Message:
2828
# The TypeVar must be bound to `Message` or we get mypy errors, so we cannot use `Self` for `HasExtension` & `ClearExtension`
2929
def HasExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> bool: ...
3030
def ClearExtension(self: _M, extension_handle: _ExtensionFieldDescriptor[_M, Any]) -> None: ...
31+
# The TypeVar must be bound to `Message` or we get mypy errors, so we cannot use `Self` for `Extensions`
32+
@property
33+
def Extensions(self: _M) -> _ExtensionDict[_M]: ...
3134
def ByteSize(self) -> int: ...
3235
@classmethod
3336
def FromString(cls: type[Self], s: bytes) -> Self: ...

0 commit comments

Comments
 (0)