You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Python] MapScalar inherits collections.abc.Mapping but doesn't follow Mapping protocol #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:
fromcollections.abcimportMappingimportpyarrowaspas=pa.scalar([("a", 1), ("b", 2)], type=pa.map_(pa.string(), pa.int8()))
assertisinstance(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 keyslist(s) # [('a', 1), ('b', 2)]# 2. items() relies on iteration yielding keys, so it failslist(s.items()) # result: TypeError: 'tuple' object cannot be interpreted as an integer# 3. values is the ListScalar.values property (a StructArray), not a methods.values() # result: TypeError: 'pyarrow.lib.StructArray' object is not callable# 4. `in` tests (key, value) pairs; a Mapping tests keys"a"ins# False, although s["a"] works
("a", 1) ins# True# 5. get() with an int is a positional lookup and only catches KeyErrors.get(0) # result: (<pyarrow.StringScalar: 'a'>, <pyarrow.Int8Scalar: 1>), instead of Nones.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).
Activity
AlenkaF commentedon Dec 1, 2026
Adding some background info. Support for Python protocols in Scalars have been raised by #45653 and worked on in #45818. Connected discussion in:
GH-55590: [Python][Annotations] Add type annotations for scalars (#55592