Initial support for PEP 695 type aliases #13508
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 invisit_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 onlytype A = list[T]
in resulting HTML. This seems expected, since there is no supported syntax for type params inpy:type
, unlikepy: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