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 untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -35,15 +35,18 @@ class ParseContext:
Initialize the return type and parameters from the function annotations. 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 # Initialize the return type from the annotations
annotations = getattr(self.obj, "__annotations__", {}) annotations = getattr(self.obj, "__annotations__", {})
if annotations: if annotations:
self.returns.type = annotations.get("return") self.returns.type = annotations.get("return")
# Initialize the parameters from the signature # Initialize the parameters from the signature
spec = inspect.getfullargspec(self.obj) defaults = self.spec.defaults or ()
defaults = spec.defaults or () defaults = ((Any,) * (len(self.param_names) - len(defaults or ()))) + defaults
defaults = defaults + ((Any,) * (len(self.param_names) - len(defaults or ())))
self.parsed_params = { self.parsed_params = {
name: Argument( name: Argument(
name=name, name=name,
@ -54,6 +57,19 @@ class ParseContext:
for name, default in zip(self.param_names, defaults) 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 @property
def spec(self) -> inspect.FullArgSpec: def spec(self) -> inspect.FullArgSpec:
return inspect.getfullargspec(self.obj) return inspect.getfullargspec(self.obj)