From 1b56a6acd98ecb960f2544da66d2812d2e4daaff Mon Sep 17 00:00:00 2001 From: lrdossan Date: Tue, 22 Oct 2024 09:56:16 +0200 Subject: [PATCH 1/2] added conditionals type checks --- .../calculator/models/monte_carlo/models.py | 19 +++++++++++-------- .../cern_caimira/apps/expert_apps/state.py | 5 ++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/caimira/src/caimira/calculator/models/monte_carlo/models.py b/caimira/src/caimira/calculator/models/monte_carlo/models.py index f4ad09e2..9ee1d481 100644 --- a/caimira/src/caimira/calculator/models/monte_carlo/models.py +++ b/caimira/src/caimira/calculator/models/monte_carlo/models.py @@ -84,14 +84,17 @@ def _build_mc_model(model: dataclass_instance) -> typing.Type[MCModelBase[_Model else: # Check that we don't need to do anything with this type. - for item in new_field.type.__args__: - if getattr(item, '__module__', None) == 'source.models.models': - raise ValueError( - f"unsupported type annotation transformation required for {new_field.type}") + if hasattr(new_field.type, "__args__"): + for item in new_field.type.__args__: + if getattr(item, '__module__', None) == 'source.models.models': + raise ValueError( + f"unsupported type annotation transformation required for {new_field.type}") elif field_type.__module__ == 'source.models.models': - mc_model = getattr(sys.modules[__name__], new_field.type.__name__) - field_type = typing.Union[new_field.type, mc_model] - + if isinstance(new_field.type, type): + mc_model = getattr(sys.modules[__name__], new_field.type.__name__) + field_type = typing.Union[new_field.type, mc_model] + else: + raise ValueError(f"Expected a class/type but got {new_field.type}") fields.append((new_field.name, field_type, new_field)) bases = [] @@ -120,7 +123,7 @@ def _build_mc_model(model: dataclass_instance) -> typing.Type[MCModelBase[_Model _MODEL_CLASSES = [ cls for cls in vars(models).values() - if dataclasses.is_dataclass(cls) + if dataclasses.is_dataclass(cls) and isinstance(cls, type) ] diff --git a/cern_caimira/src/cern_caimira/apps/expert_apps/state.py b/cern_caimira/src/cern_caimira/apps/expert_apps/state.py index c2f2fcd5..98381975 100644 --- a/cern_caimira/src/cern_caimira/apps/expert_apps/state.py +++ b/cern_caimira/src/cern_caimira/apps/expert_apps/state.py @@ -25,8 +25,11 @@ class StateBuilder: def resolve_builder(self, field: dataclasses.Field): method_name = [ f'build_name_{field.name}', - f'build_type_{field.type.__name__}', ] + + if isinstance(field.type, type): + method_name.append(f'build_type_{field.type.__name__}') + for name in method_name: method = getattr(self, name, None) if method is not None: From b8da113aba33a656b0bbb9f9b3dfdb7229be79bd Mon Sep 17 00:00:00 2001 From: lrdossan Date: Tue, 22 Oct 2024 09:56:24 +0200 Subject: [PATCH 2/2] updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b603e659..f3da12c4 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ python -m cern_caimira.apps.calculator To run with a specific template theme created: ``` -python -m cern_caimira.apps.calculator --theme=cern_caimira/apps/templates/{theme} +python -m cern_caimira.apps.calculator --theme=cern_caimira/src/cern_caimira/apps/templates/{theme} ``` To run the entire app in a different `APPLICATION_ROOT` path: