Skip to content

[Python] MapScalar inherits collections.abc.Mapping but doesn't follow Mapping protocol #55866

Backlog

Description

@tadeja

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:

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

Activity

  1. AlenkaF commented on Dec 1, 2026

    @AlenkaF
    Member

    Adding some background info. Support for Python protocols in Scalars have been raised by #45653 and worked on in #45818. Connected discussion in:

  2. added a commit that references this issue on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions