-
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.
refactored non-cached methods to Type_Safe__Not_Cached starting with …
…the annotations
- Loading branch information
Showing
11 changed files
with
99 additions
and
85 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,27 @@ | ||
from typing import get_origin | ||
from osbot_utils.type_safe.shared.Type_Safe__Cache import type_safe_cache | ||
|
||
|
||
class Type_Safe__Annotations: | ||
|
||
def all_annotations(self, target): | ||
return type_safe_cache.get_annotations(target) # use cache | ||
|
||
def all_annotations__in_class(self, cls): | ||
return type_safe_cache.get_class_annotations(cls) | ||
|
||
def obj_attribute_annotation(self, target, attr_name): | ||
return self.all_annotations(target).get(attr_name) # use cache | ||
|
||
def obj_is_attribute_annotation_of_type(self, target, attr_name, expected_type): | ||
attribute_annotation = self.obj_attribute_annotation(target, attr_name) | ||
if expected_type is attribute_annotation: | ||
return True | ||
if expected_type is type(attribute_annotation): | ||
return True | ||
if expected_type is get_origin(attribute_annotation): # todo: use get_origin cache # handle genericAlias | ||
return True | ||
return False | ||
|
||
|
||
type_safe_annotations = Type_Safe__Annotations() |
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,19 @@ | ||
class Type_Safe__Not_Cached: | ||
|
||
def all_annotations(self, target): | ||
annotations = {} | ||
if hasattr(target.__class__, '__mro__'): | ||
for base in reversed(target.__class__.__mro__): | ||
if hasattr(base, '__annotations__'): | ||
annotations.update(base.__annotations__) | ||
return annotations | ||
|
||
def all_annotations__in_class(self, target): | ||
annotations = {} | ||
if hasattr(target, '__mro__'): | ||
for base in reversed(target.__mro__): | ||
if hasattr(base, '__annotations__'): | ||
annotations.update(base.__annotations__) | ||
return annotations | ||
|
||
type_safe_not_cached = Type_Safe__Not_Cached() |
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
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
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
Oops, something went wrong.