Open abarnert opened 6 years ago
This is a good discussion but it could alter the interface enough that we aren't comfortable putting in 1.0. The 2.x branch is internally improving on the 1.x branch and making maintenance and certain features easier, but its interface is still compliant with 1.0. So this is slated for 3.x unless there is great need for it.
Admittedly, this is an unspoken design constraint based on dpath's original (way back when) use-case; processing JSON, which does not have tuples. We did not actually consider all possible python objects for traversal, though we did try to remain conscious of its use case outside of JSON specific use cases.
Blocked by #136
dpath
(after #35 was fixed) distinguishes between mappings, sequences, and other objects usingMutableMapping
andMutableSequence
, instead ofMapping
andSequence
. This means that it can't search within immutable sequences, like tuples:What about functions that need to mutate the input, like
set
oradd
? I think that even there, you want to path through usingMapping
andSequence
. You may need to check at the "bottom" level, where you do the update/insert, that it's actually aMutableMapping
orMutableSequence
, but I'm not sure even that is necessary—and if it is, you probably still want to checkMapping
orSequence
first, so you correctly report that a tuple is immutable instead of reporting that it's not something you can path into.Consider:
(Also, the text of the
TypeError
is a bit weird—the problem is pathing into elements of()
, or of typetuple
, not of type()
.)This does raise the question of whether you want to allow pathing into strings, since
isinstance(str, Sequence)
. If you don't want that, you'd need to explicitly exclude it with a secondisinstance
which checks(unicode, bytearray)
for 2.x,(bytes, bytearray)
for 3.0-3.4, andByteString
for 3.5+. But maybe it's fine fordpath.util.get(['abc'], '0/0')
return return'a'
instead of raising aTypeError
about being unable to path into strings.