Skip to content

Conversation

@dstansby
Copy link
Contributor

@dstansby dstansby commented Dec 4, 2023

Subject: Add metadata to Annotated types

Feature or Bugfix

  • Feature

Purpose

This adds metadata to Annotated types.

Detail

Relates

Fixes #11773

Copy link
Contributor

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the parameter x: Annotated[int, Ge(0), Lt(10)] (using annotated-types). Rendering this as x: int[Ge(0), Lt(10)] would be absurd.

I therefore think that we should render typing.Annotated types in documentation with f"Annotated[{stringify_annotation(T)}, {', '.join(repr(x) for x in metadata)}].

@AA-Turner AA-Turner changed the title Add metadata to Annotated types Display typing.Annotated metadata in the Python domain Jul 13, 2024
@AA-Turner AA-Turner merged commit 159c267 into sphinx-doc:master Jul 13, 2024
@AA-Turner
Copy link
Member

Thanks all!

A

@AA-Turner AA-Turner added this to the 7.4.0 milestone Jul 14, 2024
@2bndy5
Copy link

2bndy5 commented Jul 16, 2024

I think I found a regression about this. We're using the Annotated class + pydantic to validate types of a user-defined config in conf.py...

CSSClassType = Annotated[str, pydantic.AfterValidator(nodes.make_id)]

class CustomAdmonitionConfig(pydantic.BaseModel):
    title: Annotated[Optional[str], pydantic.Field(validate_default=True)] = None
    classes: List[CSSClassType] = []

But autodoc-ing CustomAdmonitionConfig's title and classes attributes results in errors trying to reference the metadata:

/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/sphinx_immaterial/custom_admonitions.py:docstring of sphinx_immaterial.custom_admonitions.CustomAdmonitionConfig.title:1: WARNING: py:obj reference target not found: typing.Annotated[str | None, FieldInfo(annotation=NoneType, required=True, validate_default=True)]

/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/sphinx_immaterial/custom_admonitions.py:docstring of sphinx_immaterial.custom_admonitions.CustomAdmonitionConfig.classes:1: WARNING: py:obj reference target not found: typing.List[~typing.Annotated[str, AfterValidator(func=<function make_id at 0x7fd2a7b5cae0>)]]

I think lesson here is that not all metadata is compatible with autodoc. Is there a way to disable it?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better rendering of Annotated types

5 participants