Skip to content

Proper pep695 generic function docstrings #5743

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 4 commits into
base: master
Choose a base branch
from

Conversation

InvincibleRMC
Copy link
Contributor

Description

Add typevar_record to hold information about function type variables. Use these type variables to update the generated docstrings as according to pep695.

To be used in conjuction with the typevar types introduced in #5167.

Suggested changelog entry:

  • Introduce typevar_record to add pep695 generics to function docstrings.

@InvincibleRMC
Copy link
Contributor Author

@timohl if you have any comments/suggestions please let me know.

@timohl
Copy link
Contributor

timohl commented Jun 25, 2025

I should find time to review the code in more detail during the weekend, but I already skimmed through the tests and the new syntax looks nice. :)

One thing that came to my mind though is backwards compatibility.
PEP695 is added in Python 3.12.
Have you tested how stubgen and type checkers react in older versions, or any plans on how to make this compatible?

Also, what is the impact of the check for PYBIND11_HAS_OPTIONAL?
The test is guarded by PYBIND11_TYPING_H_HAS_STRING_LITERAL.
In the end, it would be nice to add tests for setups where the requirements of this feature are not met.

@InvincibleRMC
Copy link
Contributor Author

InvincibleRMC commented Jun 25, 2025

I should find time to review the code in more detail during the weekend, but I already skimmed through the tests and the new syntax looks nice. :)

One thing that came to my mind though is backwards compatibility. PEP695 is added in Python 3.12. Have you tested how stubgen and type checkers react in older versions, or any plans on how to make this compatible?

From what I can tell mypy's stubgen or pybind11-stubgen currently ignore the inside of pep695. Mypy has a todo.

Mypy will fail if running in version less than 3.12

def foo[T](a: T) -> T: ...  # invalid syntax

However, mypy could when implementing support generate pre-pep695 syntax in stubs when ran on older python versions.

import typing

T = typing.TypeVar("T")
def foo(a: T) -> T: ... # valid

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.

2 participants