Skip to content

Fix self type hint in properties and add type hinted handle and self - #6285

Open
gentlegiantJGC wants to merge 19 commits into
pybind:masterfrom
gentlegiantJGC:fix-self-type-hint
Open

gentlegiantJGC wants to merge 19 commits into
pybind:masterfrom
gentlegiantJGC:fix-self-type-hint

Conversation

@gentlegiantJGC

@gentlegiantJGC gentlegiantJGC commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Some property constructors for classes using smart_holder take self as a handle which gets converted to object in the type hints.

This adds a type hinted variant of handle and object that accept a C++ type which is used as the type hint but are otherwise interchangeable with handle and object. This is used to correctly type hint the self argument in def_read(only|write)

#4961 requested a type hinted version of py::object

Suggested changelog entry:

  • Fix self type hint in properties and add type hinted handle and self

@gentlegiantJGC
gentlegiantJGC marked this pull request as ready for review November 17, 2026 21:22
@gentlegiantJGC gentlegiantJGC changed the title Fix self type hint in properties Fix self type hint in properties and add type hinted handle and self Nov 18, 2026
@henryiii

Copy link
Copy Markdown
Collaborator

I ran a review on this. The key finding is the test doesn't actually test the change, and passes on master too. That needs to be fixed.

🤖 AI text below 🤖

The /code-review run finished with six findings. I have not checked them myself, and I have not changed any code.

Important

  1. The new docstring test does not test the fix (tests/test_methods_and_attributes.cpp:477). TestProperties2 uses py::class_, so def_readonly/def_readwrite never reach the typing::Handle<T> lambdas you edited in pybind11.h (lines 1124, 1164, 1182, 1224). The test also passes on master. The default CI runs reach those lines only in the job that makes smart_holder the default. Fix: bind the class with py::classh and confirm the test fails without the pybind11.h change.

Pre-existing
2. Smart-holder property getters can segfault when self has the wrong type (pybind11.h:1124 and the same pattern at the other three lines). The load does no type check, the loader result is not checked, and the code dereferences a null shared_ptr. For example, TestProperties2.def_readonly_raw_ptr.fget(42) crashes. The old code had the same gap, but the hint now says self is T.

Nits
3. typing::Handle<A> and typing::Object<A> accept any object when they load (typing.h:40). Because of this, overload dispatch ignores the type in the hint. Either document that the hint is not checked, or add a check.
4. The new test functions in tests/test_pytypes.cpp:920 return a null handle or object. Calling them raises an error. Nothing tests that a value passes through them. Return the argument and add a call assert.
5. py::isinstance<typing::Handle<T>>(h) always returns false, because Handle does not derive from object. Add a check_, or delete the overload as is done for handle.
6. Some commit messages on the branch do not follow conventional commits ("Fix lint error", "Fix template", "Add tests", and others). Reword them, or make sure the squash-merge title is conventional.

@gentlegiantJGC

Copy link
Copy Markdown
Contributor Author
  1. Do you want me to add a custom classh test or is the job that swaps class_ for classh sufficient?
  2. Pre-existing issue. The correct type hint should stop people passing in the wrong type.
  3. This is the same behaviour as the base handle and object
  4. These were only added to test the annotations.
  5. Fixed

@henryiii

Copy link
Copy Markdown
Collaborator

Oh, that's tricky. I'd personally like to get rid of that job someday, and just build both variations into the tests that touch them. So I think if this is smart-holder specific, making only smart holder makes sense. But if you like it running on both, that job isn't going anywhere soon and all cases like this would have to be handled anyway, so keeping this way is fine too.

@henryiii

Copy link
Copy Markdown
Collaborator

Rebased to pick up #6290.

This branch has not been deployed

No deployments
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