`ParseContext` should also process `kwonlyargs`.

This commit is contained in:
Fabio Manganiello 2023-10-10 00:37:08 +02:00
parent 52e353dc14
commit b225b056b0
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 19 additions and 3 deletions

View File

@ -35,15 +35,18 @@ class ParseContext:
Initialize the return type and parameters from the function annotations.
"""
# If we're dealing with a wrapped @action, then unwrap the underlying function
if hasattr(self.obj, "wrapped"):
self.obj = self.obj.wrapped
# Initialize the return type from the annotations
annotations = getattr(self.obj, "__annotations__", {})
if annotations:
self.returns.type = annotations.get("return")
# Initialize the parameters from the signature
spec = inspect.getfullargspec(self.obj)
defaults = spec.defaults or ()
defaults = defaults + ((Any,) * (len(self.param_names) - len(defaults or ())))
defaults = self.spec.defaults or ()
defaults = ((Any,) * (len(self.param_names) - len(defaults or ()))) + defaults
self.parsed_params = {
name: Argument(
name=name,
@ -54,6 +57,19 @@ class ParseContext:
for name, default in zip(self.param_names, defaults)
}
# Update the parameters with the keyword-only arguments
self.parsed_params.update(
{
arg: Argument(
name=arg,
type=self.param_types.get(arg),
default=(self.spec.kwonlydefaults or {}).get(arg),
required=arg not in (self.spec.kwonlydefaults or {}),
)
for arg in self.spec.kwonlyargs
}
)
@property
def spec(self) -> inspect.FullArgSpec:
return inspect.getfullargspec(self.obj)