Docment-based signature rendering loses type parameters from generic annotations.
For example, list[str] is rendered as list, and tuple[str, int] is rendered as tuple.
Reproduction
from fastcore.docments import sig2str
def f(
items:list[str],
) -> tuple[str, int]:
pass
print(sig2str(f))
Actual output:
def f(
items:list
)->tuple:
Expected output:
def f(
items:list[str]
)->tuple[str,int]:
Environment
- fastcore: 2.2.13
- Branch:
main
Scope
The issue affects multiple docment renderers, including:
DocmentText
DocmentList
DocmentTbl
Likely cause
The renderers use _maybe_nm() for annotations:
def _maybe_nm(o):
if o == inspect._empty: return ''
return o.__name__ if hasattr(o, '__name__') else str(o)
Parameterized generic aliases expose the unparameterized origin through __name__:
tuple[str, int].__name__
# 'tuple'
By comparison, annotation-aware formatting preserves the parameters:
inspect.formatannotation(tuple[str, int])
# 'tuple[str, int]'
Annotation formatting should preserve generic parameters without changing the existing formatting of ordinary classes or default values.
Docment-based signature rendering loses type parameters from generic annotations.
For example,
list[str]is rendered aslist, andtuple[str, int]is rendered astuple.Reproduction
Actual output:
Expected output:
Environment
mainScope
The issue affects multiple docment renderers, including:
DocmentTextDocmentListDocmentTblLikely cause
The renderers use
_maybe_nm()for annotations:Parameterized generic aliases expose the unparameterized origin through
__name__:By comparison, annotation-aware formatting preserves the parameters:
Annotation formatting should preserve generic parameters without changing the existing formatting of ordinary classes or default values.