|
26 | 26 |
|
27 | 27 |
|
28 | 28 | Request = typing.TypeVar("Request")
|
| 29 | +ArgMapCallable = typing.Callable[[Request], ma.Schema] |
29 | 30 | ArgMap = typing.Union[
|
30 |
| - ma.Schema, |
31 |
| - type[ma.Schema], |
32 |
| - typing.Mapping[str, typing.Union[ma.fields.Field, type[ma.fields.Field]]], |
33 |
| - typing.Callable[[Request], ma.Schema], |
| 31 | + ma.Schema, type[ma.Schema], typing.Mapping[str, ma.fields.Field], ArgMapCallable |
34 | 32 | ]
|
35 | 33 |
|
36 | 34 | ValidateArg = typing.Union[None, typing.Callable, typing.Iterable[typing.Callable]]
|
@@ -328,15 +326,11 @@ def _get_schema(self, argmap: ArgMap, req: Request) -> ma.Schema:
|
328 | 326 | elif isinstance(argmap, type) and issubclass(argmap, ma.Schema):
|
329 | 327 | schema = argmap()
|
330 | 328 | elif isinstance(argmap, collections.abc.Mapping):
|
331 |
| - if isinstance(argmap, dict): |
332 |
| - argmap_dict = argmap |
333 |
| - else: |
334 |
| - argmap_dict = dict(argmap) |
| 329 | + argmap_dict = argmap if isinstance(argmap, dict) else dict(argmap) |
335 | 330 | schema = self.schema_class.from_dict(argmap_dict)()
|
336 | 331 | elif callable(argmap):
|
337 |
| - # type-ignore because mypy seems to incorrectly deduce the type |
338 |
| - # as `[def (Request) -> Schema] | object` |
339 |
| - schema = argmap(req) # type: ignore[call-arg, assignment] |
| 332 | + argmap_callable = typing.cast(ArgMapCallable, argmap) |
| 333 | + schema = argmap_callable(req) |
340 | 334 | else:
|
341 | 335 | raise TypeError(f"argmap was of unexpected type {type(argmap)}")
|
342 | 336 | return schema
|
@@ -585,9 +579,8 @@ def greet(querystring_args):
|
585 | 579 | # Optimization: If argmap is passed as a dictionary, we only need
|
586 | 580 | # to generate a Schema once
|
587 | 581 | if isinstance(argmap, typing.Mapping):
|
588 |
| - if not isinstance(argmap, dict): |
589 |
| - argmap = dict(argmap) |
590 |
| - argmap = self.schema_class.from_dict(argmap)() |
| 582 | + argmap_dict = argmap if isinstance(argmap, dict) else dict(argmap) |
| 583 | + argmap = self.schema_class.from_dict(argmap_dict)() |
591 | 584 |
|
592 | 585 | if arg_name is not None and as_kwargs:
|
593 | 586 | raise ValueError("arg_name and as_kwargs are mutually exclusive")
|
|
0 commit comments