-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aran-Fey
committed
Mar 10, 2024
1 parent
0ddc5a5
commit 281ddec
Showing
4 changed files
with
41 additions
and
9 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
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,28 @@ | ||
from typing import Dict, cast | ||
|
||
from .type_info import TypeInfo | ||
from ..misc import static_mro, static_vars | ||
from ..types import TypeAnnotation | ||
|
||
|
||
__all__ = ["get_type_annotations"] | ||
|
||
|
||
def get_type_annotations(cls: type) -> Dict[str, TypeInfo]: | ||
""" | ||
Similar to `typing.get_type_hints`, but returns the annotations as `TypeInfo` objects. | ||
""" | ||
result: Dict[str, TypeInfo] = {} | ||
|
||
for class_ in static_mro(cls): | ||
cls_vars = static_vars(class_) | ||
try: | ||
annotations = cast(Dict[str, TypeAnnotation], cls_vars["__annotations__"]) | ||
except KeyError: | ||
continue | ||
|
||
for attr_name, annotation in annotations.items(): | ||
if attr_name not in result: | ||
result[attr_name] = TypeInfo(annotation) | ||
|
||
return result |
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