Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/internal/enum_type_wrapper.py: 35%

34 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:40 +0000

1# Protocol Buffers - Google's data interchange format 

2# Copyright 2008 Google Inc. All rights reserved. 

3# 

4# Use of this source code is governed by a BSD-style 

5# license that can be found in the LICENSE file or at 

6# https://developers.google.com/open-source/licenses/bsd 

7 

8"""A simple wrapper around enum types to expose utility functions. 

9 

10Instances are created as properties with the same name as the enum they wrap 

11on proto classes. For usage, see: 

12 reflection_test.py 

13""" 

14 

15__author__ = 'rabsatt@google.com (Kevin Rabsatt)' 

16 

17 

18class EnumTypeWrapper(object): 

19 """A utility for finding the names of enum values.""" 

20 

21 DESCRIPTOR = None 

22 

23 # This is a type alias, which mypy typing stubs can type as 

24 # a genericized parameter constrained to an int, allowing subclasses 

25 # to be typed with more constraint in .pyi stubs 

26 # Eg. 

27 # def MyGeneratedEnum(Message): 

28 # ValueType = NewType('ValueType', int) 

29 # def Name(self, number: MyGeneratedEnum.ValueType) -> str 

30 ValueType = int 

31 

32 def __init__(self, enum_type): 

33 """Inits EnumTypeWrapper with an EnumDescriptor.""" 

34 self._enum_type = enum_type 

35 self.DESCRIPTOR = enum_type # pylint: disable=invalid-name 

36 

37 def Name(self, number): # pylint: disable=invalid-name 

38 """Returns a string containing the name of an enum value.""" 

39 try: 

40 return self._enum_type.values_by_number[number].name 

41 except KeyError: 

42 pass # fall out to break exception chaining 

43 

44 if not isinstance(number, int): 

45 raise TypeError( 

46 'Enum value for {} must be an int, but got {} {!r}.'.format( 

47 self._enum_type.name, type(number), number)) 

48 else: 

49 # repr here to handle the odd case when you pass in a boolean. 

50 raise ValueError('Enum {} has no name defined for value {!r}'.format( 

51 self._enum_type.name, number)) 

52 

53 def Value(self, name): # pylint: disable=invalid-name 

54 """Returns the value corresponding to the given enum name.""" 

55 try: 

56 return self._enum_type.values_by_name[name].number 

57 except KeyError: 

58 pass # fall out to break exception chaining 

59 raise ValueError('Enum {} has no value defined for name {!r}'.format( 

60 self._enum_type.name, name)) 

61 

62 def keys(self): 

63 """Return a list of the string names in the enum. 

64 

65 Returns: 

66 A list of strs, in the order they were defined in the .proto file. 

67 """ 

68 

69 return [value_descriptor.name 

70 for value_descriptor in self._enum_type.values] 

71 

72 def values(self): 

73 """Return a list of the integer values in the enum. 

74 

75 Returns: 

76 A list of ints, in the order they were defined in the .proto file. 

77 """ 

78 

79 return [value_descriptor.number 

80 for value_descriptor in self._enum_type.values] 

81 

82 def items(self): 

83 """Return a list of the (name, value) pairs of the enum. 

84 

85 Returns: 

86 A list of (str, int) pairs, in the order they were defined 

87 in the .proto file. 

88 """ 

89 return [(value_descriptor.name, value_descriptor.number) 

90 for value_descriptor in self._enum_type.values] 

91 

92 def __getattr__(self, name): 

93 """Returns the value corresponding to the given enum name.""" 

94 try: 

95 return super( 

96 EnumTypeWrapper, 

97 self).__getattribute__('_enum_type').values_by_name[name].number 

98 except KeyError: 

99 pass # fall out to break exception chaining 

100 raise AttributeError('Enum {} has no value defined for name {!r}'.format( 

101 self._enum_type.name, name))