-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started refactoring Type_Safe__Step__Class_Kwargs into separate class…
…es Type_Safe__Cache, Type_Safe__Raise_Exception, Type_Safe__Step__Class_Kwargs
- Loading branch information
Showing
6 changed files
with
83 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import inspect | ||
from weakref import WeakKeyDictionary | ||
|
||
|
||
class Type_Safe__Cache: | ||
|
||
_annotations_cache : WeakKeyDictionary | ||
_mro_cache : WeakKeyDictionary | ||
_valid_vars_cache : WeakKeyDictionary | ||
|
||
# Caching system for Type_Safe methods | ||
def __init__(self): | ||
self._annotations_cache = WeakKeyDictionary() # Cache for class annotations | ||
self._mro_cache = WeakKeyDictionary() # Cache for Method Resolution Order | ||
self._valid_vars_cache = WeakKeyDictionary() | ||
|
||
def get_class_annotations(self, cls): | ||
if cls not in self._annotations_cache: | ||
self._annotations_cache[cls] = cls.__annotations__.items() | ||
return self._annotations_cache[cls] | ||
|
||
def get_class_mro(self, cls): | ||
if cls not in self._mro_cache: | ||
self._mro_cache[cls] = inspect.getmro(cls) | ||
return self._mro_cache[cls] | ||
|
||
# todo: see if we have cache misses and invalid hits based on the validator (we might need more validator specific methods) | ||
def get_valid_class_variables(self, cls, validator): # Returns a dictionary of valid class variables that should be processed. Filters out internal variables, methods, and other non-data attributes. | ||
if cls not in self._valid_vars_cache: | ||
valid_variables = {} | ||
for name, value in vars(cls).items(): | ||
if not validator(name, value): | ||
valid_variables[name] = value | ||
self._valid_vars_cache[cls] = valid_variables | ||
return self._valid_vars_cache[cls] | ||
|
||
type_safe_cache = Type_Safe__Cache() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Any | ||
|
||
from osbot_utils.type_safe.steps.Type_Safe__Validation import IMMUTABLE_TYPES | ||
|
||
|
||
class Type_Safe__Raise_Exception: | ||
|
||
def type_mismatch_error(self, var_name: str, expected_type: Any, actual_value: Any) -> None: # Raises formatted error for type validation failures | ||
exception_message = f"variable '{var_name}' is defined as type '{expected_type}' but has value '{actual_value}' of type '{type(actual_value)}'" | ||
raise ValueError(exception_message) | ||
|
||
def immutable_type_error(self, var_name, var_type): | ||
exception_message = f"variable '{var_name}' is defined as type '{var_type}' which is not supported by Type_Safe, with only the following immutable types being supported: '{IMMUTABLE_TYPES}'" | ||
raise ValueError(exception_message) | ||
|
||
type_safe_raise_exception = Type_Safe__Raise_Exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import types | ||
from enum import EnumMeta | ||
|
||
IMMUTABLE_TYPES = (bool, int, float, complex, str, tuple, frozenset, bytes, types.NoneType, EnumMeta, type) | ||
|
||
class Type_Safe__Validation: | ||
pass | ||
|
||
|
||
type_safe_validation = Type_Safe__Validation() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters