pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.08k stars 2.05k forks source link

feat(types): add `Union` and `Optional` to typing.h #5165

Closed InvincibleRMC closed 2 weeks ago

InvincibleRMC commented 2 weeks ago

Description

Add Union and Optional for better static typing on the python side.

Suggested changelog entry:

``Union`` and ``Optional`` were added to ``pybind11/typing.h``
rwgk commented 2 weeks ago

Looks useful.

I just converted this to a Draft PR.

Could you please work on the GitHub Actions failures? Then click Ready for review and maybe tag me.

InvincibleRMC commented 2 weeks ago

Will do. It seems to be falling into the default handle_type_name rather than the special case describe in typing.h but, will keep investigating.

rwgk commented 2 weeks ago

Maybe you just something like this?

https://github.com/pybind/pybind11/pull/5073/files

InvincibleRMC commented 2 weeks ago

The solution was to inherit from one of the types found in cast.h 900-1035. I wasn't super sure on the best type to inherit from but, from what I could tell type::type made some sense. If there is some better type to inherit from let me know.

InvincibleRMC commented 2 weeks ago

@rwgk the CI is now passing.

InvincibleRMC commented 2 weeks ago

With further testing on actual code this does not seem to currently work.

InvincibleRMC commented 2 weeks ago

@rwgk With some updated testing and switching to inheriting from py::object it seems to be all working.