all tests passes !
This commit is contained in:
@@ -13,34 +13,26 @@ class BaseElement:
|
||||
|
||||
def __setattr__(self, key: str, value: Any):
|
||||
print(f"!guarded_setattr {self} {key} {value}")
|
||||
print(f"__lam_object_mutable__ = {self.__lam_object_mutable__}")
|
||||
print(self.__lam_schema__)
|
||||
|
||||
if key.startswith("_"):
|
||||
print(f"hola: {self} ")
|
||||
return super().__setattr__(key, value)
|
||||
|
||||
if key in self.__lam_schema__.keys():
|
||||
if key in self.__dict__ and is_data_attribute(key, self.__dict__[key]):
|
||||
print(f"a: {self.__lam_object_mutable__}")
|
||||
if not self.__lam_object_mutable__:
|
||||
print("RAISE")
|
||||
raise ReadOnlyField(f"{key} is read-only")
|
||||
else:
|
||||
print("TYPE CHECK")
|
||||
try:
|
||||
check_type(
|
||||
value,
|
||||
self.__lam_schema__[key].annotations,
|
||||
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
|
||||
)
|
||||
except TypeCheckError as exp:
|
||||
raise InvalidFieldValue(
|
||||
f"Value of Field <{key}> is not of expected type {self.__lam_schema__[key].annotations}."
|
||||
) from exp
|
||||
return super().__setattr__(key, value)
|
||||
else:
|
||||
raise NewFieldForbidden("creating new fields is not allowed")
|
||||
if key not in self.__lam_schema__.keys():
|
||||
raise NewFieldForbidden(f"Can't create new object attributes: {key}")
|
||||
|
||||
if not self.__lam_object_mutable__:
|
||||
raise ReadOnlyField(f"{key} is read-only")
|
||||
|
||||
try:
|
||||
check_type(
|
||||
value,
|
||||
self.__lam_schema__[key].annotations,
|
||||
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
|
||||
)
|
||||
except TypeCheckError as exp:
|
||||
raise InvalidFieldValue(
|
||||
f"Value of Field <{key}> is not of expected type {self.__lam_schema__[key].annotations}."
|
||||
) from exp
|
||||
return super().__setattr__(key, value)
|
||||
|
||||
def freeze(self, force: bool = False):
|
||||
@@ -110,5 +102,5 @@ class BaseElement:
|
||||
raise SchemaViolation(f"Attribute <{name}> is missing from instance")
|
||||
|
||||
@classmethod
|
||||
def freeze_class(cls):
|
||||
def freeze_class(cls, force: bool = False):
|
||||
pass
|
||||
|
||||
@@ -85,6 +85,12 @@ class InvalidFieldValue(DABModelException):
|
||||
"""
|
||||
|
||||
|
||||
class InvalidFieldName(DABModelException):
|
||||
"""InvalidFieldName Exception class
|
||||
The Field name is invalid
|
||||
"""
|
||||
|
||||
|
||||
class NonExistingField(DABModelException):
|
||||
"""NonExistingField Exception class
|
||||
The given Field is non existing
|
||||
|
||||
@@ -7,10 +7,8 @@ from ..LAMFields.LAMField import LAMField
|
||||
from ..LAMFields.FrozenLAMField import FrozenLAMField
|
||||
from .element import _MetaElement
|
||||
from ..feature import Feature
|
||||
from ..exception import (
|
||||
InvalidFieldValue,
|
||||
InvalidFeatureInheritance,
|
||||
)
|
||||
from ..exception import InvalidFieldValue, InvalidFeatureInheritance, InvalidFieldName
|
||||
from ..tools import LAMdeepfreeze
|
||||
|
||||
|
||||
class _MetaAppliance(_MetaElement):
|
||||
@@ -88,6 +86,8 @@ class _MetaAppliance(_MetaElement):
|
||||
"""
|
||||
print(f"(appliance) process_new_field: {_fname}")
|
||||
print(namespace["__lam_schema__"]["features"].keys())
|
||||
if _fname == "feature":
|
||||
raise InvalidFieldName("'feature' is a reserved Field name")
|
||||
if _fname in namespace["__lam_schema__"]["features"].keys():
|
||||
print("??a")
|
||||
if not issubclass(_fvalue, namespace["__lam_schema__"]["features"][_fname]):
|
||||
@@ -161,8 +161,8 @@ class _MetaAppliance(_MetaElement):
|
||||
f"Invalid value for {fname}.{field_name}: "
|
||||
f"expected {field.annotations}, got {new_val!r}"
|
||||
) from exp
|
||||
# object.__setattr__(inst, field_name, LAMdeepfreeze(new_val))
|
||||
object.__setattr__(inst, field_name, new_val)
|
||||
object.__setattr__(inst, field_name, LAMdeepfreeze(new_val))
|
||||
# object.__setattr__(inst, field_name, new_val)
|
||||
inst.__lam_schema__[field_name] = FrozenLAMField(
|
||||
LAMField(field_name, new_val, field.annotations, field._info)
|
||||
)
|
||||
|
||||
@@ -524,6 +524,8 @@ class _MetaElement(type):
|
||||
mcs.commit_fields(_cls, name, bases, namespace, extensions)
|
||||
mcs.apply_initializer(_cls, name, bases, namespace, extensions)
|
||||
mcs.finalize_class(_cls, name, bases, namespace, extensions)
|
||||
if not _cls.__lam_class_mutable__:
|
||||
_cls.freeze_class(True)
|
||||
_cls.__lam_initialized__ = True
|
||||
return _cls
|
||||
|
||||
@@ -678,7 +680,11 @@ class _MetaElement(type):
|
||||
return super().__getattr__(name)
|
||||
# return cls.__dict__[name]
|
||||
if name in cls.__lam_schema__.keys():
|
||||
return cls.__lam_schema__[name].raw_value
|
||||
if cls.__lam_class_mutable__:
|
||||
print("HEy hey WHYYYYYY")
|
||||
return cls.__lam_schema__[name].raw_value
|
||||
else:
|
||||
return cls.__lam_schema__[name].value
|
||||
else:
|
||||
raise NonExistingField("Non existing class attribute")
|
||||
# return super().__getattr__(name)
|
||||
|
||||
Reference in New Issue
Block a user