Skip to content

feat(types): add TypeVars / method generics typing - #5167

Merged
henryiii merged 54 commits into
pybind:masterfrom
InvincibleRMC:typevars
Jun 25, 2024
Merged

feat(types): add TypeVars / method generics typing#5167
henryiii merged 54 commits into
pybind:masterfrom
InvincibleRMC:typevars

Conversation

@InvincibleRMC

@InvincibleRMCInvincibleRMC commented Jun 14, 2024

Copy link
Copy Markdown
Contributor

Description

Add supports for Typevars.
Requires __cpp_nontype_template_parameter_class feature.

To create the new a new typing::TypeVar use the following syntax.

typedef py::typing::TypeVar<"T"> TypeVarT;

This allows use to create type annotations like the following.

deffoo(arg: list[T]) ->T: ...

Suggested changelog entry:

Adds support for python typing ``TypeVar`` to annotations.

@InvincibleRMC

InvincibleRMC commented Jun 14, 2024

Copy link
Copy Markdown
ContributorAuthor

TypeVars seem to be working for all versions of compilers with C++20 that support string literals in templates. Now I ask should the feature be dropped to maintain capability with older C++ standards. Or is there precedent for C++ version guarding features?

@InvincibleRMC
InvincibleRMC marked this pull request as ready for review June 14, 2024 17:24
@InvincibleRMCInvincibleRMC changed the title typevar prototypeAdd TypeVars / method genericsJun 14, 2024
@InvincibleRMCInvincibleRMC changed the title Add TypeVars / method genericsAdd TypeVars / method generics typingJun 14, 2024
@rwgk
rwgk marked this pull request as draft June 14, 2024 19:02
@rwgk

rwgk commented Jun 14, 2024

Copy link
Copy Markdown
Collaborator

I converted this to Draft since this looks like you're still working on it.

Is the idea to add ifdef(C++20)?

@InvincibleRMC

Copy link
Copy Markdown
ContributorAuthor

Yeah. I wasn't sure if that was fine since some projects want equal support for different C++ standards.

@rwgkrwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@henryiii wrote:

Not sure if we have any C++20 features yet.

Me too; I don't think so. I'm only aware of accommodations for C++20 (module_ and PYBIND11_CPP20).

But I cannot think of reasons against adding conditional support for C++20 features. Are there concerns I'm missing?

Comment threadinclude/pybind11/typing.h Outdated
Comment threadinclude/pybind11/typing.h Outdated
Comment threadinclude/pybind11/typing.h
Comment threadtests/test_pytypes.cpp
Comment threadtests/test_pytypes.py Outdated
@InvincibleRMC
InvincibleRMC marked this pull request as draft June 15, 2024 14:52
Comment threadinclude/pybind11/typing.h Outdated
Comment threadinclude/pybind11/typing.h Outdated
@InvincibleRMC
InvincibleRMC marked this pull request as ready for review June 15, 2024 16:11

@rwgkrwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me.

Could you please make a pass through the PR description (quick and simple, I think): Is "Prototyping" still what you have in mind? And we need a Suggested changlog entry.

@InvincibleRMC
InvincibleRMC marked this pull request as draft June 16, 2024 22:32
@InvincibleRMC

InvincibleRMC commented Jun 16, 2024

Copy link
Copy Markdown
ContributorAuthor

A potential problem I discovered in testing is stub generators like mypy's stubgen or pybind11-stubgen will need to recognize TypeVars from the compiled annotations to create the TypeVar objects themselves to then be used. I'm not sure of the best way of notifying the stub generators to create TypeVar instantiations. Another option would be to modify the order resolution of of the annotation generation to use the Python3.12 inline TypeVar declaration.

Python < 3.12

fromtypingimportTypeVarT=TypeVar('T')
deffoo(arg0: T) ->T: ...

Python >= 3.12

deffoo[T](arg0: T) ->T: ...

@rwgk

rwgk commented Jun 18, 2024

Copy link
Copy Markdown
Collaborator

A potential problem I discovered in testing is stub generators like mypy's stubgen or pybind11-stubgen will need to recognize TypeVars from the compiled annotations to create the TypeVar objects themselves to then be used.

I only have a very sketchy idea of what mypy's stubgen is doing. But while working on #4888 I learned that stubgen has fallback mechanisms if it cannot process some annotations.

Do you believe this PR will make things worse?

@InvincibleRMC

Copy link
Copy Markdown
ContributorAuthor

It is not ideal, but does not make anything worse. If that is acceptable I'm fine merging it in.

@InvincibleRMC
InvincibleRMC marked this pull request as ready for review June 18, 2024 13:58
@rwgk

rwgk commented Jun 18, 2024

Copy link
Copy Markdown
Collaborator

It is not ideal, but does not make anything worse. If that is acceptable I'm fine merging it in.

I'm for merging this: it's clearly making pybind11 more complete/correct, with very little extra code. — The very minimal two-way collaboration between pybind11 and mypy stubgen is a bigger problem that hopefully will get addressed down the road (something like #4888).

@henryiii What's your opinion?

@henryiii
henryiii merged commit aebcd70 into pybind:masterJun 25, 2024
@henryiiihenryiii changed the title Add TypeVars / method generics typingfeat(types): add TypeVars / method generics typingJun 25, 2024
@github-actionsgithub-actionsBot added the needs changelog Possibly needs a changelog entry label Jun 25, 2024
@henryiiihenryiii removed the needs changelog Possibly needs a changelog entry label Jun 26, 2024
@InvincibleRMC
InvincibleRMC deleted the typevars branch July 1, 2024 13:47
Sign up for freeto 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.

3 participants

@InvincibleRMC@rwgk@henryiii