Skip to content

Commit fd40c87

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Breaking change: Remove deprecated syntax accessor.
This has been replaced by edition and feature getters. Any code depending on syntax will likely be broken by editions files, and should be migrated to the finer-grained feature helpers. PiperOrigin-RevId: 589866745
1 parent a303ccb commit fd40c87

File tree

4 files changed

+22
-77
lines changed

4 files changed

+22
-77
lines changed

python/descriptor.c

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,6 @@ static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self,
648648
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
649649
}
650650

651-
static PyObject* PyUpb_Descriptor_GetSyntax(PyObject* self, void* closure) {
652-
PyErr_WarnEx(NULL,
653-
"descriptor.syntax is deprecated. It will be removed soon. "
654-
"Most usages are checking field descriptors. Consider to use "
655-
"has_presence, is_packed on field descriptors.",
656-
1);
657-
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
658-
const char* syntax =
659-
upb_MessageDef_Syntax(msgdef) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
660-
return PyUnicode_InternFromString(syntax);
661-
}
662-
663651
static PyGetSetDef PyUpb_Descriptor_Getters[] = {
664652
{"name", PyUpb_Descriptor_GetName, NULL, "Last name"},
665653
{"full_name", PyUpb_Descriptor_GetFullName, NULL, "Full name"},
@@ -694,13 +682,6 @@ static PyGetSetDef PyUpb_Descriptor_Getters[] = {
694682
"Containing type"},
695683
{"is_extendable", PyUpb_Descriptor_GetIsExtendable, NULL},
696684
{"has_options", PyUpb_Descriptor_GetHasOptions, NULL, "Has Options"},
697-
// begin:github_only
698-
{"syntax", &PyUpb_Descriptor_GetSyntax, NULL, "Syntax"},
699-
// end:github_only
700-
// begin:google_only
701-
// // TODO Use this to open-source syntax deprecation.
702-
// {"deprecated_syntax", &PyUpb_Descriptor_GetSyntax, NULL, "Syntax"},
703-
// end:google_only
704685
{NULL}};
705686

706687
static PyMethodDef PyUpb_Descriptor_Methods[] = {
@@ -1384,17 +1365,10 @@ static PyObject* PyUpb_FileDescriptor_GetPublicDependencies(PyObject* _self,
13841365
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
13851366
}
13861367

1387-
static PyObject* PyUpb_FileDescriptor_GetSyntax(PyObject* _self,
1388-
void* closure) {
1389-
PyErr_WarnEx(NULL,
1390-
"descriptor.syntax is deprecated. It will be removed soon. "
1391-
"Most usages are checking field descriptors. Consider to use "
1392-
"has_presence, is_packed on field descriptors.",
1393-
1);
1368+
static PyObject* PyUpb_FileDescriptor_GetEdition(PyObject* _self,
1369+
void* closure) {
13941370
PyUpb_DescriptorBase* self = (void*)_self;
1395-
const char* syntax =
1396-
upb_FileDef_Syntax(self->def) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
1397-
return PyUnicode_FromString(syntax);
1371+
return PyLong_FromLong(upb_FileDef_Edition(self->def));
13981372
}
13991373

14001374
static PyObject* PyUpb_FileDescriptor_GetHasOptions(PyObject* _self,
@@ -1445,14 +1419,7 @@ static PyGetSetDef PyUpb_FileDescriptor_Getters[] = {
14451419
{"public_dependencies", PyUpb_FileDescriptor_GetPublicDependencies, NULL,
14461420
"Dependencies"},
14471421
{"has_options", PyUpb_FileDescriptor_GetHasOptions, NULL, "Has Options"},
1448-
// begin:github_only
1449-
{"syntax", PyUpb_FileDescriptor_GetSyntax, (setter)NULL, "Syntax"},
1450-
// end:github_only
1451-
// begin:google_only
1452-
// // TODO Use this to open-source syntax deprecation.
1453-
// {"deprecated_syntax", PyUpb_FileDescriptor_GetSyntax, (setter)NULL,
1454-
// "Syntax"},
1455-
// end:google_only
1422+
{"edition", PyUpb_FileDescriptor_GetEdition, (setter)NULL, "Edition"},
14561423
{NULL},
14571424
};
14581425

python/google/protobuf/descriptor.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,24 +1179,23 @@ class FileDescriptor(DescriptorBase):
11791179
Attributes:
11801180
name (str): Name of file, relative to root of source tree.
11811181
package (str): Name of the package
1182-
syntax (str): string indicating syntax of the file (can be "proto2" or
1183-
"proto3")
1182+
edition (Edition): Enum value indicating edition of the file
11841183
serialized_pb (bytes): Byte string of serialized
11851184
:class:`descriptor_pb2.FileDescriptorProto`.
11861185
dependencies (list[FileDescriptor]): List of other :class:`FileDescriptor`
11871186
objects this :class:`FileDescriptor` depends on.
11881187
public_dependencies (list[FileDescriptor]): A subset of
11891188
:attr:`dependencies`, which were declared as "public".
1190-
message_types_by_name (dict(str, Descriptor)): Mapping from message names
1191-
to their :class:`Descriptor`.
1189+
message_types_by_name (dict(str, Descriptor)): Mapping from message names to
1190+
their :class:`Descriptor`.
11921191
enum_types_by_name (dict(str, EnumDescriptor)): Mapping from enum names to
11931192
their :class:`EnumDescriptor`.
11941193
extensions_by_name (dict(str, FieldDescriptor)): Mapping from extension
11951194
names declared at file scope to their :class:`FieldDescriptor`.
11961195
services_by_name (dict(str, ServiceDescriptor)): Mapping from services'
11971196
names to their :class:`ServiceDescriptor`.
1198-
pool (DescriptorPool): The pool this descriptor belongs to. When not
1199-
passed to the constructor, the global default pool is used.
1197+
pool (DescriptorPool): The pool this descriptor belongs to. When not passed
1198+
to the constructor, the global default pool is used.
12001199
"""
12011200

12021201
if _USE_C_DESCRIPTORS:
@@ -1260,7 +1259,6 @@ def __init__(
12601259
self.message_types_by_name = {}
12611260
self.name = name
12621261
self.package = package
1263-
self._deprecated_syntax = syntax or "proto2"
12641262
self.serialized_pb = serialized_pb
12651263

12661264
self.enum_types_by_name = {}
@@ -1269,15 +1267,6 @@ def __init__(
12691267
self.dependencies = (dependencies or [])
12701268
self.public_dependencies = (public_dependencies or [])
12711269

1272-
@property
1273-
def syntax(self):
1274-
warnings.warn(
1275-
'descriptor.syntax is deprecated. It will be removed'
1276-
' soon. Most usages are checking field descriptors. Consider to use'
1277-
' has_presence, is_packed on field descriptors.'
1278-
)
1279-
return self._deprecated_syntax
1280-
12811270
def CopyToProto(self, proto):
12821271
"""Copies this to a descriptor_pb2.FileDescriptorProto.
12831272
@@ -1290,6 +1279,13 @@ def CopyToProto(self, proto):
12901279
def _parent(self):
12911280
return None
12921281

1282+
@property
1283+
def edition(self):
1284+
# pylint: disable=g-import-not-at-top
1285+
from google.protobuf import descriptor_pb2
1286+
1287+
return descriptor_pb2.Edition.Value(self._edition)
1288+
12931289

12941290
def _ParseOptions(message, string):
12951291
"""Parses serialized options.

python/google/protobuf/internal/descriptor_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,9 @@ def testFileDescriptor(self):
539539
self.assertEqual(self.my_file.package, 'protobuf_unittest')
540540
self.assertEqual(self.my_file.pool, self.pool)
541541
self.assertFalse(self.my_file.has_options)
542-
self.assertEqual(self.my_file.syntax, 'proto2')
542+
self.assertEqual(
543+
self.my_file.edition, descriptor_pb2.Edition.EDITION_PROTO2
544+
)
543545
file_proto = descriptor_pb2.FileDescriptorProto()
544546
self.my_file.CopyToProto(file_proto)
545547
self.assertEqual(self.my_file.serialized_pb,

python/google/protobuf/pyext/descriptor.cc

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "absl/log/absl_check.h"
2323
#include "absl/strings/string_view.h"
2424
#include "google/protobuf/descriptor.h"
25-
#include "google/protobuf/descriptor_legacy.h"
2625
#include "google/protobuf/dynamic_message.h"
2726
#include "google/protobuf/io/coded_stream.h"
2827
#include "google/protobuf/pyext/descriptor_containers.h"
@@ -696,17 +695,6 @@ static PyObject* EnumValueName(PyBaseDescriptor *self, PyObject *args) {
696695
return PyString_FromCppString(enum_value->name());
697696
}
698697

699-
static PyObject* GetSyntax(PyBaseDescriptor *self, void *closure) {
700-
PyErr_WarnEx(nullptr,
701-
"descriptor.syntax is deprecated. It will be removed soon. "
702-
"Most usages are checking field descriptors. Consider to use "
703-
"has_presence, is_packed on field descriptors.",
704-
1);
705-
std::string syntax(FileDescriptorLegacy::SyntaxName(
706-
FileDescriptorLegacy(_GetDescriptor(self)->file()).syntax()));
707-
return PyUnicode_InternFromString(syntax.c_str());
708-
}
709-
710698
static PyGetSetDef Getters[] = {
711699
{"name", (getter)GetName, nullptr, "Last name"},
712700
{"full_name", (getter)GetFullName, nullptr, "Full name"},
@@ -743,7 +731,6 @@ static PyGetSetDef Getters[] = {
743731
{"_options", (getter) nullptr, (setter)SetOptions, "Options"},
744732
{"_serialized_options", (getter) nullptr, (setter)SetSerializedOptions,
745733
"Serialized Options"},
746-
{"syntax", (getter)GetSyntax, (setter) nullptr, "Syntax"},
747734
{nullptr},
748735
};
749736

@@ -1542,15 +1529,8 @@ static int SetSerializedOptions(PyFileDescriptor *self, PyObject *value,
15421529
return CheckCalledFromGeneratedFile("_serialized_options");
15431530
}
15441531

1545-
static PyObject* GetSyntax(PyFileDescriptor *self, void *closure) {
1546-
PyErr_WarnEx(nullptr,
1547-
"descriptor.syntax is deprecated. It will be removed soon. "
1548-
"Most usages are checking field descriptors. Consider to use "
1549-
"has_presence, is_packed on field descriptors.",
1550-
1);
1551-
std::string syntax(FileDescriptorLegacy::SyntaxName(
1552-
FileDescriptorLegacy(_GetDescriptor(self)).syntax()));
1553-
return PyUnicode_InternFromString(syntax.c_str());
1532+
static PyObject* GetEdition(PyFileDescriptor* self, void* closure) {
1533+
return PyLong_FromLong(_GetDescriptor(self)->edition());
15541534
}
15551535

15561536
static PyObject* CopyToProto(PyFileDescriptor *self, PyObject *target) {
@@ -1579,7 +1559,7 @@ static PyGetSetDef Getters[] = {
15791559
{"_options", (getter) nullptr, (setter)SetOptions, "Options"},
15801560
{"_serialized_options", (getter) nullptr, (setter)SetSerializedOptions,
15811561
"Serialized Options"},
1582-
{"syntax", (getter)GetSyntax, (setter) nullptr, "Syntax"},
1562+
{"edition", (getter)GetEdition, (setter) nullptr, "Edition"},
15831563
{nullptr},
15841564
};
15851565

0 commit comments

Comments
 (0)