tadeja opened a new issue, #55866:
URL: https://github.com/apache/arrow/issues/55866
### Describe the bug, including details regarding any error messages,
version, and platform.
Since #45818 (issue #45653) `MapScalar` inherits from
`collections.abc.Mapping` so `isinstance(s, Mapping)` is true. Moreover, it is
a `Sequence` of (key, value) pairs via `ListScalar`, and those interfaces
conflict, causing several standard mapping operations to fail or return
Sequence-like results instead of Mapping-like results:
```python
from collections.abc import Mapping
import pyarrow as pa
s = pa.scalar([("a", 1), ("b", 2)], type=pa.map_(pa.string(), pa.int8()))
assert isinstance(s, Mapping)
# These results are mapping-like, so OK:
s["a"] # result: <pyarrow.Int8Scalar: 1>
s.keys() # result: ['a', 'b']
s.get("a") # result: <pyarrow.Int8Scalar: 1>
s.get("z") # result: None
# 1. Iteration yields (key, value) pairs but a Mapping yields keys
list(s) # [('a', 1), ('b', 2)]
# 2. items() relies on iteration yielding keys, so it fails
list(s.items()) # result: TypeError: 'tuple' object cannot be interpreted
as an integer
# 3. values is the ListScalar.values property (a StructArray), not a method
s.values() # result: TypeError: 'pyarrow.lib.StructArray' object is not
callable
# 4. `in` tests (key, value) pairs; a Mapping tests keys
"a" in s # False, although s["a"] works
("a", 1) in s # True
# 5. get() with an int is a positional lookup and only catches KeyError
s.get(0) # result: (<pyarrow.StringScalar: 'a'>,
<pyarrow.Int8Scalar: 1>), instead of None
s.get(99, "default") # result: IndexError: index out of bounds, instead of
"default"
```
The existing `test_scalars.py::test_map` tests pair iteration, positional
indexing, and the `values` property, so changing these to follow Mapping
protocol would change existing behavior.
Should `MapScalar` continue to inherit from `Mapping`?
Found while adding type annotations in #55592. Checked on `main`(27.0.0.dev).
### Component(s)
Python
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]