Skip to content

Commit 5043c20

Browse files
authored
sdk/basyx/aas: Fix type issues under mypy 1.16 (#399)
Previously, the release of `mypy 1.16` added new features for the type check, resulting in our pipeline to fail. This refactors the codebase to comply with `mypy 1.16`. Instances where `# type: ignore` was necessary are documented with context. Fixes #390
1 parent e043960 commit 5043c20

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

sdk/basyx/aas/adapter/xml/xml_serialization.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -875,83 +875,79 @@ def object_to_xml_element(obj: object) -> etree._Element:
875875
876876
:param obj: The object to serialize
877877
"""
878-
serialization_func: Callable[..., etree._Element]
879-
880878
if isinstance(obj, model.Key):
881-
serialization_func = key_to_xml
882-
elif isinstance(obj, model.Reference):
883-
serialization_func = reference_to_xml
879+
return key_to_xml(obj)
884880
elif isinstance(obj, model.Reference):
885-
serialization_func = reference_to_xml
881+
return reference_to_xml(obj)
886882
elif isinstance(obj, model.AdministrativeInformation):
887-
serialization_func = administrative_information_to_xml
883+
return administrative_information_to_xml(obj)
888884
elif isinstance(obj, model.Qualifier):
889-
serialization_func = qualifier_to_xml
885+
return qualifier_to_xml(obj)
890886
elif isinstance(obj, model.AnnotatedRelationshipElement):
891-
serialization_func = annotated_relationship_element_to_xml
887+
return annotated_relationship_element_to_xml(obj)
892888
elif isinstance(obj, model.BasicEventElement):
893-
serialization_func = basic_event_element_to_xml
889+
return basic_event_element_to_xml(obj)
894890
elif isinstance(obj, model.Blob):
895-
serialization_func = blob_to_xml
891+
return blob_to_xml(obj)
896892
elif isinstance(obj, model.Capability):
897-
serialization_func = capability_to_xml
893+
return capability_to_xml(obj)
898894
elif isinstance(obj, model.Entity):
899-
serialization_func = entity_to_xml
895+
return entity_to_xml(obj)
900896
elif isinstance(obj, model.Extension):
901-
serialization_func = extension_to_xml
897+
return extension_to_xml(obj)
902898
elif isinstance(obj, model.File):
903-
serialization_func = file_to_xml
899+
return file_to_xml(obj)
904900
elif isinstance(obj, model.Resource):
905-
serialization_func = resource_to_xml
901+
return resource_to_xml(obj)
906902
elif isinstance(obj, model.MultiLanguageProperty):
907-
serialization_func = multi_language_property_to_xml
903+
return multi_language_property_to_xml(obj)
908904
elif isinstance(obj, model.Operation):
909-
serialization_func = operation_to_xml
905+
return operation_to_xml(obj)
910906
elif isinstance(obj, model.Property):
911-
serialization_func = property_to_xml
907+
return property_to_xml(obj)
912908
elif isinstance(obj, model.Range):
913-
serialization_func = range_to_xml
909+
return range_to_xml(obj)
914910
elif isinstance(obj, model.ReferenceElement):
915-
serialization_func = reference_element_to_xml
911+
return reference_element_to_xml(obj)
916912
elif isinstance(obj, model.RelationshipElement):
917-
serialization_func = relationship_element_to_xml
913+
return relationship_element_to_xml(obj)
918914
elif isinstance(obj, model.SubmodelElementCollection):
919-
serialization_func = submodel_element_collection_to_xml
915+
return submodel_element_collection_to_xml(obj)
920916
elif isinstance(obj, model.SubmodelElementList):
921-
serialization_func = submodel_element_list_to_xml
917+
return submodel_element_list_to_xml(obj)
922918
elif isinstance(obj, model.AssetAdministrationShell):
923-
serialization_func = asset_administration_shell_to_xml
919+
return asset_administration_shell_to_xml(obj)
924920
elif isinstance(obj, model.AssetInformation):
925-
serialization_func = asset_information_to_xml
921+
return asset_information_to_xml(obj)
926922
elif isinstance(obj, model.SpecificAssetId):
927-
serialization_func = specific_asset_id_to_xml
923+
return specific_asset_id_to_xml(obj)
928924
elif isinstance(obj, model.Submodel):
929-
serialization_func = submodel_to_xml
925+
return submodel_to_xml(obj)
930926
elif isinstance(obj, model.ValueReferencePair):
931-
serialization_func = value_reference_pair_to_xml
927+
return value_reference_pair_to_xml(obj)
932928
elif isinstance(obj, model.ConceptDescription):
933-
serialization_func = concept_description_to_xml
929+
return concept_description_to_xml(obj)
934930
elif isinstance(obj, model.LangStringSet):
935-
serialization_func = lang_string_set_to_xml
931+
# FIXME: `lang_string_set_to_xml` expects `tag` parameter, `tag` doesn't have default value
932+
# Issue: https://github.com/eclipse-basyx/basyx-python-sdk/issues/397
933+
return lang_string_set_to_xml(obj) # type: ignore[call-arg]
936934
elif isinstance(obj, model.EmbeddedDataSpecification):
937-
serialization_func = embedded_data_specification_to_xml
935+
return embedded_data_specification_to_xml(obj)
938936
elif isinstance(obj, model.DataSpecificationIEC61360):
939-
serialization_func = data_specification_iec61360_to_xml
937+
return data_specification_iec61360_to_xml(obj)
940938
# generic serialization using the functions for abstract classes
941939
elif isinstance(obj, model.DataElement):
942-
serialization_func = data_element_to_xml
940+
return data_element_to_xml(obj)
943941
elif isinstance(obj, model.SubmodelElement):
944-
serialization_func = submodel_to_xml
942+
return submodel_element_to_xml(obj)
945943
elif isinstance(obj, model.DataSpecificationContent):
946-
serialization_func = data_specification_content_to_xml
944+
return data_specification_content_to_xml(obj)
947945
# type aliases
948946
elif isinstance(obj, model.ValueList):
949-
serialization_func = value_list_to_xml
947+
return value_list_to_xml(obj)
950948
else:
951949
raise ValueError(f"{obj!r} cannot be serialized!")
952950

953-
return serialization_func(obj)
954-
955951

956952
def write_aas_xml_element(file: _generic.PathOrBinaryIO, obj: object, **kwargs) -> None:
957953
"""

sdk/basyx/aas/model/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ def __init__(self,
15041504
self.value = value
15051505
self.refers_to: Set[ModelReference] = set(refers_to)
15061506
self.semantic_id: Optional[Reference] = semantic_id
1507-
self.supplemental_semantic_id: ConstrainedList[Reference] = ConstrainedList(supplemental_semantic_id)
1507+
self.supplemental_semantic_id = ConstrainedList(supplemental_semantic_id)
15081508

15091509
def __repr__(self) -> str:
15101510
return "Extension(name={})".format(self.name)
@@ -1653,7 +1653,7 @@ def __init__(self,
16531653
self.value_id: Optional[Reference] = value_id
16541654
self.kind: QualifierKind = kind
16551655
self.semantic_id: Optional[Reference] = semantic_id
1656-
self.supplemental_semantic_id: ConstrainedList[Reference] = ConstrainedList(supplemental_semantic_id)
1656+
self.supplemental_semantic_id = ConstrainedList(supplemental_semantic_id)
16571657

16581658
def __repr__(self) -> str:
16591659
return "Qualifier(type={})".format(self.type)

sdk/basyx/aas/model/submodel.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2024 the Eclipse BaSyx Authors
1+
# Copyright (c) 2025 the Eclipse BaSyx Authors
22
#
33
# This program and the accompanying materials are made available under the terms of the MIT License, available in
44
# the LICENSE file of this project.
@@ -76,8 +76,7 @@ def __init__(self,
7676
self.semantic_id: Optional[base.Reference] = semantic_id
7777
self.qualifier = base.NamespaceSet(self, [("type", True)], qualifier)
7878
self.extension = base.NamespaceSet(self, [("name", True)], extension)
79-
self.supplemental_semantic_id: base.ConstrainedList[base.Reference] = \
80-
base.ConstrainedList(supplemental_semantic_id)
79+
self.supplemental_semantic_id = base.ConstrainedList(supplemental_semantic_id)
8180
self.embedded_data_specifications: List[base.EmbeddedDataSpecification] = list(embedded_data_specifications)
8281

8382

@@ -147,8 +146,7 @@ def __init__(self,
147146
self.qualifier = base.NamespaceSet(self, [("type", True)], qualifier)
148147
self._kind: base.ModellingKind = kind
149148
self.extension = base.NamespaceSet(self, [("name", True)], extension)
150-
self.supplemental_semantic_id: base.ConstrainedList[base.Reference] = \
151-
base.ConstrainedList(supplemental_semantic_id)
149+
self.supplemental_semantic_id = base.ConstrainedList(supplemental_semantic_id)
152150
self.embedded_data_specifications: List[base.EmbeddedDataSpecification] = list(embedded_data_specifications)
153151

154152

0 commit comments

Comments
 (0)