-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/enum and forward ref support (#7)
- Loading branch information
Showing
5 changed files
with
188 additions
and
17 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
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,26 @@ | ||
import inspect | ||
from inspect import Parameter, _empty | ||
from typing import Callable, get_type_hints | ||
|
||
|
||
class TypedParameter(Parameter): | ||
def __init__(self, *args, param_type=_empty, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.param_type = param_type | ||
|
||
@classmethod | ||
def from_paramaeter(cls, param: Parameter) -> "TypedParameter": | ||
return cls( | ||
name=param.name, default=param.default, annotation=param.annotation, kind=param.kind | ||
) | ||
|
||
|
||
def get_types_parameters(fn: Callable) -> list[TypedParameter]: | ||
type_hints = get_type_hints(fn) | ||
parameters = list(inspect.signature(fn).parameters.values()) | ||
typed_params = [] | ||
for p in parameters: | ||
typed_param = TypedParameter.from_paramaeter(param=p) | ||
typed_param.param_type = type_hints[typed_param.name] | ||
typed_params.append(typed_param) | ||
return typed_params |