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 6fcb296
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 114 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
2 changes: 1 addition & 1 deletion test/generated/google/protobuf/duration_pb2.pyi
Original file line number Diff line number Diff line change
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
50 changes: 25 additions & 25 deletions test/generated/testproto/test3_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OuterEnum(_OuterEnum, metaclass=_OuterEnumEnumTypeWrapper): ...
UNKNOWN: OuterEnum.ValueType # 0
FOO3: OuterEnum.ValueType # 1
BAR3: OuterEnum.ValueType # 2
global___OuterEnum: typing_extensions.TypeAlias = OuterEnum
Global___OuterEnum: typing_extensions.TypeAlias = OuterEnum

@typing.final
class OuterMessage3(google.protobuf.message.Message):
Expand All @@ -50,7 +50,7 @@ class OuterMessage3(google.protobuf.message.Message):
) -> None: ...
def ClearField(self, field_name: typing.Literal["a_string", b"a_string"]) -> None: ...

global___OuterMessage3: typing_extensions.TypeAlias = OuterMessage3
Global___OuterMessage3: typing_extensions.TypeAlias = OuterMessage3

@typing.final
class SimpleProto3(google.protobuf.message.Message):
Expand Down Expand Up @@ -93,12 +93,12 @@ class SimpleProto3(google.protobuf.message.Message):
VALUE_FIELD_NUMBER: builtins.int
key: builtins.int
@property
def value(self) -> global___OuterMessage3: ...
def value(self) -> Global___OuterMessage3: ...
def __init__(
self,
*,
key: builtins.int = ...,
value: global___OuterMessage3 | None = ...,
value: Global___OuterMessage3 | None = ...,
) -> None: ...
def HasField(self, field_name: typing.Literal["value", b"value"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["key", b"key", "value", b"value"]) -> None: ...
Expand Down Expand Up @@ -141,57 +141,57 @@ class SimpleProto3(google.protobuf.message.Message):
EMAIL_FIELD_NUMBER: builtins.int
EMAIL_BY_UID_FIELD_NUMBER: builtins.int
a_string: builtins.str
a_outer_enum: global___OuterEnum.ValueType
inner_enum: global___SimpleProto3.InnerEnum.ValueType
a_outer_enum: Global___OuterEnum.ValueType
inner_enum: Global___SimpleProto3.InnerEnum.ValueType
a_oneof_1: builtins.str
a_oneof_2: builtins.str
outer_enum_in_oneof: global___OuterEnum.ValueType
inner_enum_in_oneof: global___SimpleProto3.InnerEnum.ValueType
outer_enum_in_oneof: Global___OuterEnum.ValueType
inner_enum_in_oneof: Global___SimpleProto3.InnerEnum.ValueType
b_oneof_1: builtins.str
b_oneof_2: builtins.str
OuterEnum: global___OuterEnum.ValueType
OuterEnum: Global___OuterEnum.ValueType
"""Test having fieldname match messagename"""
an_optional_string: builtins.str
user_id: test.test_generated_mypy.UserId
email: test.test_generated_mypy.Email
@property
def a_repeated_string(self) -> google.protobuf.internal.containers.RepeatedScalarFieldContainer[builtins.str]: ...
@property
def outer_message(self) -> global___OuterMessage3: ...
def outer_message(self) -> Global___OuterMessage3: ...
@property
def outer_message_in_oneof(self) -> global___OuterMessage3: ...
def outer_message_in_oneof(self) -> Global___OuterMessage3: ...
@property
def bool(self) -> global___OuterMessage3: ...
def bool(self) -> Global___OuterMessage3: ...
@property
def OuterMessage3(self) -> global___OuterMessage3: ...
def OuterMessage3(self) -> Global___OuterMessage3: ...
@property
def map_scalar(self) -> google.protobuf.internal.containers.ScalarMap[builtins.int, builtins.str]:
"""Test generation of map"""

@property
def map_message(self) -> google.protobuf.internal.containers.MessageMap[builtins.int, global___OuterMessage3]: ...
def map_message(self) -> google.protobuf.internal.containers.MessageMap[builtins.int, Global___OuterMessage3]: ...
@property
def email_by_uid(self) -> google.protobuf.internal.containers.ScalarMap[test.test_generated_mypy.UserId, test.test_generated_mypy.Email]: ...
def __init__(
self,
*,
a_string: builtins.str = ...,
a_repeated_string: collections.abc.Iterable[builtins.str] | None = ...,
a_outer_enum: global___OuterEnum.ValueType = ...,
outer_message: global___OuterMessage3 | None = ...,
inner_enum: global___SimpleProto3.InnerEnum.ValueType = ...,
a_outer_enum: Global___OuterEnum.ValueType = ...,
outer_message: Global___OuterMessage3 | None = ...,
inner_enum: Global___SimpleProto3.InnerEnum.ValueType = ...,
a_oneof_1: builtins.str = ...,
a_oneof_2: builtins.str = ...,
outer_message_in_oneof: global___OuterMessage3 | None = ...,
outer_enum_in_oneof: global___OuterEnum.ValueType = ...,
inner_enum_in_oneof: global___SimpleProto3.InnerEnum.ValueType = ...,
outer_message_in_oneof: Global___OuterMessage3 | None = ...,
outer_enum_in_oneof: Global___OuterEnum.ValueType = ...,
inner_enum_in_oneof: Global___SimpleProto3.InnerEnum.ValueType = ...,
b_oneof_1: builtins.str = ...,
b_oneof_2: builtins.str = ...,
bool: global___OuterMessage3 | None = ...,
OuterEnum: global___OuterEnum.ValueType = ...,
OuterMessage3: global___OuterMessage3 | None = ...,
bool: Global___OuterMessage3 | None = ...,
OuterEnum: Global___OuterEnum.ValueType = ...,
OuterMessage3: Global___OuterMessage3 | None = ...,
map_scalar: collections.abc.Mapping[builtins.int, builtins.str] | None = ...,
map_message: collections.abc.Mapping[builtins.int, global___OuterMessage3] | None = ...,
map_message: collections.abc.Mapping[builtins.int, Global___OuterMessage3] | None = ...,
an_optional_string: builtins.str | None = ...,
user_id: test.test_generated_mypy.UserId = ...,
email: test.test_generated_mypy.Email = ...,
Expand All @@ -206,4 +206,4 @@ class SimpleProto3(google.protobuf.message.Message):
@typing.overload
def WhichOneof(self, oneof_group: typing.Literal["b_oneof", b"b_oneof"]) -> typing.Literal["b_oneof_1", "b_oneof_2"] | None: ...

global___SimpleProto3: typing_extensions.TypeAlias = SimpleProto3
Global___SimpleProto3: typing_extensions.TypeAlias = SimpleProto3
4 changes: 2 additions & 2 deletions test/generated/testproto/test_extensions2_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SeparateFileExtension(google.protobuf.message.Message):
FLAG_FIELD_NUMBER: builtins.int
flag: builtins.bool
EXT_FIELD_NUMBER: builtins.int
ext: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[testproto.test_pb2.Simple2, global___SeparateFileExtension]
ext: google.protobuf.internal.extension_dict._ExtensionFieldDescriptor[testproto.test_pb2.Simple2, Global___SeparateFileExtension]
def __init__(
self,
*,
Expand All @@ -34,4 +34,4 @@ class SeparateFileExtension(google.protobuf.message.Message):
def HasField(self, field_name: typing.Literal["flag", b"flag"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["flag", b"flag"]) -> None: ...

global___SeparateFileExtension: typing_extensions.TypeAlias = SeparateFileExtension
Global___SeparateFileExtension: typing_extensions.TypeAlias = SeparateFileExtension
2 changes: 1 addition & 1 deletion test/generated/testproto/test_extensions3_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MessageOptionsTestMsg(google.protobuf.message.Message):
self,
) -> None: ...

global___MessageOptionsTestMsg: typing_extensions.TypeAlias = MessageOptionsTestMsg
Global___MessageOptionsTestMsg: typing_extensions.TypeAlias = MessageOptionsTestMsg

TEST_FIELD_EXTENSION_FIELD_NUMBER: builtins.int
SCALAR_OPTION_FIELD_NUMBER: builtins.int
Expand Down
2 changes: 1 addition & 1 deletion test/generated/testproto/test_no_generic_services_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class Simple3(google.protobuf.message.Message):
def HasField(self, field_name: typing.Literal["a_string", b"a_string"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["a_string", b"a_string"]) -> None: ...

global___Simple3: typing_extensions.TypeAlias = Simple3
Global___Simple3: typing_extensions.TypeAlias = Simple3
Loading

0 comments on commit 6fcb296

Please sign in to comment.