| 1 |
import asyncio |
| 2 |
import inspect |
| 3 |
import re |
| 4 |
from sys import version_info |
| 5 |
from typing import Any, Callable, ForwardRef, List, Set |
| 6 |
|
| 7 |
from django.urls import register_converter |
| 8 |
from django.urls.converters import UUIDConverter |
| 9 |
from pydantic._internal._typing_extra import eval_type_lenient as evaluate_forwardref |
| 10 |
|
| 11 |
from ninja.types import DictStrAny |
| 12 |
|
| 13 |
__all__ = [ |
| 14 |
"get_typed_signature", |
| 15 |
"get_typed_annotation", |
| 16 |
"make_forwardref", |
| 17 |
"get_path_param_names", |
| 18 |
"is_async", |
| 19 |
] |
| 20 |
|
| 21 |
|
| 22 |
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: |
| 23 |
"Finds call signature and resolves all forwardrefs" |
| 24 |
signature = inspect.signature(call) |
| 25 |
globalns = getattr(call, "__globals__", {}) |
| 26 |
typed_params = [ |
| 27 |
inspect.Parameter( |
| 28 |
name=param.name, |
| 29 |
kind=param.kind, |
| 30 |
default=param.default, |
| 31 |
annotation=get_typed_annotation(param, globalns), |
| 32 |
) |
| 33 |
for param in signature.parameters.values() |
| 34 |
] |
| 35 |
typed_signature = inspect.Signature(typed_params) |
| 36 |
return typed_signature |
| 37 |
|
| 38 |
|
| 39 |
def get_typed_annotation(param: inspect.Parameter, globalns: DictStrAny) -> Any: |
| 40 |
annotation = param.annotation |
| 41 |
if isinstance(annotation, str): |
| 42 |
annotation = make_forwardref(annotation, globalns) |
| 43 |
return annotation |
| 44 |
|
| 45 |
|
| 46 |
def make_forwardref(annotation: str, globalns: DictStrAny) -> Any: |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
forward_ref = ForwardRef(annotation) |
| 52 |
return evaluate_forwardref(forward_ref, globalns, globalns) |
| 53 |
|
| 54 |
|
| 55 |
def get_path_param_names(path: str) -> Set[str]: |
| 56 |
"""turns path string like /foo/{var}/path/{int:another}/end to set {'var', 'another'}""" |
| 57 |
return {item.strip("{}").split(":")[-1] for item in re.findall("{[^}]*}", path)} |
| 58 |
|
| 59 |
|
| 60 |
def is_async(callable: Callable[..., Any]) -> bool: |
| 61 |
|
| 62 |
if version_info >= (3, 12): |
| 63 |
return inspect.iscoroutinefunction(callable) |
| 64 |
else: |
| 65 |
return asyncio.iscoroutinefunction(callable) |
| 66 |
|
| 67 |
|
| 68 |
def has_kwargs(func: Callable[..., Any]) -> bool: |
| 69 |
for param in inspect.signature(func).parameters.values(): |
| 70 |
if param.kind == param.VAR_KEYWORD: |
| 71 |
return True |
| 72 |
return False |
| 73 |
|
| 74 |
|
| 75 |
def get_args_names(func: Callable[..., Any]) -> List[str]: |
| 76 |
"returns list of function argument names" |
| 77 |
return list(inspect.signature(func).parameters.keys()) |
| 78 |
|
| 79 |
|
| 80 |
class UUIDStrConverter(UUIDConverter): |
| 81 |
"""Return a path converted UUID as a str instead of the standard UUID""" |
| 82 |
|
| 83 |
def to_python(self, value: str) -> str: |
| 84 |
return value |
| 85 |
|
| 86 |
|
| 87 |
register_converter(UUIDStrConverter, "uuidstr") |