From d08756d1b449e215b0e377e31719f7a7d2139bd3 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Tue, 8 Jul 2025 12:55:49 +0500 Subject: [PATCH] typing --- src/bonsai/bonsai/bim/helper.py | 3 +- .../bonsai/bim/module/group/operator.py | 2 +- src/bonsai/bonsai/bim/module/group/ui.py | 2 +- src/bonsai/bonsai/bim/module/pset/prop.py | 56 ++++++++++++++----- src/bonsai/bonsai/tool/pset.py | 4 +- .../api/geometry/add_representation.py | 1 + .../ifcopenshell/entity_instance.py | 2 + .../ifcopenshell/ifcopenshell_wrapper.pyi | 22 +++++--- .../ifcopenshell/util/attribute.py | 29 ++++++++-- 9 files changed, 87 insertions(+), 34 deletions(-) diff --git a/src/bonsai/bonsai/bim/helper.py b/src/bonsai/bonsai/bim/helper.py index b19dfa18e2..32ee842ae3 100644 --- a/src/bonsai/bonsai/bim/helper.py +++ b/src/bonsai/bonsai/bim/helper.py @@ -170,7 +170,8 @@ def import_attribute( if isinstance(data_type, tuple) or data_type == "entity": callback(attribute.name(), None, data) if callback else None return - new: bonsai.bim.prop.Attribute = props.add() + + new = props.add() new.name = attribute.name() new.is_null = data[attribute.name()] is None new.is_optional = attribute.optional() diff --git a/src/bonsai/bonsai/bim/module/group/operator.py b/src/bonsai/bonsai/bim/module/group/operator.py index 76271253ec..13af368e58 100644 --- a/src/bonsai/bonsai/bim/module/group/operator.py +++ b/src/bonsai/bonsai/bim/module/group/operator.py @@ -47,7 +47,7 @@ class LoadGroups(bpy.types.Operator, tool.Ifc.Operator): bpy.ops.bim.disable_editing_group() return {"FINISHED"} - def load_group(self, group, tree_depth=0): + def load_group(self, group: ifcopenshell.entity_instance, tree_depth: int = 0) -> None: new = self.props.groups.add() new.ifc_definition_id = group.id() new.name = group.Name or "Unnamed" diff --git a/src/bonsai/bonsai/bim/module/group/ui.py b/src/bonsai/bonsai/bim/module/group/ui.py index e433549f34..c94c699c90 100644 --- a/src/bonsai/bonsai/bim/module/group/ui.py +++ b/src/bonsai/bonsai/bim/module/group/ui.py @@ -129,7 +129,7 @@ class BIM_UL_groups(UIList): active_data, active_propname, index: int, - ): + ) -> None: if item: row = layout.row(align=True) for i in range(0, item.tree_depth): diff --git a/src/bonsai/bonsai/bim/module/pset/prop.py b/src/bonsai/bonsai/bim/module/pset/prop.py index bce071b85e..d3a0c78d6e 100644 --- a/src/bonsai/bonsai/bim/module/pset/prop.py +++ b/src/bonsai/bonsai/bim/module/pset/prop.py @@ -103,19 +103,19 @@ def get_pset_name(self: "PsetProperties", context: bpy.types.Context) -> tool.Bl return items -def get_object_pset_name(self, context): +def get_object_pset_name(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: if not ObjectPsetsData.is_loaded: ObjectPsetsData.load() return ObjectPsetsData.data["pset_name"] -def get_material_pset_names(self, context): +def get_material_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: if not MaterialPsetsData.is_loaded: MaterialPsetsData.load() return MaterialPsetsData.data["pset_name"] -def get_material_set_pset_names(self, context): +def get_material_set_pset_names(self: "PsetProperties", context: object): global psetnames if not ObjectMaterialData.is_loaded: ObjectMaterialData.load() @@ -128,7 +128,7 @@ def get_material_set_pset_names(self, context): return psetnames[ifc_class] -def get_material_set_item_pset_names(self, context) -> list[tuple[str, str, str]]: +def get_material_set_item_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global psetnames obj = context.active_object assert obj @@ -142,7 +142,7 @@ def get_material_set_item_pset_names(self, context) -> list[tuple[str, str, str] return psetnames[ifc_class] -def get_task_qto_names(self, context): +def get_task_qto_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global qtonames ifc_class = "IfcTask" if ifc_class not in qtonames: @@ -151,7 +151,7 @@ def get_task_qto_names(self, context): return qtonames[ifc_class] -def get_resource_pset_names(self, context): +def get_resource_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global psetnames rprops = context.scene.BIMResourceProperties rtprops = context.scene.BIMResourceTreeProperties @@ -162,7 +162,7 @@ def get_resource_pset_names(self, context): return psetnames[ifc_class] -def get_resource_qto_names(self, context): +def get_resource_qto_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global qtonames rprops = context.scene.BIMResourceProperties rtprops = context.scene.BIMResourceTreeProperties @@ -173,7 +173,7 @@ def get_resource_qto_names(self, context): return qtonames[ifc_class] -def get_group_pset_names(self, context): +def get_group_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global psetnames ifc_class = "IfcGroup" if ifc_class not in psetnames: @@ -182,7 +182,7 @@ def get_group_pset_names(self, context): return psetnames[ifc_class] -def get_group_qto_names(self, context): +def get_group_qto_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global qtonames ifc_class = "IfcGroup" if ifc_class not in qtonames: @@ -191,7 +191,7 @@ def get_group_qto_names(self, context): return qtonames[ifc_class] -def get_profile_pset_names(self, context): +def get_profile_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global psetnames pprops = tool.Profile.get_profile_props() ifc_class = tool.Ifc.get().by_id(pprops.profiles[pprops.active_profile_index].ifc_definition_id).is_a() @@ -201,7 +201,7 @@ def get_profile_pset_names(self, context): return psetnames[ifc_class] -def get_work_schedule_pset_names(self, context): +def get_work_schedule_pset_names(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: global psetnames ifc_class = "IfcWorkSchedule" if ifc_class not in psetnames: @@ -211,7 +211,7 @@ def get_work_schedule_pset_names(self, context): # TODO: unsafe? -def get_qto_name(self, context): +def get_qto_name(self: "PsetProperties", context: bpy.types.Context) -> tool.Blender.BLENDER_ENUM_ITEMS: pset_type = repr(self) prop_type = pset_type.split(".")[-1] if "bpy.data.objects" in pset_type: @@ -226,21 +226,21 @@ def get_qto_name(self, context): return [("BBIM_CUSTOM", "Custom Qto", "Create a quantity set without using a template."), None] + results -def get_object_qto_name(self, context): +def get_object_qto_name(self: "PsetProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: if not ObjectPsetsData.is_loaded: ObjectPsetsData.load() return ObjectPsetsData.data["qto_name"] # TODO: unsafe? -def get_template_type(self, context): +def get_template_type(self: "AddEditProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: version = tool.Ifc.get_schema() for t in ("IfcPropertySingleValue", "IfcPropertyEnumeratedValue"): yield (t, t, ifcopenshell.util.doc.get_entity_doc(version, t).get("description", "")) # TODO: unsafe? -def get_primary_measure_type(self, context): +def get_primary_measure_type(self: "AddEditProperties", context: object) -> tool.Blender.BLENDER_ENUM_ITEMS: if not AddEditCustomPropertiesData.is_loaded: AddEditCustomPropertiesData.load() return AddEditCustomPropertiesData.data["primary_measure_type"] @@ -249,6 +249,9 @@ def get_primary_measure_type(self, context): class IfcPropertyEnumeratedValue(PropertyGroup): enumerated_values: CollectionProperty(type=Attribute) + if TYPE_CHECKING: + enumerated_values: bpy.types.bpy_prop_collection_idprop[Attribute] + IfcPropertyValueType = Literal["IfcPropertySingleValue", "IfcPropertyEnumeratedValue"] @@ -297,6 +300,11 @@ class RenameProperties(PropertyGroup): existing_property_name: StringProperty(name="Existing Property Name") new_property_name: StringProperty(name="New Property Name") + if TYPE_CHECKING: + pset_name: str + existing_property_name: str + new_property_name: str + class AddEditProperties(PropertyGroup): pset_name: StringProperty(name="Pset") @@ -309,6 +317,17 @@ class AddEditProperties(PropertyGroup): template_type: EnumProperty(items=get_template_type, name="Template Type") enum_values: CollectionProperty(name="Enum Values", type=Attribute) + if TYPE_CHECKING: + pset_name: str + property_name: str + string_value: str + bool_value: bool + int_value: int + float_value: float + primary_measure_type: str + template_type: str + enum_values: bpy.types.bpy_prop_collection_idprop[Attribute] + def get_value_name(self) -> Union[Literal["string_value", "bool_value", "int_value", "float_value"], None]: schema = tool.Ifc.schema() ifc_data_type = schema.declaration_by_name(self.primary_measure_type) @@ -326,7 +345,14 @@ class AddEditProperties(PropertyGroup): class DeletePsets(PropertyGroup): pset_name: StringProperty(name="Pset") + if TYPE_CHECKING: + pset_name: str + class GlobalPsetProperties(PropertyGroup): pset_filter: StringProperty(name="Pset Filter", options={"TEXTEDIT_UPDATE"}) qto_filter: StringProperty(name="Qto Filter", options={"TEXTEDIT_UPDATE"}) + + if TYPE_CHECKING: + pset_filter: str + qto_filter: str diff --git a/src/bonsai/bonsai/tool/pset.py b/src/bonsai/bonsai/tool/pset.py index 94b6c21d8d..11c2478a59 100644 --- a/src/bonsai/bonsai/tool/pset.py +++ b/src/bonsai/bonsai/tool/pset.py @@ -263,7 +263,7 @@ class Pset(bonsai.core.tool.Pset): pset_template: ifcopenshell.entity_instance, prop_template: ifcopenshell.entity_instance, data: dict[str, Any], - props: bpy.types.PropertyGroup, + props: PsetProperties, ) -> None: prop = props.properties.add() prop.name = prop_template.Name @@ -293,7 +293,7 @@ class Pset(bonsai.core.tool.Pset): cls, pset_template: ifcopenshell.entity_instance, pset: Union[ifcopenshell.entity_instance, None], - props: bpy.types.PropertyGroup, + props: PsetProperties, ) -> None: if pset: data = ifcopenshell.util.element.get_property_definition(pset, verbose=True) diff --git a/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py b/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py index b95f0f7945..3f3a9881a7 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py +++ b/src/ifcopenshell-python/ifcopenshell/api/geometry/add_representation.py @@ -143,6 +143,7 @@ class Usecase: else: vx = vz.cross(X_AXIS) vy = vx.cross(vz) + assert isinstance(vy, Vector) tM = Matrix( [[vx.x, vy.x, vz.x, co.x], [vx.y, vy.y, vz.y, co.y], [vx.z, vy.z, vz.z, co.z], [0, 0, 0, 1]] ).inverted() diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index 4567a01d3d..5509167179 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -568,6 +568,8 @@ class entity_instance: ) -> dict[str, Any]: """Return a dictionary of the entity_instance's properties (Python and IFC) and their values. + Resulting dictionary keys: 'id', 'type', all entity attribute names. + :param include_identifier: Whether or not to include the STEP numerical identifier :param recursive: Whether or not to convert referenced IFC elements into dictionaries too. All attributes also apply recursively :param return_type: The return data type to be casted into diff --git a/src/ifcopenshell-python/ifcopenshell/ifcopenshell_wrapper.pyi b/src/ifcopenshell-python/ifcopenshell/ifcopenshell_wrapper.pyi index 2caa9757a6..1bce3a7734 100644 --- a/src/ifcopenshell-python/ifcopenshell/ifcopenshell_wrapper.pyi +++ b/src/ifcopenshell-python/ifcopenshell/ifcopenshell_wrapper.pyi @@ -584,7 +584,7 @@ class aggregation_type(parameter_type): bag_type: Any list_type: Any set_type: Any - def as_aggregation_type(self): ... + def as_aggregation_type(self) -> aggregation_type: ... def bound1(self): ... def bound2(self): ... def type_of_aggregation(self): ... @@ -1120,8 +1120,8 @@ class matrix4(item): class named_type(parameter_type): def _is(self, *args): ... - def as_named_type(self): ... - def declared_type(self): ... + def as_named_type(self) -> named_type: ... + def declared_type(self) -> declaration: ... class node(item): def calc_hash(self): ... @@ -1146,10 +1146,16 @@ class offset_function(function_item): def start(self): ... class parameter_type: - def _is(self, *args): ... - def as_aggregation_type(self): ... - def as_named_type(self): ... - def as_simple_type(self): ... + def _is(self, *args: Union[str, declaration]) -> bool: + """ + :param args: IFC class name or declaration to check. + Argument accepts only 1 value. + """ + ... + + def as_aggregation_type(self) -> Union[aggregation_type, None]: ... + def as_named_type(self) -> Union[named_type, None]: ... + def as_simple_type(self) -> Union[simple_type, None]: ... class piecewise_function(function_item): def calc_hash(self): ... @@ -1268,7 +1274,7 @@ class simple_type(parameter_type): real_type: Any string_type: Any datatype_COUNT: Any - def as_simple_type(self): ... + def as_simple_type(self) -> simple_type: ... def declared_type(self): ... class solid: diff --git a/src/ifcopenshell-python/ifcopenshell/util/attribute.py b/src/ifcopenshell-python/ifcopenshell/util/attribute.py index 06cd10fcff..a3212aca67 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/attribute.py +++ b/src/ifcopenshell-python/ifcopenshell/util/attribute.py @@ -17,13 +17,22 @@ # along with IfcOpenShell. If not, see . import ifcopenshell.ifcopenshell_wrapper as ifcopenshell_wrapper -from typing import Union +from typing import Union, Literal + +PrimitiveType = Literal["entity", "string", "float", "integer", "boolean", "enum", "binary"] +ComplexPrimitiveType = Literal["list", "array", "set"] +PrimitiveTypeOutput = Union[ + PrimitiveType, + tuple[ComplexPrimitiveType, "PrimitiveTypeOutput"], + tuple[Literal["select"], tuple["PrimitiveTypeOutput", ...]], + None, +] def get_primitive_type( attribute_or_data_type: Union[ifcopenshell_wrapper.attribute, ifcopenshell_wrapper.parameter_type], -) -> Union[str, tuple[str, list[str]]]: - if hasattr(attribute_or_data_type, "type_of_attribute"): +) -> PrimitiveTypeOutput: + if isinstance(attribute_or_data_type, ifcopenshell_wrapper.attribute): data_type = str(attribute_or_data_type.type_of_attribute()) else: data_type = str(attribute_or_data_type) @@ -56,8 +65,16 @@ def get_primitive_type( def get_enum_items(attribute: ifcopenshell_wrapper.attribute) -> tuple[str, ...]: - return attribute.type_of_attribute().declared_type().enumeration_items() + named_type = attribute.type_of_attribute().as_named_type() + assert named_type + enumeration = named_type.declared_type().as_enumeration_type() + assert enumeration + return enumeration.enumeration_items() -def get_select_items(attribute: ifcopenshell_wrapper.attribute) -> tuple[ifcopenshell_wrapper.entity, ...]: - return attribute.type_of_attribute().declared_type().select_list() +def get_select_items(attribute: ifcopenshell_wrapper.attribute) -> tuple[ifcopenshell_wrapper.declaration, ...]: + named_type = attribute.type_of_attribute().as_named_type() + assert named_type + select_type = named_type.declared_type().as_select_type() + assert select_type + return select_type.select_list()