From f2696d5352547e6ce0d1c9145c6a4157adbbf36e Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 13 May 2024 17:25:05 +0500 Subject: [PATCH] avoid confusing TypeErrors from api calls After ab5ea4c85 it was always throwing wrong singature errors like below even if TypeError was caused by some internal issues inside API - it was adding couple extra steps to traceback making errors more noisy. TypeError: Incorrect function arguments provided for library.edit_library attribute 'VersionDate' for entity 'IFC2X3.IfcLibraryInformation' is expecting value of type 'ENTITY INSTANCE', got 'str'.. You specified args (,) and settings {'library': #1=IfcLibraryInformation('Name','Version',$,$,$), 'attributes': {'Name': 'Name', 'Version': 'Version', 'VersionDate': 'VersionDate', 'Location': 'Location', 'Description': 'Description'}} E Correct signature is (file: ifcopenshell.file.file, library: ifcopenshell.entity_instance.entity_instance, attributes: dict[str, typing.Any]) -> None See help(ifcopenshell.api.library.edit_library) for documentation. --- .../ifcopenshell/api/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/__init__.py b/src/ifcopenshell-python/ifcopenshell/api/__init__.py index 7ecf53329f..b4311519fc 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/api/__init__.py @@ -324,8 +324,18 @@ def wrap_usecase(usecase_path, usecase): try: result = usecase(*args, **settings) - except TypeError as e: - msg = f"Incorrect function arguments provided for {usecase_path}\n{str(e)}. You specified args {args} and settings {settings}\n\nCorrect signature is {inspect.signature(usecase)}\nSee help(ifcopenshell.api.{usecase_path}) for documentation." + except NotImplementedError as e: + if not e.args[0].startswith(f"{usecase.__name__}()"): + # signature errors typically start with function name + # e.g. "TypeError: edit_library() got an unexpected keyword argument 'test'" + # otherwise it's an error inside api call and we shouldn't get in the way + raise e + msg = ( + f"Incorrect function arguments provided for {usecase_path}\n{str(e)}. " + f"You specified args {args} and settings {settings}\n\n" + f"Correct signature is {inspect.signature(usecase)}\n" + f"See help(ifcopenshell.api.{usecase_path}) for documentation." + ) raise TypeError(msg) from e if should_run_listeners: