Skip to content

Commit

Permalink
Automated rollback of commit f799af8.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 629760811
  • Loading branch information
kmonte authored and copybara-github committed May 1, 2024
1 parent 9c8e0e6 commit c4bf83b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/google/protobuf/internal/enum_type_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
reflection_test.py
"""

import sys

__author__ = '[email protected] (Kevin Rabsatt)'


Expand Down Expand Up @@ -99,3 +101,12 @@ def __getattr__(self, name):
pass # fall out to break exception chaining
raise AttributeError('Enum {} has no value defined for name {!r}'.format(
self._enum_type.name, name))

def __or__(self, other):
"""Returns the union type of self and other."""
if sys.version_info >= (3, 10):
return type(self) | other
else:
raise NotImplementedError(
'You may not use | on EnumTypes (or classes) below python 3.10'
)
22 changes: 22 additions & 0 deletions python/google/protobuf/internal/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pickle
import pydoc
import sys
import types
import unittest
from unittest import mock
import warnings
Expand All @@ -30,6 +31,7 @@

from google.protobuf.internal import api_implementation # pylint: disable=g-import-not-at-top
from google.protobuf.internal import encoder
from google.protobuf.internal import enum_type_wrapper
from google.protobuf.internal import more_extensions_pb2
from google.protobuf.internal import more_messages_pb2
from google.protobuf.internal import packed_field_test_pb2
Expand Down Expand Up @@ -1314,6 +1316,26 @@ def __eq__(self, other):
self.assertNotEqual(m, ComparesWithFoo())
self.assertNotEqual(ComparesWithFoo(), m)

def testTypeUnion(self, message_module):
# Below python 3.10 you cannot create union types with the | operator, so we
# skip testing for unions with old versions.
if sys.version_info < (3, 10):
return
enum_type = enum_type_wrapper.EnumTypeWrapper(
message_module.TestAllTypes.NestedEnum.DESCRIPTOR
)
union_type = enum_type | int
self.assertIsInstance(union_type, types.UnionType)

def get_union() -> union_type:
return enum_type

union = get_union()
self.assertIsInstance(union, enum_type_wrapper.EnumTypeWrapper)
self.assertEqual(
union.DESCRIPTOR, message_module.TestAllTypes.NestedEnum.DESCRIPTOR
)


# Class to test proto2-only features (required, extensions, etc.)
@testing_refleaks.TestCase
Expand Down

0 comments on commit c4bf83b

Please sign in to comment.