rok commented on code in PR #55592:
URL: https://github.com/apache/arrow/pull/55592#discussion_r4758705940


##########
python/pyarrow-stubs/__lib/scalar.pyi:
##########
@@ -15,11 +15,430 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# Placeholder stub - complete annotations in future PR.
-# At runtime, these symbols are provided by the pyarrow.lib C extension.
+import datetime as dt
+from collections.abc import Iterator, Mapping, Sequence
+from decimal import Decimal
+from typing import Any, SupportsIndex, TypeAlias, overload
+from uuid import UUID
 
-from typing import Any
+import numpy as np
+from pyarrow._compute import CastOptions
+from pyarrow._stubs_typing import MapsAsPydicts
+from pyarrow.lib import (
+    Array,
+    BaseExtensionType,
+    Buffer,
+    ChunkedArray,
+    DataType,
+    MemoryPool,
+    MonthDayNano,
+    Tensor,
+    _Weakrefable,
+)
+from typing_extensions import CapsuleType
 
-class ExtensionScalar: ...
+class Scalar(_Weakrefable):
+    @property
+    def type(self) -> DataType: ...
+    @property
+    def is_valid(self) -> bool: ...
+    def cast(
+        self,
+        target_type: DataType | str | None = None,
+        safe: bool | None = None,
+        options: CastOptions | None = None,
+        memory_pool: MemoryPool | None = None,
+    ) -> Scalar: ...
+    def validate(self, *, full: bool = False) -> None: ...
+    def equals(self, other: Scalar) -> bool: ...
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> Any: 
...
+    def __abs__(self) -> Scalar: ...
+    def __neg__(self) -> Scalar: ...
+    # The binary operators call the compute functions. An Array or ChunkedArray
+    # operand gives one of the same kind, a Scalar or a number gives a Scalar.
+    # Other operands (e.g. a list or a date) may give either.
+    @overload
+    def __add__(self, other: ChunkedArray, /) -> ChunkedArray: ...
+    @overload
+    def __add__(self, other: Array, /) -> Array: ...
+    @overload
+    def __add__(self, other: Scalar | float, /) -> Scalar: ...

Review Comment:
   Optional follow-up: now that `Scalar` is annotated, `Array.__add__` / 
`Array.__pow__` in `array.pyi` could also accept `Array | Scalar | float -> 
Array` so `arr + scalar` is symmetric with `scalar + arr`.



##########
python/pyarrow-stubs/__lib/scalar.pyi:
##########
@@ -15,11 +15,430 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# Placeholder stub - complete annotations in future PR.
-# At runtime, these symbols are provided by the pyarrow.lib C extension.
+import datetime as dt
+from collections.abc import Iterator, Mapping, Sequence
+from decimal import Decimal
+from typing import Any, SupportsIndex, TypeAlias, overload
+from uuid import UUID
 
-from typing import Any
+import numpy as np
+from pyarrow._compute import CastOptions
+from pyarrow._stubs_typing import MapsAsPydicts
+from pyarrow.lib import (
+    Array,
+    BaseExtensionType,
+    Buffer,
+    ChunkedArray,
+    DataType,
+    MemoryPool,
+    MonthDayNano,
+    Tensor,
+    _Weakrefable,
+)
+from typing_extensions import CapsuleType
 
-class ExtensionScalar: ...
+class Scalar(_Weakrefable):
+    @property
+    def type(self) -> DataType: ...
+    @property
+    def is_valid(self) -> bool: ...
+    def cast(
+        self,
+        target_type: DataType | str | None = None,
+        safe: bool | None = None,
+        options: CastOptions | None = None,
+        memory_pool: MemoryPool | None = None,
+    ) -> Scalar: ...
+    def validate(self, *, full: bool = False) -> None: ...
+    def equals(self, other: Scalar) -> bool: ...
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> Any: 
...
+    def __abs__(self) -> Scalar: ...
+    def __neg__(self) -> Scalar: ...
+    # The binary operators call the compute functions. An Array or ChunkedArray
+    # operand gives one of the same kind, a Scalar or a number gives a Scalar.
+    # Other operands (e.g. a list or a date) may give either.
+    @overload
+    def __add__(self, other: ChunkedArray, /) -> ChunkedArray: ...
+    @overload
+    def __add__(self, other: Array, /) -> Array: ...
+    @overload
+    def __add__(self, other: Scalar | float, /) -> Scalar: ...
+    @overload
+    def __add__(self, other: Any, /) -> Any: ...
+    __sub__ = __add__
+    __mul__ = __add__
+    __truediv__ = __add__
+    __and__ = __add__
+    __or__ = __add__
+    __xor__ = __add__
+    __lshift__ = __add__
+    __rshift__ = __add__
+    @overload
+    def __pow__(self, other: ChunkedArray, mod: None = None, /) -> 
ChunkedArray: ...
+    @overload
+    def __pow__(self, other: Array, mod: None = None, /) -> Array: ...
+    @overload
+    def __pow__(self, other: Scalar | float, mod: None = None, /) -> Scalar: 
...
+    @overload
+    def __pow__(self, other: Any, mod: None = None, /) -> Any: ...
 
-def __getattr__(name: str) -> Any: ...
+class NullScalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> None: 
...
+
+NA: NullScalar
+_NULL: NullScalar
+
+class BooleanScalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> bool | 
None: ...
+    def __bool__(self) -> bool: ...
+
+class UInt8Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int8Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt16Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int16Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt32Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int32Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt64Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int64Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+_IntegerScalar: TypeAlias = (
+    UInt8Scalar
+    | Int8Scalar
+    | UInt16Scalar
+    | Int16Scalar
+    | UInt32Scalar
+    | Int32Scalar
+    | UInt64Scalar
+    | Int64Scalar
+)
+
+class HalfFloatScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class FloatScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class DoubleScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class Decimal32Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal64Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal128Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal256Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+# The value properties of the temporal scalars hold the integer stored in the
+# array (e.g. days since the epoch for Date32Scalar), as_py() converts it.
+class Date32Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.date | None: ...
+
+class Date64Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.date | None: ...
+
+class Time32Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.time | None: ...
+
+class Time64Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.time | None: ...
+
+class TimestampScalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    # A pandas.Timestamp, a datetime subclass, for nanoseconds if pandas is
+    # installed.
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.datetime | None: ...
+
+class DurationScalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    # A pandas.Timedelta, a timedelta subclass, for nanoseconds if pandas is
+    # installed.
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.timedelta | None: ...
+
+class MonthDayNanoIntervalScalar(Scalar):
+    @property
+    def value(self) -> MonthDayNano | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> MonthDayNano | None: ...
+
+class BinaryScalar(Scalar):
+    def as_buffer(self) -> Buffer | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> bytes | None: ...
+    def __bytes__(self) -> bytes: ...
+    # Scalars support the buffer protocol on every Python version, but only
+    # have a __buffer__ method at runtime from Python 3.12 on.
+    def __buffer__(self, flags: int, /) -> memoryview: ...
+
+class LargeBinaryScalar(BinaryScalar): ...
+class FixedSizeBinaryScalar(BinaryScalar): ...
+class BinaryViewScalar(BinaryScalar): ...
+
+class StringScalar(BinaryScalar):
+    def as_py(  # type: ignore[override]
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> str | None: ...
+
+class LargeStringScalar(StringScalar): ...
+class StringViewScalar(StringScalar): ...
+
+class ListScalar(Scalar, Sequence[Scalar]):
+    @property
+    def values(self) -> Array | None: ...
+    def __len__(self) -> int: ...
+    # Only integer indices, unlike Sequence, which also takes slices.
+    def __getitem__(self, i: SupportsIndex) -> Scalar: ...  # type: 
ignore[override]
+    def __iter__(self) -> Iterator[Scalar]: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> list[Any] | None: ...
+
+class FixedSizeListScalar(ListScalar): ...
+class LargeListScalar(ListScalar): ...
+class ListViewScalar(ListScalar): ...
+class LargeListViewScalar(ListScalar): ...
+
+class StructScalar(Scalar, Mapping[str, Scalar]):
+    def __len__(self) -> int: ...
+    def __iter__(self) -> Iterator[str]: ...
+    def __contains__(self, key: object, /) -> bool: ...
+    # Look up a field by name or position.
+    def __getitem__(self, key: str | bytes | int) -> Scalar: ...
+    # The runtime yields a generator instead of a Mapping view
+    def items(self) -> Iterator[tuple[str, Scalar]]: ...  # type: 
ignore[override]
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dict[str, Any] | None: ...
+
+# MapScalar iterates over (key, item) pairs rather than keys. It inherits
+# Mapping, but Mapping protocol conflicts with ListScalar, so Mapping and its
+# methods are omitted (see GH-55866).
+class MapScalar(ListScalar):
+    @overload  # type: ignore[override]
+    def __getitem__(self, i: SupportsIndex) -> tuple[Scalar, Scalar]: ...
+    @overload
+    def __getitem__(self, i: str | bytes) -> Scalar: ...
+    def __iter__(self) -> Iterator[tuple[Any, Any]]: ...  # type: 
ignore[override]
+    def keys(self) -> list[Any]: ...
+    # A list of (key, item) tuples, or a dict if maps_as_pydicts is given.
+    @overload  # type: ignore[override]
+    def as_py(
+        self, *, maps_as_pydicts: None = None
+    ) -> list[tuple[Any, Any]] | None: ...
+    @overload
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts) -> dict[Any, Any] | 
None: ...
+
+class DictionaryScalar(Scalar):
+    @property
+    def index(self) -> _IntegerScalar: ...
+    @property
+    def value(self) -> Scalar: ...
+    @property
+    def dictionary(self) -> Array: ...
+
+class RunEndEncodedScalar(Scalar):
+    @property
+    def value(self) -> Scalar: ...
+
+class UnionScalar(Scalar):
+    @property
+    def value(self) -> Scalar | None: ...
+    @property
+    def type_code(self) -> int: ...
+
+class ExtensionScalar(Scalar):
+    @property
+    def value(self) -> Scalar | None: ...
+    @staticmethod
+    def from_storage(typ: BaseExtensionType, value: Any) -> ExtensionScalar: 
...
+
+class JsonScalar(ExtensionScalar): ...

Review Comment:
   Optional: since `JsonType` storage is always UTF-8 string, 
`JsonScalar.as_py()` always returns `str | None` (fine to leave empty to match 
`JsonArray` too).
   ```suggestion
   class JsonScalar(ExtensionScalar):
       def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> str 
| None: ...
   ```



##########
python/pyarrow-stubs-tests/test_scalar.py:
##########
@@ -0,0 +1,326 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import datetime as dt
+from collections.abc import Hashable, Iterator, Mapping, Sequence
+from decimal import Decimal
+from typing import Any, cast, TYPE_CHECKING
+from uuid import UUID
+
+import pytest
+from typing_extensions import CapsuleType
+
+import pyarrow as pa
+# Not exported from pyarrow until io.pyi is annotated
+from pyarrow.lib import Buffer
+
+from _check import assert_type, check
+
+
+def test_scalar() -> None:
+    # With a type, pa.scalar() return type is Scalar. Tell the type checker
+    # which subclass to expect.
+    s = check(assert_type(pa.scalar(1, pa.int64()), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s.type, pa.DataType), pa.DataType)
+    check(assert_type(s.is_valid, bool), bool)
+    check(assert_type(s.as_py(), Any), int)
+    check(assert_type(s.as_py(maps_as_pydicts="strict"), Any), int)
+    check(assert_type(s.equals(pa.scalar(1)), bool), bool)
+    check(assert_type(s.cast(pa.int8()), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(s.cast("int8", safe=None), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(pa.scalar(1, "int8"), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(pa.scalar(1, pa.int8(), from_pandas=True), pa.Scalar),
+          pa.Int8Scalar)
+    assert_type(s.validate(full=True), None)
+
+    # Without a type, the class is inferred from a plain Python value
+    check(assert_type(pa.scalar(None), pa.NullScalar), pa.NullScalar)
+    check(assert_type(pa.scalar(True), pa.BooleanScalar), pa.BooleanScalar)
+    check(assert_type(pa.scalar("a"), pa.StringScalar), pa.StringScalar)
+    check(assert_type(pa.scalar(b"a"), pa.BinaryScalar), pa.BinaryScalar)
+    # Check values that still return Scalar
+    check(assert_type(pa.scalar(1), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(pa.scalar(1.5), pa.Scalar), pa.DoubleScalar)
+    check(assert_type(pa.scalar([1]), pa.Scalar), pa.ListScalar)
+    # Scalars, unlike arrays, are hashable
+    _: Hashable = s
+
+    check(assert_type(pa.NA, pa.NullScalar), pa.NullScalar)
+    check(assert_type(pa.NULL, pa.NullScalar), pa.NullScalar)
+    assert_type(pa.NA.as_py(), None)
+
+
+def test_scalar_operators() -> None:
+    s = pa.scalar(6)
+    check(assert_type(-s, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(abs(s), pa.Scalar), pa.Int64Scalar)
+    # A scalar or a number gives a scalar, an array operand gives an array
+    check(assert_type(s + 1, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s + pa.scalar(1), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s - pa.array([1]), pa.Array), pa.Int64Array)
+    check(assert_type(s * pa.chunked_array([[1]]), pa.ChunkedArray), 
pa.ChunkedArray)
+    check(assert_type(s / 4, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ** 2, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ** pa.array([2]), pa.Array), pa.Int64Array)
+    check(assert_type(s & 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s | 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ^ 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s << 1, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s >> 1, pa.Scalar), pa.Int64Scalar)
+
+    if TYPE_CHECKING:
+        # Scalars don't implement the reflected operators
+        1 + s  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+        # nor floor division and modulo
+        s // 2  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+        s % 2  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+
+
+def test_array_items() -> None:
+    arr = cast(pa.Int64Array, pa.array([1, 2]))
+    check(assert_type(next(iter(arr)), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(arr.sum(), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(arr.index(2), pa.Int64Scalar), pa.Int64Scalar)
+    check(assert_type(pa.chunked_array([arr]).index(2), pa.Int64Scalar),
+          pa.Int64Scalar)
+
+
+def test_primitive_scalars() -> None:
+    boolean = pa.scalar(True)
+    check(assert_type(boolean.as_py(), bool | None), bool)
+    check(assert_type(bool(boolean), bool), bool)
+
+    integer = cast(pa.Int8Scalar, pa.scalar(1, pa.int8()))
+    check(assert_type(integer.as_py(), int | None), int)
+    # Integer scalars can be used as indices
+    check(assert_type([1, 2][integer], int), int)
+
+    double = cast(pa.DoubleScalar, pa.scalar(1.5))
+    check(assert_type(double.as_py(), float | None), float)
+    check(float(double), float)

Review Comment:
   Nit:
   ```suggestion
       check(assert_type(float(double), float), float)
   ```



##########
python/pyarrow-stubs-tests/test_scalar.py:
##########
@@ -0,0 +1,326 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import datetime as dt
+from collections.abc import Hashable, Iterator, Mapping, Sequence
+from decimal import Decimal
+from typing import Any, cast, TYPE_CHECKING
+from uuid import UUID
+
+import pytest
+from typing_extensions import CapsuleType
+
+import pyarrow as pa
+# Not exported from pyarrow until io.pyi is annotated
+from pyarrow.lib import Buffer
+
+from _check import assert_type, check
+
+
+def test_scalar() -> None:
+    # With a type, pa.scalar() return type is Scalar. Tell the type checker
+    # which subclass to expect.
+    s = check(assert_type(pa.scalar(1, pa.int64()), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s.type, pa.DataType), pa.DataType)
+    check(assert_type(s.is_valid, bool), bool)
+    check(assert_type(s.as_py(), Any), int)
+    check(assert_type(s.as_py(maps_as_pydicts="strict"), Any), int)
+    check(assert_type(s.equals(pa.scalar(1)), bool), bool)
+    check(assert_type(s.cast(pa.int8()), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(s.cast("int8", safe=None), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(pa.scalar(1, "int8"), pa.Scalar), pa.Int8Scalar)
+    check(assert_type(pa.scalar(1, pa.int8(), from_pandas=True), pa.Scalar),
+          pa.Int8Scalar)
+    assert_type(s.validate(full=True), None)
+
+    # Without a type, the class is inferred from a plain Python value
+    check(assert_type(pa.scalar(None), pa.NullScalar), pa.NullScalar)
+    check(assert_type(pa.scalar(True), pa.BooleanScalar), pa.BooleanScalar)
+    check(assert_type(pa.scalar("a"), pa.StringScalar), pa.StringScalar)
+    check(assert_type(pa.scalar(b"a"), pa.BinaryScalar), pa.BinaryScalar)
+    # Check values that still return Scalar
+    check(assert_type(pa.scalar(1), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(pa.scalar(1.5), pa.Scalar), pa.DoubleScalar)
+    check(assert_type(pa.scalar([1]), pa.Scalar), pa.ListScalar)
+    # Scalars, unlike arrays, are hashable
+    _: Hashable = s
+
+    check(assert_type(pa.NA, pa.NullScalar), pa.NullScalar)
+    check(assert_type(pa.NULL, pa.NullScalar), pa.NullScalar)
+    assert_type(pa.NA.as_py(), None)
+
+
+def test_scalar_operators() -> None:
+    s = pa.scalar(6)
+    check(assert_type(-s, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(abs(s), pa.Scalar), pa.Int64Scalar)
+    # A scalar or a number gives a scalar, an array operand gives an array
+    check(assert_type(s + 1, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s + pa.scalar(1), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s - pa.array([1]), pa.Array), pa.Int64Array)
+    check(assert_type(s * pa.chunked_array([[1]]), pa.ChunkedArray), 
pa.ChunkedArray)
+    check(assert_type(s / 4, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ** 2, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ** pa.array([2]), pa.Array), pa.Int64Array)
+    check(assert_type(s & 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s | 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s ^ 3, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s << 1, pa.Scalar), pa.Int64Scalar)
+    check(assert_type(s >> 1, pa.Scalar), pa.Int64Scalar)
+
+    if TYPE_CHECKING:
+        # Scalars don't implement the reflected operators
+        1 + s  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+        # nor floor division and modulo
+        s // 2  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+        s % 2  # type: ignore[operator]  # ty: ignore[unsupported-operator]
+
+
+def test_array_items() -> None:
+    arr = cast(pa.Int64Array, pa.array([1, 2]))
+    check(assert_type(next(iter(arr)), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(arr.sum(), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(arr.index(2), pa.Int64Scalar), pa.Int64Scalar)
+    check(assert_type(pa.chunked_array([arr]).index(2), pa.Int64Scalar),
+          pa.Int64Scalar)
+
+
+def test_primitive_scalars() -> None:
+    boolean = pa.scalar(True)
+    check(assert_type(boolean.as_py(), bool | None), bool)
+    check(assert_type(bool(boolean), bool), bool)
+
+    integer = cast(pa.Int8Scalar, pa.scalar(1, pa.int8()))
+    check(assert_type(integer.as_py(), int | None), int)
+    # Integer scalars can be used as indices
+    check(assert_type([1, 2][integer], int), int)
+
+    double = cast(pa.DoubleScalar, pa.scalar(1.5))
+    check(assert_type(double.as_py(), float | None), float)
+    check(float(double), float)
+    check(assert_type(int(double), int), int)
+
+    decimal = cast(pa.Decimal128Scalar, pa.scalar(Decimal("1.5")))
+    check(assert_type(decimal.as_py(), Decimal | None), Decimal)
+
+    null_int = cast(pa.Int64Scalar, pa.scalar(None, pa.int64()))
+    assert_type(null_int.as_py(), int | None)
+    assert null_int.as_py() is None
+
+
+def test_temporal_scalars() -> None:
+    date = cast(pa.Date32Scalar, pa.scalar(dt.date(2020, 1, 1)))
+    check(assert_type(date.value, int | None), int)
+    check(assert_type(date.as_py(), dt.date | None), dt.date)
+
+    date64 = cast(pa.Date64Scalar, pa.scalar(dt.date(2020, 1, 1), pa.date64()))
+    check(assert_type(date64.value, int | None), int)
+    check(assert_type(date64.as_py(), dt.date | None), dt.date)
+
+    time = cast(pa.Time64Scalar, pa.scalar(dt.time(1, 2)))
+    check(assert_type(time.value, int | None), int)
+    check(assert_type(time.as_py(), dt.time | None), dt.time)
+
+    time32 = cast(pa.Time32Scalar, pa.scalar(dt.time(1, 2), pa.time32("s")))
+    check(assert_type(time32.as_py(), dt.time | None), dt.time)
+
+    timestamp = cast(pa.TimestampScalar, pa.scalar(dt.datetime(2020, 1, 1)))
+    check(assert_type(timestamp.value, int | None), int)
+    check(assert_type(timestamp.as_py(), dt.datetime | None), dt.datetime)
+
+    duration = cast(pa.DurationScalar, pa.scalar(dt.timedelta(seconds=1)))
+    check(assert_type(duration.value, int | None), int)
+    check(assert_type(duration.as_py(), dt.timedelta | None), dt.timedelta)
+
+    interval = cast(pa.MonthDayNanoIntervalScalar,
+                    pa.scalar(pa.MonthDayNano([1, 2, 3])))
+    check(assert_type(interval.value, pa.MonthDayNano | None), pa.MonthDayNano)
+    check(assert_type(interval.as_py(), pa.MonthDayNano | None),
+          pa.MonthDayNano)
+
+
+def test_binary_scalars() -> None:
+    binary = pa.scalar(b"abc")
+    check(assert_type(binary.as_py(), bytes | None), bytes)
+    check(assert_type(binary.as_buffer(), Buffer | None), pa.Buffer)
+    check(assert_type(bytes(binary), bytes), bytes)
+    check(assert_type(memoryview(binary), memoryview), memoryview)
+
+    # String scalars are binary scalars at runtime
+    string = pa.scalar("abc")
+    check(assert_type(string.as_py(), str | None), str)
+    check(assert_type(string.as_buffer(), Buffer | None), pa.Buffer)
+    check(string, pa.BinaryScalar)
+
+    large = cast(pa.LargeStringScalar, pa.scalar("abc", pa.large_string()))
+    check(assert_type(large.as_py(), str | None), str)
+    view = cast(pa.BinaryViewScalar, pa.scalar(b"abc", pa.binary_view()))
+    check(assert_type(view.as_py(), bytes | None), bytes)
+
+
+def test_list_scalar() -> None:
+    lst = cast(pa.ListScalar, pa.scalar([1, 2]))
+    check(assert_type(lst.values, pa.Array | None), pa.Int64Array)
+    check(assert_type(len(lst), int), int)
+    check(assert_type(lst[0], pa.Scalar), pa.Int64Scalar)
+    check(assert_type(next(iter(lst)), pa.Scalar), pa.Int64Scalar)
+    check(assert_type(lst.as_py(), list[Any] | None), list)
+    # List scalars are sequences at runtime
+    seq: Sequence[pa.Scalar] = check(lst, Sequence)
+    check(assert_type(seq.index(lst[1]), int), int)
+
+    fixed = cast(pa.FixedSizeListScalar,
+                 pa.scalar([1, 2], pa.list_(pa.int64(), 2)))
+    check(assert_type(fixed[0], pa.Scalar), pa.Int64Scalar)
+
+    null_list = cast(pa.ListScalar, pa.scalar(None, pa.list_(pa.int64())))
+    assert_type(null_list.values, pa.Array | None)
+    assert null_list.values is None
+    assert null_list.as_py() is None
+
+    if TYPE_CHECKING:
+        # Only integer indices are supported, not slices
+        lst[1:]  # type: ignore[index]
+
+
+def test_struct_scalar() -> None:
+    struct = cast(pa.StructScalar, pa.scalar({"a": 1, "b": "x"}))
+    check(assert_type(struct["a"], pa.Scalar), pa.Int64Scalar)
+    check(assert_type(struct[1], pa.Scalar), pa.StringScalar)
+    check(assert_type(len(struct), int), int)
+    check(assert_type("a" in struct, bool), bool)
+    check(assert_type(list(struct), list[str]), list)
+    check(assert_type(struct.items(), Iterator[tuple[str, pa.Scalar]]), 
Iterator)
+    check(assert_type(struct.as_py(), dict[str, Any] | None), dict)
+    # Struct scalars are mappings at runtime
+    mapping: Mapping[str, pa.Scalar] = check(struct, Mapping)
+    check(assert_type(mapping.get("a"), pa.Scalar | None), pa.Int64Scalar)
+
+
+def test_map_scalar() -> None:
+    map_type = pa.map_(pa.string(), pa.int64())
+    map_scalar = cast(pa.MapScalar, pa.scalar([("a", 1)], map_type))
+    check(map_scalar, pa.ListScalar)
+    check(assert_type(map_scalar.values, pa.Array | None), pa.StructArray)
+    check(assert_type(map_scalar[0], tuple[pa.Scalar, pa.Scalar]), tuple)
+    check(assert_type(map_scalar["a"], pa.Scalar), pa.Int64Scalar)
+    check(assert_type(map_scalar.keys(), list[Any]), list)
+    # Iterating yields the (key, item) pairs as Python objects
+    check(assert_type(next(iter(map_scalar)), tuple[Any, Any]), tuple)
+    # as_py() returns a list of pairs, or a dict if maps_as_pydicts is given
+    check(assert_type(map_scalar.as_py(), list[tuple[Any, Any]] | None), list)
+    check(assert_type(map_scalar.as_py(maps_as_pydicts="lossy"),
+                      dict[Any, Any] | None),
+          dict)
+
+    null_map = cast(pa.MapScalar, pa.scalar(None, map_type))
+    assert null_map.as_py() is None
+    assert null_map.as_py(maps_as_pydicts="strict") is None
+    check(assert_type(null_map.keys(), list[Any]), list)
+
+
+def test_dictionary_scalar() -> None:
+    dict_arr = pa.array(["a", "b"]).dictionary_encode()
+    dict_scalar = cast(pa.DictionaryScalar, dict_arr[1])
+    index = check(dict_scalar.index, pa.Int32Scalar)
+    # The index type is one of the integer types
+    check(assert_type(index.as_py(), int | None), int)
+    check(assert_type(dict_scalar.value, pa.Scalar), pa.StringScalar)
+    check(assert_type(dict_scalar.dictionary, pa.Array), pa.StringArray)
+
+
+def test_run_end_encoded_scalar() -> None:
+    ree = pa.RunEndEncodedArray.from_arrays([2], ["a"])
+    ree_scalar = cast(pa.RunEndEncodedScalar, ree[0])
+    check(assert_type(ree_scalar.value, pa.Scalar), pa.StringScalar)
+
+
+def test_union_scalar() -> None:
+    union = pa.UnionArray.from_sparse(
+        cast(pa.Int8Array, pa.array([0], pa.int8())), [pa.array([1])]
+    )
+    union_scalar = cast(pa.UnionScalar, union[0])
+    check(assert_type(union_scalar.value, pa.Scalar | None), pa.Int64Scalar)
+    check(assert_type(union_scalar.type_code, int), int)
+
+    null_union = pa.UnionArray.from_sparse(
+        cast(pa.Int8Array, pa.array([0], pa.int8())),
+        [pa.array([None], pa.int64())],
+    )
+    null_scalar = cast(pa.UnionScalar, null_union[0])
+    assert_type(null_scalar.value, pa.Scalar | None)
+    assert null_scalar.value is None
+
+
+def test_extension_scalars() -> None:
+    ext = pa.ExtensionScalar.from_storage(pa.uuid(), b"0" * 16)
+    uuid = check(assert_type(ext, pa.ExtensionScalar), pa.UuidScalar)
+    check(assert_type(uuid.value, pa.Scalar | None), pa.FixedSizeBinaryScalar)
+
+    uuid_scalar = cast(pa.UuidScalar, uuid)
+    check(assert_type(uuid_scalar.as_py(), UUID | None), UUID)
+
+    null_uuid = cast(pa.UuidScalar,
+                     pa.ExtensionScalar.from_storage(pa.uuid(), None))
+    assert null_uuid.value is None
+    assert null_uuid.as_py() is None
+
+    bool8 = cast(pa.Bool8Scalar, pa.scalar(1, pa.bool8()))
+    check(assert_type(bool8.as_py(), bool | None), bool)
+
+
+def test_scalar_numpy_index() -> None:
+    pytest.importorskip("numpy")
+    import numpy as np
+
+    # Anything with __index__ can index a list or map scalar, e.g. NumPy ints
+    lst = cast(pa.ListScalar, pa.scalar([1, 2]))
+    check(assert_type(lst[np.int64(0)], pa.Scalar), pa.Int64Scalar)
+    map_scalar = cast(pa.MapScalar,
+                      pa.scalar([("a", 1)], pa.map_(pa.string(), pa.int64())))
+    check(assert_type(map_scalar[np.int64(0)], tuple[pa.Scalar, pa.Scalar]),
+          tuple)
+    if TYPE_CHECKING:
+        # Struct scalars only take int positions
+        struct = cast(pa.StructScalar, pa.scalar({"a": 1}))
+        struct[np.int64(0)]  # type: ignore[index]
+
+
+def test_fixed_shape_tensor_scalar() -> None:
+    pytest.importorskip("numpy")
+    import numpy as np
+
+    tensor_type = pa.fixed_shape_tensor(pa.int32(), [2])
+    storage = pa.array([[1, 2]], pa.list_(pa.int32(), 2))
+    tensors = cast(pa.FixedShapeTensorArray, tensor_type.wrap_array(storage))
+    tensor = cast(pa.FixedShapeTensorScalar, tensors[0])
+    check(assert_type(tensor.to_numpy(), np.ndarray), np.ndarray)

Review Comment:
   If `FixedShapeTensorScalar.to_numpy()` is updated to `NDArray[Any]`:
   ```suggestion
       import numpy as np
       from numpy.typing import NDArray
   
       tensor_type = pa.fixed_shape_tensor(pa.int32(), [2])
       storage = pa.array([[1, 2]], pa.list_(pa.int32(), 2))
       tensors = cast(pa.FixedShapeTensorArray, tensor_type.wrap_array(storage))
       tensor = cast(pa.FixedShapeTensorScalar, tensors[0])
       check(assert_type(tensor.to_numpy(), NDArray[Any]), np.ndarray)
   ```



##########
python/pyarrow-stubs/__init__.pyi:
##########
@@ -225,6 +275,9 @@ from pyarrow.lib import (
 
 from pyarrow import types as types
 
+# pyarrow/__init__.py imports pyarrow.lib._NULL as NULL
+NULL: NullScalar

Review Comment:
   Nit: could also re-export `_NULL as NULL` directly in the `from pyarrow.lib 
import (...)` block above to mirror `pyarrow/__init__.py`.



##########
python/pyarrow-stubs/__lib/scalar.pyi:
##########
@@ -15,11 +15,430 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# Placeholder stub - complete annotations in future PR.
-# At runtime, these symbols are provided by the pyarrow.lib C extension.
+import datetime as dt
+from collections.abc import Iterator, Mapping, Sequence
+from decimal import Decimal
+from typing import Any, SupportsIndex, TypeAlias, overload
+from uuid import UUID
 
-from typing import Any
+import numpy as np
+from pyarrow._compute import CastOptions
+from pyarrow._stubs_typing import MapsAsPydicts
+from pyarrow.lib import (
+    Array,
+    BaseExtensionType,
+    Buffer,
+    ChunkedArray,
+    DataType,
+    MemoryPool,
+    MonthDayNano,
+    Tensor,
+    _Weakrefable,
+)
+from typing_extensions import CapsuleType
 
-class ExtensionScalar: ...
+class Scalar(_Weakrefable):
+    @property
+    def type(self) -> DataType: ...
+    @property
+    def is_valid(self) -> bool: ...
+    def cast(
+        self,
+        target_type: DataType | str | None = None,
+        safe: bool | None = None,
+        options: CastOptions | None = None,
+        memory_pool: MemoryPool | None = None,
+    ) -> Scalar: ...
+    def validate(self, *, full: bool = False) -> None: ...
+    def equals(self, other: Scalar) -> bool: ...
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> Any: 
...
+    def __abs__(self) -> Scalar: ...
+    def __neg__(self) -> Scalar: ...
+    # The binary operators call the compute functions. An Array or ChunkedArray
+    # operand gives one of the same kind, a Scalar or a number gives a Scalar.
+    # Other operands (e.g. a list or a date) may give either.
+    @overload
+    def __add__(self, other: ChunkedArray, /) -> ChunkedArray: ...
+    @overload
+    def __add__(self, other: Array, /) -> Array: ...
+    @overload
+    def __add__(self, other: Scalar | float, /) -> Scalar: ...
+    @overload
+    def __add__(self, other: Any, /) -> Any: ...
+    __sub__ = __add__
+    __mul__ = __add__
+    __truediv__ = __add__
+    __and__ = __add__
+    __or__ = __add__
+    __xor__ = __add__
+    __lshift__ = __add__
+    __rshift__ = __add__
+    @overload
+    def __pow__(self, other: ChunkedArray, mod: None = None, /) -> 
ChunkedArray: ...
+    @overload
+    def __pow__(self, other: Array, mod: None = None, /) -> Array: ...
+    @overload
+    def __pow__(self, other: Scalar | float, mod: None = None, /) -> Scalar: 
...
+    @overload
+    def __pow__(self, other: Any, mod: None = None, /) -> Any: ...
 
-def __getattr__(name: str) -> Any: ...
+class NullScalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> None: 
...
+
+NA: NullScalar
+_NULL: NullScalar
+
+class BooleanScalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> bool | 
None: ...
+    def __bool__(self) -> bool: ...
+
+class UInt8Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int8Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt16Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int16Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt32Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int32Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class UInt64Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+class Int64Scalar(Scalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> int | 
None: ...
+    def __index__(self) -> int: ...
+
+_IntegerScalar: TypeAlias = (
+    UInt8Scalar
+    | Int8Scalar
+    | UInt16Scalar
+    | Int16Scalar
+    | UInt32Scalar
+    | Int32Scalar
+    | UInt64Scalar
+    | Int64Scalar
+)
+
+class HalfFloatScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class FloatScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class DoubleScalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> float | None: ...
+    def __float__(self) -> float: ...
+    def __int__(self) -> int: ...
+
+class Decimal32Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal64Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal128Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+class Decimal256Scalar(Scalar):
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> Decimal | None: ...
+
+# The value properties of the temporal scalars hold the integer stored in the
+# array (e.g. days since the epoch for Date32Scalar), as_py() converts it.
+class Date32Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.date | None: ...
+
+class Date64Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.date | None: ...
+
+class Time32Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.time | None: ...
+
+class Time64Scalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.time | None: ...
+
+class TimestampScalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    # A pandas.Timestamp, a datetime subclass, for nanoseconds if pandas is
+    # installed.
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.datetime | None: ...
+
+class DurationScalar(Scalar):
+    @property
+    def value(self) -> int | None: ...
+    # A pandas.Timedelta, a timedelta subclass, for nanoseconds if pandas is
+    # installed.
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dt.timedelta | None: ...
+
+class MonthDayNanoIntervalScalar(Scalar):
+    @property
+    def value(self) -> MonthDayNano | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> MonthDayNano | None: ...
+
+class BinaryScalar(Scalar):
+    def as_buffer(self) -> Buffer | None: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> bytes | None: ...
+    def __bytes__(self) -> bytes: ...
+    # Scalars support the buffer protocol on every Python version, but only
+    # have a __buffer__ method at runtime from Python 3.12 on.
+    def __buffer__(self, flags: int, /) -> memoryview: ...
+
+class LargeBinaryScalar(BinaryScalar): ...
+class FixedSizeBinaryScalar(BinaryScalar): ...
+class BinaryViewScalar(BinaryScalar): ...
+
+class StringScalar(BinaryScalar):
+    def as_py(  # type: ignore[override]
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> str | None: ...
+
+class LargeStringScalar(StringScalar): ...
+class StringViewScalar(StringScalar): ...
+
+class ListScalar(Scalar, Sequence[Scalar]):
+    @property
+    def values(self) -> Array | None: ...
+    def __len__(self) -> int: ...
+    # Only integer indices, unlike Sequence, which also takes slices.
+    def __getitem__(self, i: SupportsIndex) -> Scalar: ...  # type: 
ignore[override]
+    def __iter__(self) -> Iterator[Scalar]: ...
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> list[Any] | None: ...
+
+class FixedSizeListScalar(ListScalar): ...
+class LargeListScalar(ListScalar): ...
+class ListViewScalar(ListScalar): ...
+class LargeListViewScalar(ListScalar): ...
+
+class StructScalar(Scalar, Mapping[str, Scalar]):
+    def __len__(self) -> int: ...
+    def __iter__(self) -> Iterator[str]: ...
+    def __contains__(self, key: object, /) -> bool: ...
+    # Look up a field by name or position.
+    def __getitem__(self, key: str | bytes | int) -> Scalar: ...
+    # The runtime yields a generator instead of a Mapping view
+    def items(self) -> Iterator[tuple[str, Scalar]]: ...  # type: 
ignore[override]
+    def as_py(
+        self, *, maps_as_pydicts: MapsAsPydicts | None = None
+    ) -> dict[str, Any] | None: ...
+
+# MapScalar iterates over (key, item) pairs rather than keys. It inherits
+# Mapping, but Mapping protocol conflicts with ListScalar, so Mapping and its
+# methods are omitted (see GH-55866).
+class MapScalar(ListScalar):
+    @overload  # type: ignore[override]
+    def __getitem__(self, i: SupportsIndex) -> tuple[Scalar, Scalar]: ...
+    @overload
+    def __getitem__(self, i: str | bytes) -> Scalar: ...
+    def __iter__(self) -> Iterator[tuple[Any, Any]]: ...  # type: 
ignore[override]
+    def keys(self) -> list[Any]: ...
+    # A list of (key, item) tuples, or a dict if maps_as_pydicts is given.
+    @overload  # type: ignore[override]
+    def as_py(
+        self, *, maps_as_pydicts: None = None
+    ) -> list[tuple[Any, Any]] | None: ...
+    @overload
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts) -> dict[Any, Any] | 
None: ...
+
+class DictionaryScalar(Scalar):
+    @property
+    def index(self) -> _IntegerScalar: ...
+    @property
+    def value(self) -> Scalar: ...
+    @property
+    def dictionary(self) -> Array: ...
+
+class RunEndEncodedScalar(Scalar):
+    @property
+    def value(self) -> Scalar: ...
+
+class UnionScalar(Scalar):
+    @property
+    def value(self) -> Scalar | None: ...
+    @property
+    def type_code(self) -> int: ...
+
+class ExtensionScalar(Scalar):
+    @property
+    def value(self) -> Scalar | None: ...
+    @staticmethod
+    def from_storage(typ: BaseExtensionType, value: Any) -> ExtensionScalar: 
...
+
+class JsonScalar(ExtensionScalar): ...
+
+class UuidScalar(ExtensionScalar):
+    def as_py(self, *, maps_as_pydicts: MapsAsPydicts | None = None) -> UUID | 
None: ...
+
+class FixedShapeTensorScalar(ExtensionScalar):
+    def to_numpy(self) -> np.ndarray: ...

Review Comment:
   Use `NDArray[Any]` to match `Tensor.to_numpy()` and avoid bare generic 
`np.ndarray`:
   ```suggestion
       def to_numpy(self) -> NDArray[Any]: ...
   ```



-- 
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]

Reply via email to