mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-14 08:17:15 +02:00
adds warning to DataService when types change, types will not be converted anymore
This commit is contained in:
@ -64,24 +64,47 @@ class DataService(rpyc.Service, AbstractDataService):
|
|||||||
self._initialised = True
|
self._initialised = True
|
||||||
|
|
||||||
def __setattr__(self, __name: str, __value: Any) -> None:
|
def __setattr__(self, __name: str, __value: Any) -> None:
|
||||||
# converting attributes that are not properties
|
# Check and warn for unexpected type changes in attributes
|
||||||
if not is_property_attribute(self, __name):
|
self._warn_on_type_change(__name, __value)
|
||||||
current_value = getattr(self, __name, None)
|
|
||||||
# parse ints into floats if current value is a float
|
|
||||||
if isinstance(current_value, float) and isinstance(__value, int):
|
|
||||||
__value = float(__value)
|
|
||||||
|
|
||||||
if isinstance(current_value, u.Quantity):
|
# Warn if setting private attributes
|
||||||
__value = u.convert_to_quantity(__value, str(current_value.u))
|
self._warn_on_private_attr_set(__name)
|
||||||
|
|
||||||
|
# Set the attribute
|
||||||
super().__setattr__(__name, __value)
|
super().__setattr__(__name, __value)
|
||||||
|
|
||||||
if __name.startswith(f"_{self.__class__.__name__}__"):
|
def _warn_on_type_change(self, attr_name: str, new_value: Any) -> None:
|
||||||
|
if is_property_attribute(self, attr_name):
|
||||||
|
return
|
||||||
|
|
||||||
|
current_value = getattr(self, attr_name, None)
|
||||||
|
if self._is_unexpected_type_change(current_value, new_value):
|
||||||
|
logger.warning(
|
||||||
|
"Type of '%s' changed from '%s' to '%s'. This may have unwanted "
|
||||||
|
"side effects! Consider setting it to '%s' directly.",
|
||||||
|
attr_name,
|
||||||
|
type(current_value).__name__,
|
||||||
|
type(new_value).__name__,
|
||||||
|
type(current_value).__name__,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _is_unexpected_type_change(self, current_value: Any, new_value: Any) -> bool:
|
||||||
|
return (
|
||||||
|
isinstance(current_value, float)
|
||||||
|
and not isinstance(new_value, float)
|
||||||
|
or (
|
||||||
|
isinstance(current_value, u.Quantity)
|
||||||
|
and not isinstance(new_value, u.Quantity)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _warn_on_private_attr_set(self, attr_name: str) -> None:
|
||||||
|
if attr_name.startswith(f"_{self.__class__.__name__}__"):
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Warning: You should not set private but rather protected attributes! "
|
"Warning: You should not set private but rather protected attributes! "
|
||||||
"Use %s instead of %s.",
|
"Use %s instead of %s.",
|
||||||
__name.replace(f"_{self.__class__.__name__}__", "_"),
|
attr_name.replace(f"_{self.__class__.__name__}__", "_"),
|
||||||
__name.replace(f"_{self.__class__.__name__}__", "__"),
|
attr_name.replace(f"_{self.__class__.__name__}__", "__"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __check_instance_classes(self) -> None:
|
def __check_instance_classes(self) -> None:
|
||||||
|
Reference in New Issue
Block a user