Update Modules/_docstrings.py
Browse files- Modules/_docstrings.py +13 -1
Modules/_docstrings.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import inspect
|
|
|
|
|
|
|
| 4 |
from typing import Any, Annotated, get_args, get_origin, get_type_hints, Union
|
| 5 |
|
| 6 |
|
|
@@ -8,7 +10,8 @@ def _typename(tp: Any) -> str:
|
|
| 8 |
"""Return a readable type name from a type or annotation."""
|
| 9 |
try:
|
| 10 |
# Unwrap Optional[T] -> T
|
| 11 |
-
|
|
|
|
| 12 |
args = [a for a in get_args(tp) if a is not type(None)]
|
| 13 |
if len(args) == 1:
|
| 14 |
return _typename(args[0])
|
|
@@ -64,7 +67,16 @@ def autodoc(summary: str | None = None, returns: str | None = None, *, force: bo
|
|
| 64 |
# include_extras=True to retain Annotated metadata
|
| 65 |
hints = get_type_hints(func, include_extras=True, globalns=getattr(func, "__globals__", None))
|
| 66 |
except Exception:
|
|
|
|
| 67 |
hints = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
sig = inspect.signature(func)
|
| 70 |
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import inspect
|
| 4 |
+
import sys
|
| 5 |
+
import types
|
| 6 |
from typing import Any, Annotated, get_args, get_origin, get_type_hints, Union
|
| 7 |
|
| 8 |
|
|
|
|
| 10 |
"""Return a readable type name from a type or annotation."""
|
| 11 |
try:
|
| 12 |
# Unwrap Optional[T] -> T
|
| 13 |
+
origin = get_origin(tp)
|
| 14 |
+
if origin is Union or (sys.version_info >= (3, 10) and origin is types.UnionType):
|
| 15 |
args = [a for a in get_args(tp) if a is not type(None)]
|
| 16 |
if len(args) == 1:
|
| 17 |
return _typename(args[0])
|
|
|
|
| 67 |
# include_extras=True to retain Annotated metadata
|
| 68 |
hints = get_type_hints(func, include_extras=True, globalns=getattr(func, "__globals__", None))
|
| 69 |
except Exception:
|
| 70 |
+
# Fallback: try to evaluate annotations manually if they are strings
|
| 71 |
hints = {}
|
| 72 |
+
sig = inspect.signature(func)
|
| 73 |
+
for name, param in sig.parameters.items():
|
| 74 |
+
if isinstance(param.annotation, str):
|
| 75 |
+
try:
|
| 76 |
+
val = eval(param.annotation, getattr(func, "__globals__", {}))
|
| 77 |
+
hints[name] = val
|
| 78 |
+
except Exception:
|
| 79 |
+
pass
|
| 80 |
|
| 81 |
sig = inspect.signature(func)
|
| 82 |
|