Skip to content

Commit 74f5cf4

Browse files
committed
Raise warnings for python syntax usages
PiperOrigin-RevId: 579344748
1 parent edb1afd commit 74f5cf4

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

python/descriptor.c

+10
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,11 @@ static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self,
620620
}
621621

622622
static PyObject* PyUpb_Descriptor_GetSyntax(PyObject* self, void* closure) {
623+
PyErr_WarnEx(NULL,
624+
"descriptor.syntax is deprecated. It will be removed soon. "
625+
"Most usages are checking field descriptors. Consider to use "
626+
"has_presence, is_packed on field descriptors.",
627+
1);
623628
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
624629
const char* syntax =
625630
upb_MessageDef_Syntax(msgdef) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
@@ -1309,6 +1314,11 @@ static PyObject* PyUpb_FileDescriptor_GetPublicDependencies(PyObject* _self,
13091314

13101315
static PyObject* PyUpb_FileDescriptor_GetSyntax(PyObject* _self,
13111316
void* closure) {
1317+
PyErr_WarnEx(NULL,
1318+
"descriptor.syntax is deprecated. It will be removed soon. "
1319+
"Most usages are checking field descriptors. Consider to use "
1320+
"has_presence, is_packed on field descriptors.",
1321+
1);
13121322
PyUpb_DescriptorBase* self = (void*)_self;
13131323
const char* syntax =
13141324
upb_FileDef_Syntax(self->def) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";

python/google/protobuf/descriptor.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,18 @@ def __init__(self, name, full_name, filename, containing_type, fields,
353353
self.oneofs_by_name = dict((o.name, o) for o in self.oneofs)
354354
for oneof in self.oneofs:
355355
oneof.containing_type = self
356-
self.syntax = syntax or "proto2"
356+
self._deprecated_syntax = syntax or "proto2"
357357
self._is_map_entry = is_map_entry
358358

359+
@property
360+
def syntax(self):
361+
warnings.warn(
362+
'descriptor.syntax is deprecated. It will be removed'
363+
' soon. Most usages are checking field descriptors. Consider to use'
364+
' has_presence, is_packed on field descriptors.'
365+
)
366+
return self._deprecated_syntax
367+
359368
@property
360369
def fields_by_camelcase_name(self):
361370
"""Same FieldDescriptor objects as in :attr:`fields`, but indexed by
@@ -621,7 +630,7 @@ def has_presence(self):
621630
# compatibility. FieldDescriptor.file was added in cl/153110619
622631
# Some old/generated code didn't link file to FieldDescriptor.
623632
# TODO: remove syntax usage b/240619313
624-
return self.containing_type.syntax == 'proto2'
633+
return self.containing_type._deprecated_syntax == 'proto2'
625634

626635
@property
627636
def is_packed(self):
@@ -634,7 +643,7 @@ def is_packed(self):
634643
field_type == FieldDescriptor.TYPE_MESSAGE or
635644
field_type == FieldDescriptor.TYPE_BYTES):
636645
return False
637-
if self.containing_type.syntax == 'proto2':
646+
if self.containing_type._deprecated_syntax == 'proto2':
638647
return self.has_options and self.GetOptions().packed
639648
else:
640649
return (not self.has_options or
@@ -743,7 +752,7 @@ def is_closed(self):
743752
Care should be taken when using this function to respect the target
744753
runtime's enum handling quirks.
745754
"""
746-
return self.file.syntax == 'proto2'
755+
return self.file._deprecated_syntax == 'proto2'
747756

748757
def CopyToProto(self, proto):
749758
"""Copies this to a descriptor_pb2.EnumDescriptorProto.
@@ -1083,7 +1092,7 @@ def __init__(self, name, package, options=None,
10831092
self.message_types_by_name = {}
10841093
self.name = name
10851094
self.package = package
1086-
self.syntax = syntax or "proto2"
1095+
self._deprecated_syntax = syntax or "proto2"
10871096
self.serialized_pb = serialized_pb
10881097

10891098
self.enum_types_by_name = {}
@@ -1092,6 +1101,15 @@ def __init__(self, name, package, options=None,
10921101
self.dependencies = (dependencies or [])
10931102
self.public_dependencies = (public_dependencies or [])
10941103

1104+
@property
1105+
def syntax(self):
1106+
warnings.warn(
1107+
'descriptor.syntax is deprecated. It will be removed'
1108+
' soon. Most usages are checking field descriptors. Consider to use'
1109+
' has_presence, is_packed on field descriptors.'
1110+
)
1111+
return self._deprecated_syntax
1112+
10951113
def CopyToProto(self, proto):
10961114
"""Copies this to a descriptor_pb2.FileDescriptorProto.
10971115

python/google/protobuf/pyext/descriptor.cc

+10
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,11 @@ static PyObject* EnumValueName(PyBaseDescriptor *self, PyObject *args) {
672672
}
673673

674674
static PyObject* GetSyntax(PyBaseDescriptor *self, void *closure) {
675+
PyErr_WarnEx(nullptr,
676+
"descriptor.syntax is deprecated. It will be removed soon. "
677+
"Most usages are checking field descriptors. Consider to use "
678+
"has_presence, is_packed on field descriptors.",
679+
1);
675680
std::string syntax(FileDescriptorLegacy::SyntaxName(
676681
FileDescriptorLegacy(_GetDescriptor(self)->file()).syntax()));
677682
return PyUnicode_InternFromString(syntax.c_str());
@@ -1493,6 +1498,11 @@ static int SetSerializedOptions(PyFileDescriptor *self, PyObject *value,
14931498
}
14941499

14951500
static PyObject* GetSyntax(PyFileDescriptor *self, void *closure) {
1501+
PyErr_WarnEx(nullptr,
1502+
"descriptor.syntax is deprecated. It will be removed soon. "
1503+
"Most usages are checking field descriptors. Consider to use "
1504+
"has_presence, is_packed on field descriptors.",
1505+
1);
14961506
std::string syntax(FileDescriptorLegacy::SyntaxName(
14971507
FileDescriptorLegacy(_GetDescriptor(self)).syntax()));
14981508
return PyUnicode_InternFromString(syntax.c_str());

0 commit comments

Comments
 (0)