Skip to content

Initial support for PEP 695 type aliases #13508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mmatous
Copy link

@mmatous mmatous commented Apr 25, 2025

Purpose

Hi, this PR includes support for documenting PEP 695 type aliases. The main problem was that while looking for docstrings, Sphinx's parser didn't recognize ast.TypeAlias as visitable (for doc comment, missing visit_TypeAlias()) and TypeAliasType object wasn't recognized as something documentable when encountering docstring in visit_Expr() (for docstrings).

The rest of it is mostly just me guessing how stuff should be rendered into ReST and trying to ensure Python 3.11 compatibility.

I put the relevant autodoc parts into ClassDocumenter since that's where code for other type alias variants lives.

Couple of caveats:

  • Type aliases in signatures don't get cross-linked in HTML. I could use some pointers here. I think the ReST is mostly fine and the problem lies in HTML gen.? What should I do about that?

  • Generic type aliases do not get rendered as such. E.g. type A[T] = list[T] yields only type A = list[T] in resulting HTML. This seems expected, since there is no supported syntax for type params in py:type, unlike py:method

  • Does not include support for PEP 695 type parameters in generic classes (class ClassA[T: str]:) or functions/methods (def func[T](a: T, b: T) -> T:). I thought about it briefly and it seems more complicated than I'm willing to tackle rn.

  • How should I go about ruff accepting the test file? Add exemption to pyproject? SyntaxError bc of targeting Py3.11 can't be suppressed.

References

@mmatous mmatous force-pushed the pep695 branch 4 times, most recently from 89caa94 to ef7dbce Compare April 26, 2025 00:25
@AA-Turner
Copy link
Member

Thanks Martin! Please could you resolve conflicts?

cc also @picnixz if you want to have a look.

A

@mmatous
Copy link
Author

mmatous commented May 14, 2025

done

@picnixz picnixz self-requested a review May 15, 2025 17:05
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I'm surprised by how easy it was to add this support...

@@ -1693,7 +1698,7 @@ def can_document_member(
cls: type[Documenter], member: Any, membername: str, isattr: bool, parent: Any
) -> bool:
return isinstance(member, type) or (
isattr and isinstance(member, NewType | TypeVar)
isattr and isinstance(member, NewType | TypeVar | TypeAliasType)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to create a private helper say def _is_typelike(s): return isinstance(s, NewType | TypeVar | TypeAliasType)? that way, we can avoid having to add more and more types in the union in the future.

Copy link
Author

Choose a reason for hiding this comment

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

Created the helper as static method. To make mypy happy I had to delete type-hint for cls (or use type: ignore). I'm guessing it's fine and the reason for cls having a type is because the code predates some tooling feature like cls type autodetect or Documenter itself predating ABCs and abstractmethods?
In any case, it didn't seem to break anything.

class Foo:
"""This is class Foo."""

...
Copy link
Member

Choose a reason for hiding this comment

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

Let's address ruff's issues here (remove "...")

type Pep695Alias = Foo
"""This is PEP695 type alias."""

type Pep695AliasC = dict[str, Foo] #: This is PEP695 complex type alias with doc comment.
Copy link
Member

@picnixz picnixz May 15, 2025

Choose a reason for hiding this comment

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

Change the ruff configuration so that this test file requires python 3.12 as the target version: https://docs.astral.sh/ruff/settings/#per-file-target-version.

reason='PEP 695 is Python 3.12 feature. Older versions fail to parse source into AST.',
)
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_Pep695Alias(app):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_autodoc_Pep695Alias(app):
def test_autodoc_pep695_type_alias(app):

@picnixz
Copy link
Member

picnixz commented May 15, 2025

Type aliases in signatures don't get cross-linked in HTML. I could use some pointers here

That's probably because of how we parse the signature. This is a part I wrote so I could try to help you later this week (I'm sorry I got disconnected quite a lot with Sphinx now that I'm contributing directly to CPython).

type A[T] = list[T] yields only type A = list[T] in resulting HTML. This seems expected, since there is no supported syntax for type params in py:type, unlike py:method

Again that's me who wrote that part for method and the rest. We didn't have the type directive yet and I think I forgot to update this. There's an open issue somewhere where I said I'll take care of it but I forgot. My bad.

Does not include support for PEP 695 type parameters in generic classes (class ClassA[T: str]:)

I'm surprised: the parser actually supports this. But cross-referencing may not be entirely supported though.

@mmatous mmatous force-pushed the pep695 branch 3 times, most recently from 042c7ec to 74e759d Compare May 21, 2025 01:55
@mmatous
Copy link
Author

mmatous commented May 21, 2025

Updated with requested changes.

This is a part I wrote so I could try to help you later this week

The links would be nice. I'd like to incorporate them in this PR provided the necessary changes are small enough. If it's more work I'll still make them happen, but I wouldn't want this PR to be bogged down because of it. Just point me in the right direction for now. I'll figure it out.

@@ -1942,6 +1951,11 @@ def add_directive_header(self, sig: str) -> None:
):
self.add_line(' :canonical: %s' % canonical_fullname, sourcename)

if isinstance(self.object, TypeAliasType):
Copy link

Choose a reason for hiding this comment

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

typing_extensions.TypeAliasType is a different object than typing.TypeAliasType (even on 3.12 where both exist).

See litestar-org/litestar#3982 (comment) for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants