Skip to content

Commit

Permalink
Use Global___ for mangling prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Apr 25, 2024
1 parent 0a10dbd commit b158860
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 127 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Upcoming

- Mark top-level mangled identifiers `global___*` as `TypeAlias`.
- Mark top-level mangled identifiers as `TypeAlias`.
- Change the top-level mangling prefix from `global___` to `Global___` to respect
[Y042](https://github.com/PyCQA/flake8-pyi/blob/main/ERRORCODES.md#list-of-warnings) naming convention.

## 3.6.0

Expand Down
4 changes: 2 additions & 2 deletions mypy_protobuf/extensions_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class FieldOptions(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["casttype", b"casttype", "keytype", b"keytype", "valuetype", b"valuetype"]) -> None: ...

global___FieldOptions: typing_extensions.TypeAlias = FieldOptions
Global___FieldOptions: typing_extensions.TypeAlias = FieldOptions

OPTIONS_FIELD_NUMBER: builtins.int
CASTTYPE_FIELD_NUMBER: builtins.int
KEYTYPE_FIELD_NUMBER: builtins.int
VALUETYPE_FIELD_NUMBER: builtins.int
options: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, global___FieldOptions]
options: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, Global___FieldOptions]
"""Custom field options from mypy-protobuf"""
casttype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, builtins.str]
"""Legacy fields. Prefer to use ones within `options` instead."""
Expand Down
6 changes: 3 additions & 3 deletions mypy_protobuf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def _mangle_global_identifier(name: str) -> str:
Eg:
Enum variant `Name` or message field `Name` might conflict with a top level
message or enum named `Name`, so mangle it with a global___ prefix for
message or enum named `Name`, so mangle it with a `Global___` prefix for
internal references. Note that this doesn't affect inner enums/messages
because they get fuly qualified when referenced within a file"""
return f"global___{name}"
because they get fully qualified when referenced within a file"""
return f"Global___{name}"


class Descriptors(object):
Expand Down
3 changes: 2 additions & 1 deletion stubtest_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ testproto.grpc.dummy_pb2_grpc.DummyService
testproto.grpc.import_pb2_grpc.SimpleService

# global prefix globals are generated, but aren't used at runtime
.*_pb2\.global___.*
# using `__` would be preferable, but Y047 will detect unused names.
.*_pb2\.Global___.*

# All readonly generated @property fields seem to trip up
# stubtest. It claims they aren't available at runtime, when practically,
Expand Down
28 changes: 14 additions & 14 deletions test/generated/google/protobuf/duration_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,51 @@ class Duration(google.protobuf.message.Message, google.protobuf.internal.well_kn
or "month". It is related to Timestamp in that the difference between
two Timestamp values is a Duration and it can be added or subtracted
from a Timestamp. Range is approximately +-10,000 years.
# Examples
Example 1: Compute Duration from two Timestamps in pseudo code.
Timestamp start = ...;
Timestamp end = ...;
Duration duration = ...;
duration.seconds = end.seconds - start.seconds;
duration.nanos = end.nanos - start.nanos;
if (duration.seconds < 0 && duration.nanos > 0) {
duration.seconds += 1;
duration.nanos -= 1000000000;
} else if (duration.seconds > 0 && duration.nanos < 0) {
duration.seconds -= 1;
duration.nanos += 1000000000;
}
Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
Timestamp start = ...;
Duration duration = ...;
Timestamp end = ...;
end.seconds = start.seconds + duration.seconds;
end.nanos = start.nanos + duration.nanos;
if (end.nanos < 0) {
end.seconds -= 1;
end.nanos += 1000000000;
} else if (end.nanos >= 1000000000) {
end.seconds += 1;
end.nanos -= 1000000000;
}
Example 3: Compute Duration from datetime.timedelta in Python.
td = datetime.timedelta(days=3, minutes=10)
duration = Duration()
duration.FromTimedelta(td)
# JSON Mapping
In JSON format, the Duration type is encoded as a string rather than an
object, where the string ends in the suffix "s" (indicating seconds) and
is preceded by the number of seconds, with nanoseconds expressed as
Expand Down Expand Up @@ -133,4 +133,4 @@ class Duration(google.protobuf.message.Message, google.protobuf.internal.well_kn
) -> None: ...
def ClearField(self, field_name: typing.Literal["nanos", b"nanos", "seconds", b"seconds"]) -> None: ...

global___Duration: typing_extensions.TypeAlias = Duration
Global___Duration: typing_extensions.TypeAlias = Duration
4 changes: 2 additions & 2 deletions test/generated/mypy_protobuf/extensions_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class FieldOptions(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["casttype", b"casttype", "keytype", b"keytype", "valuetype", b"valuetype"]) -> None: ...

global___FieldOptions: typing_extensions.TypeAlias = FieldOptions
Global___FieldOptions: typing_extensions.TypeAlias = FieldOptions

OPTIONS_FIELD_NUMBER: builtins.int
CASTTYPE_FIELD_NUMBER: builtins.int
KEYTYPE_FIELD_NUMBER: builtins.int
VALUETYPE_FIELD_NUMBER: builtins.int
options: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, global___FieldOptions]
options: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, Global___FieldOptions]
"""Custom field options from mypy-protobuf"""
casttype: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[google.protobuf.descriptor_pb2.FieldOptions, builtins.str]
"""Legacy fields. Prefer to use ones within `options` instead."""
Expand Down
14 changes: 7 additions & 7 deletions test/generated/testproto/Capitalized/Capitalized_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,38 @@ class lower(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["a", b"a"]) -> None: ...

global___lower: typing_extensions.TypeAlias = lower
Global___lower: typing_extensions.TypeAlias = lower

@typing.final
class Upper(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor

LOWER_FIELD_NUMBER: builtins.int
@property
def Lower(self) -> global___lower: ...
def Lower(self) -> Global___lower: ...
def __init__(
self,
*,
Lower: global___lower | None = ...,
Lower: Global___lower | None = ...,
) -> None: ...
def HasField(self, field_name: typing.Literal["Lower", b"Lower"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["Lower", b"Lower"]) -> None: ...

global___Upper: typing_extensions.TypeAlias = Upper
Global___Upper: typing_extensions.TypeAlias = Upper

@typing.final
class lower2(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor

UPPER_FIELD_NUMBER: builtins.int
@property
def upper(self) -> global___Upper: ...
def upper(self) -> Global___Upper: ...
def __init__(
self,
*,
upper: global___Upper | None = ...,
upper: Global___Upper | None = ...,
) -> None: ...
def HasField(self, field_name: typing.Literal["upper", b"upper"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["upper", b"upper"]) -> None: ...

global___lower2: typing_extensions.TypeAlias = lower2
Global___lower2: typing_extensions.TypeAlias = lower2
2 changes: 1 addition & 1 deletion test/generated/testproto/comment_special_chars_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ class Test(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["a", b"a", "b", b"b", "c", b"c", "d", b"d", "e", b"e", "f", b"f", "g", b"g", "h", b"h", "i", b"i", "j", b"j", "k", b"k"]) -> None: ...

global___Test: typing_extensions.TypeAlias = Test
Global___Test: typing_extensions.TypeAlias = Test
2 changes: 1 addition & 1 deletion test/generated/testproto/dot/com/test_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ class TestMessage(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["foo", b"foo"]) -> None: ...

global___TestMessage: typing_extensions.TypeAlias = TestMessage
Global___TestMessage: typing_extensions.TypeAlias = TestMessage
4 changes: 2 additions & 2 deletions test/generated/testproto/grpc/dummy_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DummyRequest(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["value", b"value"]) -> None: ...

global___DummyRequest: typing_extensions.TypeAlias = DummyRequest
Global___DummyRequest: typing_extensions.TypeAlias = DummyRequest

@typing.final
class DummyReply(google.protobuf.message.Message):
Expand All @@ -44,4 +44,4 @@ class DummyReply(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["value", b"value"]) -> None: ...

global___DummyReply: typing_extensions.TypeAlias = DummyReply
Global___DummyReply: typing_extensions.TypeAlias = DummyReply
2 changes: 1 addition & 1 deletion test/generated/testproto/inner/inner_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class Inner(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["a", b"a"]) -> None: ...

global___Inner: typing_extensions.TypeAlias = Inner
Global___Inner: typing_extensions.TypeAlias = Inner
12 changes: 6 additions & 6 deletions test/generated/testproto/nested/nested_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Nested(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["a", b"a"]) -> None: ...

global___Nested: typing_extensions.TypeAlias = Nested
Global___Nested: typing_extensions.TypeAlias = Nested

@typing.final
class AnotherNested(google.protobuf.message.Message):
Expand Down Expand Up @@ -77,20 +77,20 @@ class AnotherNested(google.protobuf.message.Message):
NE2_FIELD_NUMBER: builtins.int
s: builtins.str
b: builtins.bool
ne: global___AnotherNested.NestedEnum.ValueType
ne2: global___AnotherNested.NestedMessage.NestedEnum2.ValueType
ne: Global___AnotherNested.NestedEnum.ValueType
ne2: Global___AnotherNested.NestedMessage.NestedEnum2.ValueType
def __init__(
self,
*,
s: builtins.str = ...,
b: builtins.bool = ...,
ne: global___AnotherNested.NestedEnum.ValueType = ...,
ne2: global___AnotherNested.NestedMessage.NestedEnum2.ValueType = ...,
ne: Global___AnotherNested.NestedEnum.ValueType = ...,
ne2: Global___AnotherNested.NestedMessage.NestedEnum2.ValueType = ...,
) -> None: ...
def ClearField(self, field_name: typing.Literal["b", b"b", "ne", b"ne", "ne2", b"ne2", "s", b"s"]) -> None: ...

def __init__(
self,
) -> None: ...

global___AnotherNested: typing_extensions.TypeAlias = AnotherNested
Global___AnotherNested: typing_extensions.TypeAlias = AnotherNested
12 changes: 6 additions & 6 deletions test/generated/testproto/nopackage_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NoPackage(google.protobuf.message.Message):
self,
) -> None: ...

global___NoPackage: typing_extensions.TypeAlias = NoPackage
Global___NoPackage: typing_extensions.TypeAlias = NoPackage

@typing.final
class NoPackage2(google.protobuf.message.Message):
Expand All @@ -37,16 +37,16 @@ class NoPackage2(google.protobuf.message.Message):
NP_FIELD_NUMBER: builtins.int
NP_REP_FIELD_NUMBER: builtins.int
@property
def np(self) -> global___NoPackage: ...
def np(self) -> Global___NoPackage: ...
@property
def np_rep(self) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[global___NoPackage]: ...
def np_rep(self) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[Global___NoPackage]: ...
def __init__(
self,
*,
np: global___NoPackage | None = ...,
np_rep: collections.abc.Iterable[global___NoPackage] | None = ...,
np: Global___NoPackage | None = ...,
np_rep: collections.abc.Iterable[Global___NoPackage] | None = ...,
) -> None: ...
def HasField(self, field_name: typing.Literal["np", b"np"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["np", b"np", "np_rep", b"np_rep"]) -> None: ...

global___NoPackage2: typing_extensions.TypeAlias = NoPackage2
Global___NoPackage2: typing_extensions.TypeAlias = NoPackage2
2 changes: 1 addition & 1 deletion test/generated/testproto/readme_enum_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ class MyEnum(_MyEnum, metaclass=_MyEnumEnumTypeWrapper): ...

HELLO: MyEnum.ValueType # 0
WORLD: MyEnum.ValueType # 1
global___MyEnum: typing_extensions.TypeAlias = MyEnum
Global___MyEnum: typing_extensions.TypeAlias = MyEnum
Loading

0 comments on commit b158860

Please sign in to comment.