Skip to content

Commit

Permalink
added support for type conversion of Timestamp_Now
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Oct 21, 2024
1 parent 8286847 commit c998795
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions osbot_utils/base_classes/Type_Safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from osbot_utils.utils.Objects import default_value, value_type_matches_obj_annotation_for_attr, \
raise_exception_on_obj_type_annotation_mismatch, obj_is_attribute_annotation_of_type, enum_from_value, \
obj_is_type_union_compatible, value_type_matches_obj_annotation_for_union_attr, \
convert_dict_to_value_from_obj_annotation, dict_to_obj, convert_str_to_value_from_obj_annotation
convert_dict_to_value_from_obj_annotation, dict_to_obj, convert_to_value_from_obj_annotation

# Backport implementations of get_origin and get_args for Python 3.7
if sys.version_info < (3, 8): # pragma: no cover
Expand Down Expand Up @@ -99,8 +99,8 @@ def __setattr__(self, name, value):
if value is not None:
if type(value) is dict:
value = convert_dict_to_value_from_obj_annotation(self, name, value)
if type(value) is str:
value = convert_str_to_value_from_obj_annotation (self, name, value)
if type(value) in [int, str]: # for now only a small number of str and int classes are supported (until we understand the full implications of this)
value = convert_to_value_from_obj_annotation (self, name, value)
check_1 = value_type_matches_obj_annotation_for_attr (self, name, value)
check_2 = value_type_matches_obj_annotation_for_union_attr(self, name, value)
if (check_1 is False and check_2 is None or
Expand Down
9 changes: 6 additions & 3 deletions osbot_utils/utils/Objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
from collections.abc import Mapping
from typing import Union
from types import SimpleNamespace
from osbot_utils.helpers.Timestamp_Now import Timestamp_Now
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.utils.Misc import list_set
from osbot_utils.utils.Str import str_unicode_escape, str_max_width

TYPE_SAFE__CONVERT_VALUE__SUPPORTED_TYPES = [Random_Guid, Timestamp_Now]

# Backport implementations of get_origin and get_args for Python 3.7
if sys.version_info < (3, 8):
def get_origin(tp):
Expand Down Expand Up @@ -105,14 +108,14 @@ def convert_dict_to_value_from_obj_annotation(target, attr_name, value):
return attribute_annotation(**value)
return value

def convert_str_to_value_from_obj_annotation(target, attr_name, value): # todo: see the side effects of doing this for all strings
def convert_to_value_from_obj_annotation(target, attr_name, value): # todo: see the side effects of doing this for all ints and floats
if target is not None and attr_name is not None:
if hasattr(target, '__annotations__'):
obj_annotations = target.__annotations__
if hasattr(obj_annotations,'get'):
attribute_annotation = obj_annotations.get(attr_name)
if 'str' in base_classes_names(attribute_annotation): # todo: figure out a better way to handle these special type casting (used for example by Random_Guid)
if attribute_annotation == Random_Guid: # and add support for other types like int (which is used my Timestamp_Now)
if attribute_annotation:
if attribute_annotation in TYPE_SAFE__CONVERT_VALUE__SUPPORTED_TYPES: # for now hard-coding this to just these types until we understand the side effects
return attribute_annotation(value)
return value

Expand Down

0 comments on commit c998795

Please sign in to comment.