Skip to content

Add support for well_known_types to google/protobuf #2157

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

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion third_party/2/google/protobuf/any_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from google.protobuf.message import (
Message,
)
from google.protobuf.internal import well_known_types

from typing import (
Optional,
Text,
)


class Any(Message):
class Any(Message, well_known_types.Any_):
type_url = ... # type: Text
value = ... # type: str

Expand Down
4 changes: 3 additions & 1 deletion third_party/2/google/protobuf/duration_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from google.protobuf.message import (
Message,
)
from google.protobuf.internal import well_known_types

from typing import (
Optional,
)


class Duration(Message):
class Duration(Message, well_known_types.Duration):
seconds = ... # type: int
nanos = ... # type: int

Expand Down
4 changes: 3 additions & 1 deletion third_party/2/google/protobuf/field_mask_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from google.protobuf.internal.containers import (
RepeatedScalarFieldContainer,
)
from google.protobuf.internal import well_known_types

from google.protobuf.message import (
Message,
)
Expand All @@ -11,7 +13,7 @@ from typing import (
)


class FieldMask(Message):
class FieldMask(Message, well_known_types.FieldMask):
paths = ... # type: RepeatedScalarFieldContainer[Text]

def __init__(self,
Expand Down
91 changes: 91 additions & 0 deletions third_party/2/google/protobuf/internal/well_known_types.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from typing import Any, Optional

class Error(Exception): ...
class ParseError(Error): ...

# This is named 'Any' in the original, but that conflicts with typing.Any,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we from typing import Any as Any_ instead to avoid this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that would require more changes to the code below (generated by stubgen), all of which uses Any as meaning typing.Any.

# and we really only need this file to mix in.
class Any_:
type_url: Any = ...
value: Any = ...
def Pack(self, msg: Any, type_url_prefix: str = ..., deterministic: Optional[Any] = ...) -> None: ...
def Unpack(self, msg: Any): ...
def TypeName(self): ...
def Is(self, descriptor: Any): ...

class Timestamp:
def ToJsonString(self): ...
seconds: Any = ...
nanos: Any = ...
def FromJsonString(self, value: Any) -> None: ...
def GetCurrentTime(self) -> None: ...
def ToNanoseconds(self): ...
def ToMicroseconds(self): ...
def ToMilliseconds(self): ...
def ToSeconds(self): ...
def FromNanoseconds(self, nanos: Any) -> None: ...
def FromMicroseconds(self, micros: Any) -> None: ...
def FromMilliseconds(self, millis: Any) -> None: ...
def FromSeconds(self, seconds: Any) -> None: ...
def ToDatetime(self): ...
def FromDatetime(self, dt: Any) -> None: ...

class Duration:
def ToJsonString(self): ...
seconds: Any = ...
nanos: Any = ...
def FromJsonString(self, value: Any) -> None: ...
def ToNanoseconds(self): ...
def ToMicroseconds(self): ...
def ToMilliseconds(self): ...
def ToSeconds(self): ...
def FromNanoseconds(self, nanos: Any) -> None: ...
def FromMicroseconds(self, micros: Any) -> None: ...
def FromMilliseconds(self, millis: Any) -> None: ...
def FromSeconds(self, seconds: Any) -> None: ...
def ToTimedelta(self): ...
def FromTimedelta(self, td: Any) -> None: ...

class FieldMask:
def ToJsonString(self): ...
def FromJsonString(self, value: Any) -> None: ...
def IsValidForDescriptor(self, message_descriptor: Any): ...
def AllFieldsFromDescriptor(self, message_descriptor: Any) -> None: ...
def CanonicalFormFromMask(self, mask: Any) -> None: ...
def Union(self, mask1: Any, mask2: Any) -> None: ...
def Intersect(self, mask1: Any, mask2: Any) -> None: ...
def MergeMessage(self, source: Any, destination: Any, replace_message_field: bool = ..., replace_repeated_field: bool = ...) -> None: ...

class _FieldMaskTree:
def __init__(self, field_mask: Optional[Any] = ...) -> None: ...
def MergeFromFieldMask(self, field_mask: Any) -> None: ...
def AddPath(self, path: Any): ...
def ToFieldMask(self, field_mask: Any) -> None: ...
def IntersectPath(self, path: Any, intersection: Any): ...
def AddLeafNodes(self, prefix: Any, node: Any) -> None: ...
def MergeMessage(self, source: Any, destination: Any, replace_message: Any, replace_repeated: Any) -> None: ...

class Struct:
def __getitem__(self, key: Any): ...
def __contains__(self, item: Any): ...
def __setitem__(self, key: Any, value: Any) -> None: ...
def __delitem__(self, key: Any) -> None: ...
def __len__(self): ...
def __iter__(self): ...
def keys(self): ...
def values(self): ...
def items(self): ...
def get_or_create_list(self, key: Any): ...
def get_or_create_struct(self, key: Any): ...
def update(self, dictionary: Any) -> None: ...

class ListValue:
def __len__(self): ...
def append(self, value: Any) -> None: ...
def extend(self, elem_seq: Any) -> None: ...
def __getitem__(self, index: Any): ...
def __setitem__(self, index: Any, value: Any) -> None: ...
def __delitem__(self, key: Any) -> None: ...
def items(self) -> None: ...
def add_struct(self): ...
def add_list(self): ...
6 changes: 4 additions & 2 deletions third_party/2/google/protobuf/struct_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from google.protobuf.internal.containers import (
RepeatedCompositeFieldContainer,
)
from google.protobuf.internal import well_known_types

from google.protobuf.message import (
Message,
)
Expand Down Expand Up @@ -36,7 +38,7 @@ class NullValue(int):
NULL_VALUE: NullValue


class Struct(Message):
class Struct(Message, well_known_types.Struct):
class FieldsEntry(Message):
key = ... # type: Text

Expand Down Expand Up @@ -90,7 +92,7 @@ class _Value(Message):
Value = _Value


class ListValue(Message):
class ListValue(Message, well_known_types.ListValue):

@property
def values(self) -> RepeatedCompositeFieldContainer[Value]: ...
Expand Down
4 changes: 3 additions & 1 deletion third_party/2/google/protobuf/timestamp_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from google.protobuf.message import (
Message,
)
from google.protobuf.internal import well_known_types

from typing import (
Optional,
)


class Timestamp(Message):
class Timestamp(Message, well_known_types.Timestamp):
seconds = ... # type: int
nanos = ... # type: int

Expand Down