Source code for symforce.ops.impl.dataclass_group_ops
# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------importdataclassesfromsymforceimporttypingasTfromsymforceimporttyping_utilfromsymforce.opsimportGroupOpsfrom.dataclass_storage_opsimportDataclassStorageOps
[docs]@staticmethoddefidentity(a:T.DataclassOrType)->T.Dataclass:constructed_fields={}ifisinstance(a,type):type_hints_map=T.get_type_hints(a)forfieldindataclasses.fields(a):field_type=type_hints_map[field.name]iffield.metadata.get("length")isnotNone:sequence_instance=typing_util.get_sequence_from_dataclass_sequence_field(field,field_type)constructed_fields[field.name]=GroupOps.identity(sequence_instance)elif(sequence_types:=typing_util.maybe_tuples_of_types_from_annotation(field_type))isnotNone:# It's a Tuple of known sizeconstructed_fields[field.name]=GroupOps.identity(sequence_types)else:constructed_fields[field.name]=GroupOps.identity(field_type)returna(**constructed_fields)else:forfieldindataclasses.fields(a):constructed_fields[field.name]=GroupOps.identity(getattr(a,field.name))returntyping_util.get_type(a)(**constructed_fields)