Skip to content

Commit f69180a

Browse files
Remove calls to Label within Protobuf Python.
PiperOrigin-RevId: 756880197
1 parent 183bfee commit f69180a

File tree

11 files changed

+28
-26
lines changed

11 files changed

+28
-26
lines changed

python/google/protobuf/descriptor_pool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def _IsMessageSetExtension(field):
7575
field.containing_type.has_options and
7676
field.containing_type.GetOptions().message_set_wire_format and
7777
field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and
78-
field.label == descriptor.FieldDescriptor.LABEL_OPTIONAL)
78+
not field.is_required and
79+
not field.is_repeated)
7980

8081
_edition_defaults_lock = threading.Lock()
8182

python/google/protobuf/internal/descriptor_pool_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def testFindFieldByName(self):
329329
field = self.pool.FindFieldByName(
330330
'google.protobuf.python.internal.Factory1Message.list_value')
331331
self.assertEqual(field.name, 'list_value')
332-
self.assertEqual(field.label, field.LABEL_REPEATED)
332+
self.assertTrue(field.is_repeated)
333333
self.assertFalse(field.has_options)
334334

335335
with self.assertRaises(KeyError):

python/google/protobuf/internal/extension_dict.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __getitem__(self, extension_handle):
6161
if result is not None:
6262
return result
6363

64-
if extension_handle.label == FieldDescriptor.LABEL_REPEATED:
64+
if extension_handle.is_repeated:
6565
result = extension_handle._default_constructor(self._extended_message)
6666
elif extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:
6767
message_type = extension_handle.message_type
@@ -129,7 +129,7 @@ def __setitem__(self, extension_handle, value):
129129

130130
_VerifyExtensionHandle(self._extended_message, extension_handle)
131131

132-
if (extension_handle.label == FieldDescriptor.LABEL_REPEATED or
132+
if (extension_handle.is_repeated or
133133
extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE):
134134
raise TypeError(
135135
'Cannot assign to extension "%s" because it is a repeated or '
@@ -183,7 +183,7 @@ def __contains__(self, extension_handle):
183183
if extension_handle not in self._extended_message._fields:
184184
return False
185185

186-
if extension_handle.label == FieldDescriptor.LABEL_REPEATED:
186+
if extension_handle.is_repeated:
187187
return bool(self._extended_message._fields.get(extension_handle))
188188

189189
if extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:

python/google/protobuf/internal/field_mask.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _IsValidPath(message_descriptor, path):
100100
for name in parts:
101101
field = message_descriptor.fields_by_name.get(name)
102102
if (field is None or
103-
field.label == FieldDescriptor.LABEL_REPEATED or
103+
field.is_repeated or
104104
field.type != FieldDescriptor.TYPE_MESSAGE):
105105
return False
106106
message_descriptor = field.message_type
@@ -271,7 +271,7 @@ def _MergeMessage(
271271
name, source_descriptor.full_name))
272272
if child:
273273
# Sub-paths are only allowed for singular message fields.
274-
if (field.label == FieldDescriptor.LABEL_REPEATED or
274+
if (field.is_repeated or
275275
field.cpp_type != FieldDescriptor.CPPTYPE_MESSAGE):
276276
raise ValueError('Error: Field {0} in message {1} is not a singular '
277277
'message field and cannot have sub-fields.'.format(
@@ -281,7 +281,7 @@ def _MergeMessage(
281281
child, getattr(source, name), getattr(destination, name),
282282
replace_message, replace_repeated)
283283
continue
284-
if field.label == FieldDescriptor.LABEL_REPEATED:
284+
if field.is_repeated:
285285
if replace_repeated:
286286
destination.ClearField(_StrConvert(name))
287287
repeated_source = getattr(source, name)

python/google/protobuf/internal/field_mask_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def testMergeMessageWithoutMapFields(self):
193193
mask.MergeMessage(src, dst)
194194
# The expected result message.
195195
msg = unittest_pb2.TestAllTypes()
196-
if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
196+
if field.is_repeated:
197197
repeated_src = getattr(src, field_name)
198198
repeated_msg = getattr(msg, field_name)
199199
if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:

python/google/protobuf/internal/message_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ def testProto3Optional(self):
19051905
if field.name.startswith('optional_'):
19061906
self.assertTrue(field.has_presence)
19071907
for field in unittest_pb2.TestAllTypes.DESCRIPTOR.fields:
1908-
if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
1908+
if field.is_repeated:
19091909
self.assertFalse(field.has_presence)
19101910
else:
19111911
self.assertTrue(field.has_presence)

python/google/protobuf/internal/python_message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ def _IsMessageSetExtension(field):
256256
field.containing_type.has_options and
257257
field.containing_type.GetOptions().message_set_wire_format and
258258
field.type == _FieldDescriptor.TYPE_MESSAGE and
259-
field.label == _FieldDescriptor.LABEL_OPTIONAL)
259+
not field.is_required and
260+
not field.is_repeated)
260261

261262

262263
def _IsMapField(field):
@@ -1260,7 +1261,7 @@ def _AddIsInitializedMethod(message_descriptor, cls):
12601261
protocol message class."""
12611262

12621263
required_fields = [field for field in message_descriptor.fields
1263-
if field.label == _FieldDescriptor.LABEL_REQUIRED]
1264+
if field.is_required]
12641265

12651266
def IsInitialized(self, errors=None):
12661267
"""Checks if all required fields of a message are set.

python/google/protobuf/internal/reflection_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,9 +3286,8 @@ def testPackedOptions(self):
32863286
proto.packed_int32.append(1)
32873287
proto.packed_double.append(3.0)
32883288
for field_descriptor, _ in proto.ListFields():
3289-
self.assertEqual(True, field_descriptor.is_packed)
3290-
self.assertEqual(descriptor.FieldDescriptor.LABEL_REPEATED,
3291-
field_descriptor.label)
3289+
self.assertTrue(field_descriptor.is_packed)
3290+
self.assertTrue(field_descriptor.is_repeated)
32923291

32933292

32943293
@testing_refleaks.TestCase

python/google/protobuf/internal/unknown_fields_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def CheckUnknownField(self, name, unknown_field_set, expected_value):
183183
continue
184184
if expected_type == wire_format.WIRETYPE_LENGTH_DELIMITED:
185185
self.assertIn(type(unknown_field.data), (str, bytes))
186-
if field_descriptor.label == descriptor.FieldDescriptor.LABEL_REPEATED:
186+
if field_descriptor.is_repeated:
187187
self.assertIn(unknown_field.data, expected_value)
188188
else:
189189
self.assertEqual(expected_value, unknown_field.data)
@@ -362,11 +362,11 @@ def CheckUnknownField(self, name, expected_value):
362362
for field in unknown_field_set:
363363
if field.field_number == field_descriptor.number:
364364
count += 1
365-
if field_descriptor.label == descriptor.FieldDescriptor.LABEL_REPEATED:
365+
if field_descriptor.is_repeated:
366366
self.assertIn(field.data, expected_value)
367367
else:
368368
self.assertEqual(expected_value, field.data)
369-
if field_descriptor.label == descriptor.FieldDescriptor.LABEL_REPEATED:
369+
if field_descriptor.is_repeated:
370370
self.assertEqual(count, len(expected_value))
371371
else:
372372
self.assertEqual(count, 1)

python/google/protobuf/json_format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def _RegularMessageToJsonObject(self, message, js):
236236
recorded_key = str(key)
237237
js_map[recorded_key] = self._FieldToJsonObject(v_field, value[key])
238238
js[name] = js_map
239-
elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
239+
elif field.is_repeated:
240240
# Convert a repeated field.
241241
js[name] = [self._FieldToJsonObject(field, k) for k in value]
242242
elif field.is_extension:
@@ -266,7 +266,7 @@ def _RegularMessageToJsonObject(self, message, js):
266266
continue
267267
if _IsMapEntry(field):
268268
js[name] = {}
269-
elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
269+
elif field.is_repeated:
270270
js[name] = []
271271
else:
272272
js[name] = self._FieldToJsonObject(field, field.default_value)
@@ -636,7 +636,7 @@ def _ConvertFieldValuePair(self, js, message, path):
636636
self._ConvertMapFieldValue(
637637
value, message, field, '{0}.{1}'.format(path, name)
638638
)
639-
elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
639+
elif field.is_repeated:
640640
message.ClearField(field.name)
641641
if not isinstance(value, _LIST_LIKE):
642642
raise ParseError(

0 commit comments

Comments
 (0)