Repository navigation
Conversation
|
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. |
|
@henryiii, to respond to your comment, I asked codex for advice: I’d move forward with the integer-only #6291 once Henry marks it ready and CI passes. Keep #6286 open while he looks at consistency across pybind11, then close it as superseded if #6291 lands. Slack discussion · Draft #6291
|
My issue was not really with that, my issue was you should not make a dictionary with keys you can't recreate unless you are doing something really weird. If you can't recreate the key, but must keep the original key because it's hashed by memory location, then you are already doing something pretty weird, and you should know what you are doing. 🤖 AI text below 🤖 #6286 (any castable key) is more consistent with the rest of pybind11. #6291 (integers only) has no precedent in the object API. Why #6286 fits
Where #6291 has precedent
Trade-off rwgk's objection to #6286 is cost, not consistency: If consistency is the deciding criterion, take #6286. #6291 is the smaller step if you want to settle the review, and it can be widened to #6286 later without a break. |
|
I would rather just support any castable type instead of adding every "safe" class one at a time, but yes, |
Replace the integer overload of object_api::operator[] with a constrained template overload that converts the key with pybind11::cast. It also covers integers. Integers wider than unsigned long long are still rejected, because cast would truncate them. The handle, object&& and const char* overloads are unchanged. Closes pybind#1309. Assisted-by: ClaudeCode:claude-opus-5-5
76d445f to
a0e7379
Compare
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[2.5],d[std::string("s")],d['c']andd[std::make_pair(1, 2)]. This is an alternative to #6316, which adds onlystd::stringandstd::string_view.The change replaces the integer overload from #6291 with one constrained template overload of
object_api::operator[]that converts the key withpy::cast. Integer keys use this overload too. Integers wider thanunsigned long long(for exampleunsigned __int128) are still rejected at compile time, becausepy::castwould truncate them.booland character keys are now accepted and becomeTrue/ a one-characterstr, the same aspy::cast.The
handle,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.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[2.5]ord[std::string("s")]).