[BUG]: Argument conversion in dispatcher assumes sequence protocol when casting to list[<something>]
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.13.6
Problem description
When a derived class implements __getitem__ and there exists an overloaded function that can accept a vector of base instances or the derived instance the argument conversion in the dispatcher logic will try to call derived.__getitem__.
If derived does not implement __len__ this will fail outright, otherwise derived.__getitem__ will be called with integer indices from 0 to len(derived). If derived.__getitem__ does not accept integers (e.g. because it mimics a map) this will also fail.
The resulting error message is extremely confusing because the trace will show that the execution failed at the function call but the message will say that __getitem__ was involved with derived and 0 but supported types are ...
A potential fix / workaround is to manually break the sequence protocol for derived by setting its tp_as_sequence->sq_item to NULL as this will cause it to fail PySequence_Check and avoid this conversion path entirely.
Adding noconvert to the vector argument does not help because this failure already happens in the initial non-converting pass in the dispatcher.
Reproducible example code
I have created a minimal example here: https://github.com/IljaManakov/pybind_arg_cast_issue_repro
Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproducer linked in the issue and confirm the failure with pybind11 2.13.6. Trace the dispatcher’s argument-conversion path for overloaded functions accepting a vector of base instances and a derived instance. Done should prevent the unintended sequence-protocol lookup and provide a clear conversion result without breaking valid conversions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100