Repository navigation
Conversation
Add a constrained template overload of object_api::operator[] that converts the key with pybind11::cast. The handle, object&& and const char* overloads are unchanged. The literal 0 is now the integer key 0. Before, it converted to a null const char*. Closes pybind#1309. Assisted-by: ClaudeCode:claude-fable-5-2
|
Here's ideas for a followup. I do wonder if this should not close 1309 until getattr is also supported... 🤖 AI text below 🤖 These are the entry points that still accept only Python objects (or
These already cast, so they need no change:
I recommend that #6286 stays as it is. The |
rwgk
left a comment
There was a problem hiding this comment.
My main concern is that obj[key] can hide a fair amount of work for a small convenience.
With the STL caster, a vector used as an index becomes a fresh Python list on every access, with every element converted. That happens as soon as the accessor is created, even though the lookup is lazy. I’d prefer to see that conversion at the call site. Conversion path
There’s also a less obvious identity issue. In a small reproducer with a bound C++ key that uses Python’s default identity hashing, d[key] = 42 followed by d[key] raised KeyError. The two casts created distinct Python objects. Casting once and reusing the Python key worked.
I see the consistency with dict::contains and dict::get. For operator[], could we start with integral keys? obj[1] is useful, and obj[0] fixes the null C-string trap. I’d keep py::cast(key) explicit for compound and user-defined keys, where the cost and identity of the converted key can matter.
|
I'm trying an int-only version, in #6291. But I don't follow:
But keys of dictionaries are supposed to be hashable. Most places in pybind11 autocast, I think, so I still think this is fairly sensible. |
The key difference here is that it's a constrained template, so that the old fast-paths can keep working. Every one of the three current specialization does something useful:
const char *avoids alocation and copy,py::handlecan't be done generally (would take/drop refcounts), and evenpy::object &&avoids a borrow and move.I didn't do any of the other accessors, but
getattrand maybesetattrfor consistency seems reasonable too.This fixes a bug: if you pass
obj[0], before this PR that0matchedconst char *asnullptr(common C++ annoyance).🤖 AI text below 🤖
Description
Closes #1309. Supersedes and closes #4004 (and #4007). Thanks to @woodychow and @cielavenir for the original proposal and tests.
obj[key]now accepts any C++ type thatpy::castcan convert, for exampled[1],d[2.5],d[std::string("s")],d[std::make_pair(1, 2)]ando[1][2].The change adds one constrained template overload of
object_api::operator[]. Thehandle,object &&andconst char *overloads are unchanged, so existing code resolves to the same overload at the same cost. #4004 replaced all three with a catch-allT &&. That sends string literals throughpy::cast, which builds a temporarystd::string, and it loses the implicit conversions tohandle(for example from a type withoperator PyObject *()).py::tuple,py::listandpy::sequencedeclare their ownoperator[], which hides the new overload.t[1]on those types still uses thesize_taccessors.Behaviour change:
obj[0]compiled before, because the literal0converted to a nullconst char *and went topy::str(nullptr). It is now the integer key 0.The segfault seen in #4007 was in the tests. The lambdas returned
l1[1]with a deduced return type, which is alist_accessor. An accessor holds a non-owning handle to the container and reads the item lazily, so the read occurred after the list was destroyed. This also occurs on master.Suggested changelog entry:
operator[]onpy::object,py::dictand accessors now accepts any key type thatpy::castcan convert (for exampled[1]). The literal0is now the integer key 0; before, it was a nullconst char *.