From 0b901bff5f39c34872bd50cfbeccfa3687c327a3 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 24 Jun 2024 11:20:04 +0500 Subject: [PATCH] typing --- .../ifcopenshell/express/bootstrap.py | 3 ++- .../ifcopenshell/express/express_parser.py | 3 ++- .../ifcopenshell/express/mapping.py | 4 +++- .../ifcopenshell/express/rule_executor.py | 3 ++- .../ifcopenshell/express/schema.py | 3 ++- .../ifcopenshell/express/schema_class.py | 4 +++- .../util/ifc4x3dev_scrape_data_for_docs.py | 17 +++++++++-------- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/express/bootstrap.py b/src/ifcopenshell-python/ifcopenshell/express/bootstrap.py index c7e126c8e1..c93fdc21f5 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/bootstrap.py +++ b/src/ifcopenshell-python/ifcopenshell/express/bootstrap.py @@ -207,6 +207,7 @@ if __name__ == "__main__": print(""" # This file is generated by IfcOpenShell ifcexpressparser bootstrap.py +from __future__ import annotations import os import sys import pickle @@ -217,7 +218,7 @@ import mapping from pyparsing import * from nodes import * -def parse(fn): +def parse(fn: str) -> mapping.Mapping: cache_file = fn + ".cache.dat" if os.path.exists(cache_file) and os.path.getmtime(cache_file) >= os.path.getmtime(fn): with open(cache_file, "rb") as f: diff --git a/src/ifcopenshell-python/ifcopenshell/express/express_parser.py b/src/ifcopenshell-python/ifcopenshell/express/express_parser.py index 61c88ef8ed..3cd688ac58 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/express_parser.py +++ b/src/ifcopenshell-python/ifcopenshell/express/express_parser.py @@ -1,6 +1,7 @@ # This file is generated by IfcOpenShell ifcexpressparser bootstrap.py +from __future__ import annotations import os import sys import pickle @@ -11,7 +12,7 @@ import mapping from pyparsing import * from nodes import * -def parse(fn): +def parse(fn: str) -> mapping.Mapping: cache_file = fn + ".cache.dat" if os.path.exists(cache_file) and os.path.getmtime(cache_file) >= os.path.getmtime(fn): with open(cache_file, "rb") as f: diff --git a/src/ifcopenshell-python/ifcopenshell/express/mapping.py b/src/ifcopenshell-python/ifcopenshell/express/mapping.py index ffabee4e82..3e2d819e1c 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/mapping.py +++ b/src/ifcopenshell-python/ifcopenshell/express/mapping.py @@ -17,9 +17,11 @@ # along with IfcOpenShell. If not, see . +from __future__ import annotations import sys import nodes import templates +import schema class Mapping: @@ -55,7 +57,7 @@ class Mapping: ] ) - def __init__(self, schema): + def __init__(self, schema: schema.Schema): self.schema = schema def flatten_type_string(self, type): diff --git a/src/ifcopenshell-python/ifcopenshell/express/rule_executor.py b/src/ifcopenshell-python/ifcopenshell/express/rule_executor.py index b91597e042..0bf8ac439c 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/rule_executor.py +++ b/src/ifcopenshell-python/ifcopenshell/express/rule_executor.py @@ -3,6 +3,7 @@ import re import ast import collections import ifcopenshell +from logging import Logger from dataclasses import dataclass from codegen import indent @@ -62,7 +63,7 @@ def fix_type(v): return v -def run(f, logger): +def run(f: ifcopenshell.file, logger: Logger) -> None: from _pytest import assertion if hasattr(logger, "set_instance"): diff --git a/src/ifcopenshell-python/ifcopenshell/express/schema.py b/src/ifcopenshell-python/ifcopenshell/express/schema.py index bfa2e2a6f0..ebb050d1ae 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/schema.py +++ b/src/ifcopenshell-python/ifcopenshell/express/schema.py @@ -20,6 +20,7 @@ import nodes import platform import collections +import pyparsing if tuple(map(int, platform.python_version_tuple())) < (2, 7): import ordereddict @@ -83,7 +84,7 @@ class Schema: def __getitem__(self, key): return self.all_declarations[OrderedCaseInsensitiveDict_KeyObject(key)] - def __init__(self, parsetree): + def __init__(self, parsetree: pyparsing.ParseResults): self.tree = parsetree schema = next(iter(parsetree.syntax[0])) self.name = schema.simple_id diff --git a/src/ifcopenshell-python/ifcopenshell/express/schema_class.py b/src/ifcopenshell-python/ifcopenshell/express/schema_class.py index 934f47309b..233c39844b 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/schema_class.py +++ b/src/ifcopenshell-python/ifcopenshell/express/schema_class.py @@ -17,12 +17,14 @@ # along with IfcOpenShell. If not, see . +from __future__ import annotations import operator import re import nodes import codegen import templates +import mapping from collections import defaultdict @@ -363,7 +365,7 @@ const std::string strings[] = {%s}; class SchemaClass(codegen.Base): - def __init__(self, mapping, code=EarlyBoundCodeWriter): + def __init__(self, mapping: mapping.Mapping, code=EarlyBoundCodeWriter): class UnmetDependenciesException(Exception): pass diff --git a/src/ifcopenshell-python/ifcopenshell/util/ifc4x3dev_scrape_data_for_docs.py b/src/ifcopenshell-python/ifcopenshell/util/ifc4x3dev_scrape_data_for_docs.py index 77fbb708de..5daf2b5f46 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/ifc4x3dev_scrape_data_for_docs.py +++ b/src/ifcopenshell-python/ifcopenshell/util/ifc4x3dev_scrape_data_for_docs.py @@ -25,16 +25,17 @@ except ModuleNotFoundError as e: ) raise e -from collections import Counter import itertools import operator -from bs4 import BeautifulSoup import json import ifcopenshell +from collections import Counter +from bs4 import BeautifulSoup +from typing import Any # Hacky modified functions from server.py to make parser work -def get_definition_from_md(resource, mdc): +def get_definition_from_md(resource: str, mdc: str) -> str: # Only match up to the first h2 lines = [] for line in mdc.split("\n"): @@ -49,7 +50,7 @@ def get_definition_from_md(resource, mdc): return mdc_splitted[1] if len(mdc_splitted) > 1 else "" -def get_type_values(resource, mdc): +def get_type_values(resource: str, mdc: str) -> dict[str, Any]: values = R.type_values.get(resource) if not values: return @@ -128,7 +129,7 @@ def get_attributes_keep_md(resource, builder): # ------------------------- -def get_description_json(resource): +def get_description_json(resource: str) -> str: md = get_resource_path(resource, abort_on_error=False) mdc = open(md, "r", encoding="utf-8").read() description = get_definition_from_md(resource, mdc) @@ -153,7 +154,7 @@ def get_predefined_type_values_json(resource): return get_type_values(resource, mdc)["schema_values"] -def save_entities_data(entities): +def save_entities_data(entities: list[str]) -> None: entities_description = dict() for entity in entities: entity_data = dict() @@ -198,6 +199,6 @@ def save_entities_data(entities): if __name__ == "__main__": - schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name("IFC4X3") - entities = [e.name() for e in schema.declarations()] + schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name("IFC4X3_ADD2") + entities: list[str] = [e.name() for e in schema.declarations()] save_entities_data(entities)