Merge branch 'bugfix/type_checks' into 'master'

Added conditionals for type checks

See merge request caimira/caimira!519
This commit is contained in:
Luis Aleixo 2024-10-22 10:06:57 +02:00
commit 23c8a349b2
3 changed files with 16 additions and 10 deletions

View file

@ -131,7 +131,7 @@ python -m cern_caimira.apps.calculator
To run with a specific template theme created: 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: To run the entire app in a different `APPLICATION_ROOT` path:

View file

@ -84,14 +84,17 @@ def _build_mc_model(model: dataclass_instance) -> typing.Type[MCModelBase[_Model
else: else:
# Check that we don't need to do anything with this type. # Check that we don't need to do anything with this type.
for item in new_field.type.__args__: if hasattr(new_field.type, "__args__"):
if getattr(item, '__module__', None) == 'source.models.models': for item in new_field.type.__args__:
raise ValueError( if getattr(item, '__module__', None) == 'source.models.models':
f"unsupported type annotation transformation required for {new_field.type}") raise ValueError(
f"unsupported type annotation transformation required for {new_field.type}")
elif field_type.__module__ == 'source.models.models': elif field_type.__module__ == 'source.models.models':
mc_model = getattr(sys.modules[__name__], new_field.type.__name__) if isinstance(new_field.type, type):
field_type = typing.Union[new_field.type, mc_model] 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)) fields.append((new_field.name, field_type, new_field))
bases = [] bases = []
@ -120,7 +123,7 @@ def _build_mc_model(model: dataclass_instance) -> typing.Type[MCModelBase[_Model
_MODEL_CLASSES = [ _MODEL_CLASSES = [
cls for cls in vars(models).values() cls for cls in vars(models).values()
if dataclasses.is_dataclass(cls) if dataclasses.is_dataclass(cls) and isinstance(cls, type)
] ]

View file

@ -25,8 +25,11 @@ class StateBuilder:
def resolve_builder(self, field: dataclasses.Field): def resolve_builder(self, field: dataclasses.Field):
method_name = [ method_name = [
f'build_name_{field.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: for name in method_name:
method = getattr(self, name, None) method = getattr(self, name, None)
if method is not None: if method is not None: