From 899416423fc0e7f9a4d445da13816eacd806bde0 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 21 Sep 2026 19:26:21 -0700 Subject: [PATCH] Remove unused APIs --- README.md | 4 + capnp/includes/capnp_cpp.pxd | 6 - capnp/includes/schema_cpp.pxd | 1 - capnp/includes/types.pxd | 18 +-- capnp/lib/capnp.pxd | 12 +- capnp/lib/capnp.pyx | 213 +++++-------------------------- test/test_blob_to_dict_base64.py | 6 +- test/test_regression.py | 19 +-- test/test_struct.py | 31 +---- 9 files changed, 52 insertions(+), 258 deletions(-) diff --git a/README.md b/README.md index cdf388c..4227a95 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ packed serialization, file-descriptor I/O, segment APIs, borrowed Data views, custom allocators, orphans/resizable lists, AnyPointer wrappers, type registration, the Python schema import hook, and the Cython code generator. Their examples, tests, docs, dependencies, and unsupported-platform CI were removed too. +Also removed are generated per-schema `Reader`/`Builder` classes, synthetic +`.Union` enums, `_has_by_field`/`_init_by_field`, allocation-size overrides, +`from_bytes(builder=True)`, and `to_dict` ordering/base64 options. Incoming base64 +Data values in `from_dict` remain supported. `remove_import_hook()` remains a no-op for cereal/opendbc compatibility. This is intentionally not a full upstream API replacement. The import and diff --git a/capnp/includes/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd index 2d1da39..38c4f2c 100644 --- a/capnp/includes/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -146,9 +146,7 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp": DynamicValueForward.Reader get(char *) except +reraise_kj_exception DynamicValueForward.Reader getByField"get"(StructSchema.Field) except +reraise_kj_exception bint has(char *) except +reraise_kj_exception - bint hasByField"has"(StructSchema.Field) except +reraise_kj_exception StructSchema getSchema() - uint64_t getId"getSchema().getProto().getId"() Maybe[StructSchema.Field] which() MessageSize totalSize() @@ -159,15 +157,11 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp": DynamicValueForward.Builder get(char *) except +reraise_kj_exception DynamicValueForward.Builder getByField"get"(StructSchema.Field) except +reraise_kj_exception bint has(char *) except +reraise_kj_exception - bint hasByField"has"(StructSchema.Field) except +reraise_kj_exception void set(char *, DynamicValueForward.Reader) except +reraise_kj_exception void setByField"set"(StructSchema.Field, DynamicValueForward.Reader) except +reraise_kj_exception DynamicValueForward.Builder init(char *, uint size) except +reraise_kj_exception DynamicValueForward.Builder init(char *) except +reraise_kj_exception - DynamicValueForward.Builder initByField"init"(StructSchema.Field, uint size) except +reraise_kj_exception - DynamicValueForward.Builder initByField"init"(StructSchema.Field) except +reraise_kj_exception StructSchema getSchema() - uint64_t getId"getSchema().getProto().getId"() Maybe[StructSchema.Field] which() DynamicStruct.Reader asReader() MessageSize totalSize() diff --git a/capnp/includes/schema_cpp.pxd b/capnp/includes/schema_cpp.pxd index 7c2c185..e4eeadd 100644 --- a/capnp/includes/schema_cpp.pxd +++ b/capnp/includes/schema_cpp.pxd @@ -82,7 +82,6 @@ cdef extern from "capnp/message.h" namespace " ::capnp": cdef cppclass MallocMessageBuilder(MessageBuilder) nogil: MallocMessageBuilder() - MallocMessageBuilder(int) cdef extern from "capnp/serialize.h" namespace " ::capnp": cdef cppclass FlatArrayMessageReader(MessageReader) nogil: diff --git a/capnp/includes/types.pxd b/capnp/includes/types.pxd index ceec0b2..f198fe5 100644 --- a/capnp/includes/types.pxd +++ b/capnp/includes/types.pxd @@ -1,18 +1,4 @@ -from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF from libc.stdint cimport * -ctypedef unsigned int uint -ctypedef uint8_t byte -ctypedef uint8_t UInt8 -ctypedef uint16_t UInt16 -ctypedef uint32_t UInt32 -ctypedef uint64_t UInt64 -ctypedef int8_t Int8 -ctypedef int16_t Int16 -ctypedef int32_t Int32 -ctypedef int64_t Int64 - -ctypedef char * Object -ctypedef bint Bool -ctypedef float Float32 -ctypedef double Float64 from libcpp cimport bool as cbool + +ctypedef unsigned int uint diff --git a/capnp/lib/capnp.pxd b/capnp/lib/capnp.pxd index 9d161b5..51c1812 100644 --- a/capnp/lib/capnp.pxd +++ b/capnp/lib/capnp.pxd @@ -5,7 +5,7 @@ from capnp.includes cimport schema_cpp from capnp.includes.capnp_cpp cimport ( Schema as C_Schema, StructSchema as C_StructSchema, EnumSchema as C_EnumSchema, ListSchema as C_ListSchema, DynamicStruct as C_DynamicStruct, - DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, + DynamicValue as C_DynamicValue, DynamicList as C_DynamicList, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, String, StringTree, DynamicStruct_Builder ) @@ -47,8 +47,7 @@ cdef class _DynamicStructReader: cpdef _DynamicEnumField _which(self) cpdef _which_str(self) cpdef _get_by_field(self, _StructSchemaField field) - cpdef _has_by_field(self, _StructSchemaField field) - cpdef as_builder(self, num_first_segment_words=?) + cpdef as_builder(self) cdef class _DynamicStructBuilder: @@ -68,12 +67,10 @@ cdef class _DynamicStructBuilder: cpdef init(self, field, size=?) cpdef _get_by_field(self, _StructSchemaField field) cpdef _set_by_field(self, _StructSchemaField field, value) - cpdef _has_by_field(self, _StructSchemaField field) - cpdef _init_by_field(self, _StructSchemaField field, size=?) cpdef _DynamicEnumField _which(self) cpdef _which_str(self) cpdef as_reader(self) - cpdef copy(self, num_first_segment_words=?) + cpdef copy(self) cdef class _DynamicEnumField: cdef object thisptr @@ -115,10 +112,9 @@ cdef class _MessageBuilder: cpdef set_root(self, value) cdef to_python_reader(C_DynamicValue.Reader self, object parent) cdef to_python_builder(C_DynamicValue.Builder self, object parent) -cdef _to_dict(msg, bint verbose, bint ordered, bint encode_bytes_as_base64=?) +cdef _to_dict(msg, bint verbose) cdef _from_list(_DynamicListBuilder msg, list d) cdef _from_tuple(_DynamicListBuilder msg, tuple d) cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField field, value, parent) -cdef _setDynamicFieldStatic(DynamicStruct_Builder thisptr, field, value, parent) cdef api object wrap_kj_exception_for_reraise(capnp.Exception & exception) with gil diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 5519392..1aae53e 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -21,7 +21,6 @@ from libc.string cimport memcpy from libcpp.utility cimport move -import collections as _collections import contextlib import base64 import enum as _enum @@ -164,11 +163,6 @@ cdef schema_cpp.ReaderOptions make_reader_opts(traversal_limit_in_words, nesting return opts -ctypedef fused _DynamicStructReaderOrBuilder: - _DynamicStructReader - _DynamicStructBuilder - - ctypedef fused _DynamicSetterClasses: C_DynamicList.Builder DynamicStruct_Builder @@ -627,119 +621,56 @@ cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField # TODO: Is this function used by anyone? Can it be removed? -cdef _setDynamicFieldStatic(DynamicStruct_Builder thisptr, field, value, parent): - cdef C_DynamicValue.Reader temp - value_type = type(value) - - if value_type is int or value_type is long: - if value < 0: - temp = C_DynamicValue.Reader(value) - else: - temp = C_DynamicValue.Reader(value) - thisptr.set(field, temp) - elif value_type is float: - temp = C_DynamicValue.Reader(value) - thisptr.set(field, temp) - elif value_type is bool: - temp = C_DynamicValue.Reader(value) - thisptr.set(field, temp) - elif value_type is bytes: - _setBytes(thisptr, field, value) - elif isinstance(value, basestring): - _setBaseString(thisptr, field, value) - elif value_type is list: - ptr = thisptr.init(field, len(value)) - builder = to_python_builder(ptr, parent) - _from_list(builder, value) - elif value_type is dict: - ptr = thisptr.get(field) - builder = to_python_builder(ptr, parent) - builder.from_dict(value) - elif value is None: - temp = C_DynamicValue.Reader(VOID) - thisptr.set(field, temp) - elif value_type is _DynamicStructBuilder: - thisptr.set(field, _extract_dynamic_struct_builder(value)) - elif value_type is _DynamicStructReader: - thisptr.set(field, _extract_dynamic_struct_reader(value)) - elif value_type is _DynamicListBuilder: - thisptr.set(field, _extract_dynamic_list_builder(value)) - elif value_type is _DynamicListReader: - thisptr.set(field, _extract_dynamic_list_reader(value)) - elif value_type is _DynamicEnum: - thisptr.set(field, _extract_dynamic_enum(value)) - else: - raise KjException( - "Tried to set field: '{}' with a value of: '{}' which is an unsupported type: '{}'" - .format(field, str(value), str(type(value)))) - - cdef _DynamicListBuilder temp_list_b cdef _DynamicListReader temp_list_r cdef _DynamicStructBuilder temp_msg_b cdef _DynamicStructReader temp_msg_r -cdef _to_dict(msg, bint verbose, bint ordered, bint encode_bytes_as_base64=False): +cdef _to_dict(msg, bint verbose): msg_type = type(msg) if msg_type is _DynamicListBuilder: temp_list_b = msg - return [_to_dict(temp_list_b._get(i), verbose, ordered, encode_bytes_as_base64) for i in range(len(msg))] + return [_to_dict(temp_list_b._get(i), verbose) for i in range(len(msg))] elif msg_type is _DynamicListReader: temp_list_r = msg - return [_to_dict(temp_list_r._get(i), verbose, ordered, encode_bytes_as_base64) for i in range(len(msg))] + return [_to_dict(temp_list_r._get(i), verbose) for i in range(len(msg))] if msg_type is _DynamicStructBuilder: temp_msg_b = msg - if ordered: - ret = _collections.OrderedDict() - else: - ret = {} + ret = {} try: which = temp_msg_b.which() - ret[which] = _to_dict(temp_msg_b._get(which), verbose, ordered, encode_bytes_as_base64) + ret[which] = _to_dict(temp_msg_b._get(which), verbose) except KjException: pass for field in temp_msg_b.schema.non_union_fields: if verbose or temp_msg_b._has(field): - ret[field] = _to_dict(temp_msg_b._get(field), verbose, ordered, encode_bytes_as_base64) + ret[field] = _to_dict(temp_msg_b._get(field), verbose) return ret elif msg_type is _DynamicStructReader: temp_msg_r = msg - if ordered: - ret = _collections.OrderedDict() - else: - ret = {} + ret = {} try: which = temp_msg_r.which() - ret[which] = _to_dict(temp_msg_r._get(which), verbose, ordered, encode_bytes_as_base64) + ret[which] = _to_dict(temp_msg_r._get(which), verbose) except KjException: pass for field in temp_msg_r.schema.non_union_fields: if verbose or temp_msg_r._has(field): - ret[field] = _to_dict(temp_msg_r._get(field), verbose, ordered, encode_bytes_as_base64) + ret[field] = _to_dict(temp_msg_r._get(field), verbose) return ret if isinstance(msg, (_DynamicStructBuilder, _DynamicStructReader)): - return msg.to_dict(verbose, ordered) + return msg.to_dict(verbose) if msg_type is _DynamicEnum: return str(msg) - if encode_bytes_as_base64 and msg_type is bytes: - # encode the message as base64 and return utf-8 string - return base64.b64encode(msg).decode('utf-8') - - if msg_type is memoryview: - if encode_bytes_as_base64: - return base64.b64encode(bytes(msg)).decode('utf-8') - else: - return bytes(msg) - return msg @@ -890,9 +821,6 @@ cdef class _DynamicStructReader: cpdef _has(self, field): return self.thisptr.has(field) - cpdef _has_by_field(self, _StructSchemaField field): - return self.thisptr.hasByField(field.thisptr) - cpdef _which_str(self): try: return helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() @@ -946,21 +874,18 @@ cdef class _DynamicStructReader: def __repr__(self): return '<%s reader %s>' % (self.schema.node.displayName, strStructReader(self.thisptr).cStr()) - def to_dict(self, verbose=False, ordered=False, encode_bytes_as_base64=False): - return _to_dict(self, verbose, ordered, encode_bytes_as_base64) + def to_dict(self, verbose=False): + return _to_dict(self, verbose) - cpdef as_builder(self, num_first_segment_words=None): + cpdef as_builder(self): """A method for casting this Reader to a Builder This is a copying operation with respect to the message's buffer. Changes in the new builder will not reflect in the original reader. - :type num_first_segment_words: int - :param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments) - :rtype: :class:`_DynamicStructBuilder` """ - builder = _MallocMessageBuilder(num_first_segment_words) + builder = _MallocMessageBuilder() return builder.set_root(self) property total_size: @@ -1055,9 +980,6 @@ cdef class _DynamicStructBuilder: cpdef _has(self, field): return self.thisptr.has(field) - cpdef _has_by_field(self, _StructSchemaField field): - return self.thisptr.hasByField(field.thisptr) - cpdef init(self, field, size=None): """Method for initializing fields that are of type union/struct/list @@ -1082,28 +1004,6 @@ cdef class _DynamicStructBuilder: ptr = self.thisptr.init(field, size) return to_python_builder(ptr, self._parent) - cpdef _init_by_field(self, _StructSchemaField field, size=None): - """Method for initializing fields that are of type union/struct/list - - Typically, you don't have to worry about initializing structs/unions, so this method is mainly for lists. - - :type field: str - :param field: The field name to initialize - - :type size: int - :param size: The size of the list to initiialize. This should be None for struct/union initialization. - - :rtype: :class:`_DynamicStructBuilder` or :class:`_DynamicListBuilder` - - :Raises: :exc:`KjException` if the field isn't in this struct - """ - if size is None: - ptr = self.thisptr.initByField(field.thisptr) - return to_python_builder(ptr, self._parent) - else: - ptr = self.thisptr.initByField(field.thisptr, size) - return to_python_builder(ptr, self._parent) - cpdef _which_str(self): try: return helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() @@ -1155,18 +1055,15 @@ cdef class _DynamicStructBuilder: reader._obj_to_pin = self return reader - cpdef copy(self, num_first_segment_words=None): + cpdef copy(self): """A method for copying this Builder This is a copying operation with respect to the message's buffer. Changes in the new builder will not reflect in the original reader. - :type num_first_segment_words: int - :param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments) - :rtype: :class:`_DynamicStructBuilder` """ - builder = _MallocMessageBuilder(num_first_segment_words) + builder = _MallocMessageBuilder() return builder.set_root(self) property schema: @@ -1185,8 +1082,8 @@ cdef class _DynamicStructBuilder: def __repr__(self): return '<%s builder %s>' % (self.schema.node.displayName, strStructBuilder(self.thisptr).cStr()) - def to_dict(self, verbose=False, ordered=False, encode_bytes_as_base64=False): - return _to_dict(self, verbose, ordered, encode_bytes_as_base64) + def to_dict(self, verbose=False): + return _to_dict(self, verbose) def from_dict(self, dict d): for key, val in d.iteritems(): @@ -1415,15 +1312,9 @@ cdef class _ParsedSchema(_Schema): return _ParsedSchema()._init_child(self.thisptr_child.getNested(name)) -class _StructABCMeta(type): - """A metaclass for the Type.Reader and Type.Builder ABCs.""" - def __instancecheck__(cls, obj): - return isinstance(obj, cls.__base__) and obj.schema == cls._schema - - -cdef _new_message(self, kwargs, num_first_segment_words): +cdef _new_message(self, kwargs): cdef _MessageBuilder builder - builder = _MallocMessageBuilder(num_first_segment_words) + builder = _MallocMessageBuilder() msg = builder.init_root(self.schema) if kwargs is not None: msg.from_dict(kwargs) @@ -1458,14 +1349,6 @@ class _StructModule(object): sub_module = _StructModuleWhich("StructModuleWhich", mapping) setattr(sub_module, 'schema', raw_schema) setattr(self, name, sub_module) - if schema.union_fields and not schema.non_union_fields: - mapping = [] - for union_field in schema.node.struct.fields: - name = union_field.name - name = name[0].upper() + name[1:] - mapping.append((name, union_field.discriminantValue)) - sub_module = _StructModuleWhich("StructModuleWhich", mapping) - setattr(self, 'Union', sub_module) def read_multiple_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None): """Returns an iterable, that when traversed will return Readers for messages. @@ -1485,7 +1368,7 @@ class _StructModule(object): return reader @contextlib.contextmanager - def from_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None, builder=False): + def from_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None): """Returns a Reader for the unpacked object in buf. :type buf: buffer @@ -1498,34 +1381,22 @@ class _StructModule(object): :type nesting_limit: int :param nesting_limit: Limits how many total words of data are allowed to be traversed. Default is 64. - :type builder: bool - :param builder: If true, return a builder object. - - Enabling `builder` returns a writable copy of the message. - - :rtype: :class:`_DynamicStructReader` or :class:`_DynamicStructBuilder` + :rtype: :class:`_DynamicStructReader` """ message = None try: - if builder: - message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit) - yield message.get_root(self.schema).as_builder() - else: - message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit) - yield message.get_root(self.schema) + message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit) + yield message.get_root(self.schema) finally: if message: message.close() - def __call__(self, num_first_segment_words=None, **kwargs): - return self.new_message(num_first_segment_words=num_first_segment_words, **kwargs) + def __call__(self, **kwargs): + return self.new_message(**kwargs) - def new_message(self, num_first_segment_words=None, **kwargs): + def new_message(self, **kwargs): """Returns a newly allocated builder message. - :type num_first_segment_words: int - :param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments) - :type kwargs: dict :param kwargs: A list of fields and their values to initialize in the struct. @@ -1534,7 +1405,7 @@ class _StructModule(object): :rtype: :class:`_DynamicStructBuilder` """ - return _new_message(self, kwargs, num_first_segment_words) + return _new_message(self, kwargs) class _EnumModule(object): @@ -1641,27 +1512,6 @@ cdef class SchemaParser: if proto.isStruct: local_module = _StructModule(schema.as_struct(), node.name) - class Reader(_DynamicStructReader): - """An abstract base class. Readers are 'instances' of this class.""" - __metaclass__ = _StructABCMeta - __slots__ = [] - _schema = local_module.schema - - def __new__(self): - raise TypeError('This is an abstract base class') - - class Builder(_DynamicStructBuilder): - """An abstract base class. Builders are 'instances' of this class.""" - __metaclass__ = _StructABCMeta - __slots__ = [] - _schema = local_module.schema - - def __new__(self): - raise TypeError('This is an abstract base class') - - local_module.Reader = Reader - local_module.Builder = Builder - module.__dict__[node.name] = local_module elif proto.isConst: module.__dict__[node.name] = schema.as_const_value() @@ -1794,11 +1644,8 @@ cdef class _MallocMessageBuilder(_MessageBuilder): ... data = person.to_bytes() """ - def __init__(self, size=None): - if size is None: - self.thisptr = new schema_cpp.MallocMessageBuilder() - else: - self.thisptr = new schema_cpp.MallocMessageBuilder(size) + def __init__(self): + self.thisptr = new schema_cpp.MallocMessageBuilder() cdef class _MessageReader: diff --git a/test/test_blob_to_dict_base64.py b/test/test_blob_to_dict_base64.py index 5e70f7e..1e5b76a 100644 --- a/test/test_blob_to_dict_base64.py +++ b/test/test_blob_to_dict_base64.py @@ -14,8 +14,8 @@ def blob_schema(): def test_blob_to_dict(blob_schema): blob_value = b"hello world" blob = blob_schema.BlobTest(blob=blob_value) - blob_dict = blob.to_dict(encode_bytes_as_base64=True) - assert base64.b64decode(blob_dict["blob"]) == blob_value + assert blob.to_dict()["blob"] == blob_value + blob_dict = {"blob": base64.b64encode(blob_value).decode("ascii")} msg = blob_schema.BlobTest.new_message() msg.from_dict(blob_dict) - assert blob.blob == blob_value + assert msg.blob == blob_value diff --git a/test/test_regression.py b/test/test_regression.py index 5029bf0..0b56fa4 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -143,16 +143,15 @@ def test_addressbook(addressbook): def test_addressbook_explicit_fields(addressbook): def writeAddressBook(file): addresses = addressbook.AddressBook.new_message() - address_fields = addressbook.AddressBook.schema.fields person_fields = addressbook.Person.schema.fields phone_fields = addressbook.Person.PhoneNumber.schema.fields - people = addresses._init_by_field(address_fields["people"], 2) + people = addresses.init("people", 2) alice = people[0] alice._set_by_field(person_fields["id"], 123) alice._set_by_field(person_fields["name"], "Alice") alice._set_by_field(person_fields["email"], "alice@example.com") - alicePhones = alice._init_by_field(person_fields["phones"], 1) + alicePhones = alice.init("phones", 1) alicePhones[0]._set_by_field(phone_fields["number"], "555-1212") alicePhones[0]._set_by_field(phone_fields["type"], "mobile") employment = alice._get_by_field(person_fields["employment"]) @@ -162,7 +161,7 @@ def test_addressbook_explicit_fields(addressbook): bob._set_by_field(person_fields["id"], 456) bob._set_by_field(person_fields["name"], "Bob") bob._set_by_field(person_fields["email"], "bob@example.com") - bobPhones = bob._init_by_field(person_fields["phones"], 2) + bobPhones = bob.init("phones", 2) bobPhones[0]._set_by_field(phone_fields["number"], "555-4567") bobPhones[0]._set_by_field(phone_fields["type"], "home") bobPhones[1]._set_by_field(phone_fields["number"], "555-7654") @@ -443,18 +442,6 @@ def test_build(all_types): assert str(root) + "\n" == expectedText -def test_build_first_segment_size(all_types): - root = all_types.TestAllTypes.new_message(1) - init_all_types(root) - expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read() - assert str(root) + "\n" == expectedText - - root = all_types.TestAllTypes.new_message(1024 * 1024) - init_all_types(root) - expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read() - assert str(root) + "\n" == expectedText - - def test_binary_read(all_types): f = open(os.path.join(this_dir, "all-types.binary"), "rb") with all_types.TestAllTypes.from_bytes(f.read()) as reader: diff --git a/test/test_struct.py b/test/test_struct.py index 17db1da..eae1bf2 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -206,30 +206,24 @@ def test_set_dict_union(addressbook): def test_union_enum(all_types): - assert all_types.UnionAllTypes.Union.UnionStructField1 == 0 - assert all_types.UnionAllTypes.Union.UnionStructField2 == 1 msg = all_types.UnionAllTypes.new_message(**{"unionStructField1": {"textField": "foo"}}) - assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1 assert msg.which == "unionStructField1" assert msg.which == 0 msg = all_types.UnionAllTypes.new_message(**{"unionStructField2": {"textField": "foo"}}) - assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2 assert msg.which == "unionStructField2" assert msg.which == 1 - assert all_types.GroupedUnionAllTypes.Union.G1 == 0 - assert all_types.GroupedUnionAllTypes.Union.G2 == 1 - msg = all_types.GroupedUnionAllTypes.new_message(**{"g1": {"unionStructField1": {"textField": "foo"}}}) - assert msg.which == all_types.GroupedUnionAllTypes.Union.G1 + assert msg.which() == "g1" msg = all_types.GroupedUnionAllTypes.new_message(**{"g2": {"unionStructField2": {"textField": "foo"}}}) - assert msg.which == all_types.GroupedUnionAllTypes.Union.G2 + assert msg.which() == "g2" msg = all_types.UnionAllTypes.new_message() - msg.unionStructField2 = msg.init(all_types.UnionAllTypes.Union.UnionStructField2) + msg.init("unionStructField2") + assert msg.which() == "unionStructField2" def isstr(s): @@ -258,14 +252,11 @@ def test_to_dict_verbose(addressbook): assert person.to_dict(verbose=True)["phones"] == [] - if sys.version_info >= (2, 7): - assert person.to_dict(verbose=True, ordered=True)["phones"] == [] - with pytest.raises(KeyError): assert person.to_dict()["phones"] == [] -def test_to_dict_ordered(addressbook): +def test_to_dict_field_order(addressbook): person = addressbook.Person.new_message( **{ "name": "Alice", @@ -276,17 +267,7 @@ def test_to_dict_ordered(addressbook): } ) - if sys.version_info >= (2, 7): - assert list(person.to_dict(ordered=True).keys()) == [ - "id", - "name", - "email", - "phones", - "employment", - ] - else: - with pytest.raises(Exception): - person.to_dict(ordered=True) + assert list(person.to_dict().keys()) == ["id", "name", "email", "phones", "employment"] def test_nested_list(addressbook):