Skip to content

Docment renderers collapse parameterized annotations to their unparameterized type #910

Description

@civvic

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions