Skip to content

Commit efc42db

Browse files
h-jooThe dataclass_array Authors
authored andcommitted
Code update
PiperOrigin-RevId: 942136812
1 parent beb9b64 commit efc42db

24 files changed

Lines changed: 78 additions & 78 deletions

dataclass_array/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/array_dataclass.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -137,7 +137,7 @@ def array_field(
137137
# TODO(epot): Validate shape, dtype
138138
dca_field = _ArrayFieldMetadata(
139139
inner_shape_non_static=shape,
140-
dtype=dtype,
140+
dtype=dtype, # pyrefly: ignore[bad-argument-type]
141141
)
142142
return dataclasses.field(**field_kwargs, metadata={_METADATA_KEY: dca_field})
143143

@@ -148,7 +148,7 @@ class MetaDataclassArray(type):
148148
# TODO(b/204422756): We cannot use `__class_getitem__` due to b/204422756
149149
def __getitem__(cls, spec):
150150
# Not clear how this would interact if cls is also a `Generic`
151-
return Annotated[cls, field_utils.ShapeAnnotation(spec)]
151+
return Annotated[cls, field_utils.ShapeAnnotation(spec)] # pyrefly: ignore[not-a-type]
152152

153153

154154
# TODO(epot): Restore once pytype support this
@@ -386,7 +386,7 @@ def __getitem__(self: _DcT, indices: _IndicesArg) -> _DcT:
386386
indices = _to_absolute_indices(indices, shape=self.shape)
387387
return self._map_field(
388388
array_fn=lambda f: f.value[indices],
389-
dc_fn=lambda f: f.value[indices],
389+
dc_fn=lambda f: f.value[indices], # pyrefly: ignore[bad-argument-type]
390390
)
391391

392392
# _DcT[n *d] -> Iterator[_DcT[*d]]
@@ -454,7 +454,7 @@ def fn(ray: Optional[dca.Ray] = None):
454454

455455
def map_field(
456456
self: _DcT,
457-
fn: Callable[[Array['*din']], Array['*dout']],
457+
fn: Callable[[Array['*din']], Array['*dout']], # pyrefly: ignore[not-a-type]
458458
) -> _DcT:
459459
"""Apply a transformation on all arrays from the fields."""
460460
return self._map_field( # pylint: disable=protected-access
@@ -659,7 +659,7 @@ def _cast_field(f: _ArrayField) -> None:
659659
new_value = np_utils.asarray(
660660
f.value,
661661
xnp=xnp,
662-
dtype=f.dtype,
662+
dtype=f.dtype, # pyrefly: ignore[bad-argument-type]
663663
cast_dtype=self.__dca_params__.cast_dtype,
664664
)
665665
self._setattr(f.name, new_value)
@@ -740,7 +740,7 @@ def _to_absolute_axis(self, axis: Axes) -> Axes:
740740
def _map_field(
741741
self: _DcT,
742742
*,
743-
array_fn: Callable[[_ArrayField[Array['*din']]], Array['*dout']],
743+
array_fn: Callable[[_ArrayField[Array['*din']]], Array['*dout']], # pyrefly: ignore[not-a-type]
744744
dc_fn: Optional[Callable[[_ArrayField[_DcT]], _DcT]],
745745
_inplace: bool = False,
746746
) -> _DcT:
@@ -758,7 +758,7 @@ def _map_field(
758758

759759
def _apply_field_dn(f: _ArrayField):
760760
if f.is_dataclass: # Recurse on dataclasses
761-
return dc_fn(f) # pylint: disable=protected-access
761+
return dc_fn(f) # pylint: disable=protected-access # pyrefly: ignore[not-callable]
762762
else:
763763
return array_fn(f)
764764

@@ -881,7 +881,7 @@ def _setattr(self, name: str, value: Any) -> None:
881881
"""Like setattr, but support `frozen` dataclasses."""
882882
object.__setattr__(self, name, value)
883883

884-
def assert_same_xnp(self, x: Union[Array[...], DataclassArray]) -> None:
884+
def assert_same_xnp(self, x: Union[Array[...], DataclassArray]) -> None: # pyrefly: ignore[not-a-type]
885885
"""Assert the given array is of the same type as the current object."""
886886
xnp = np_utils.get_xnp(x)
887887
if xnp is not self.xnp:
@@ -942,7 +942,7 @@ def _init_cls(self: DataclassArray) -> None:
942942
# is propagated by the various ops.
943943
dca_fields_metadata[_DUMMY_ARRAY_FIELD] = _ArrayFieldMetadata( # pytype: disable=wrong-arg-types
944944
inner_shape_non_static=(),
945-
dtype=np.float32,
945+
dtype=np.float32, # pyrefly: ignore[bad-argument-type]
946946
)
947947
default_dummy_array = np.zeros((), dtype=np.float32)
948948
_add_field_to_dataclass(
@@ -1032,7 +1032,7 @@ def _to_absolute_indices(indices: _Indices, *, shape: Shape) -> _Indices:
10321032
start_elems = indices[:ellipsis_index]
10331033
end_elems = indices[ellipsis_index + 1 :]
10341034
ellipsis_replacement = [slice(None)] * (len(shape) - valid_count)
1035-
return (*start_elems, *ellipsis_replacement, *end_elems)
1035+
return (*start_elems, *ellipsis_replacement, *end_elems) # pyrefly: ignore[bad-return]
10361036

10371037

10381038
@dataclasses.dataclass(frozen=True)
@@ -1116,15 +1116,15 @@ def full_shape(self) -> DcOrArrayT:
11161116
"""Access the `host.<field-name>.shape`."""
11171117
# TODO(b/198633198): We need to convert to tuple because TF evaluate
11181118
# empty shapes to True `bool(shape) == True` when `shape=()`
1119-
return tuple(self.value.shape)
1119+
return tuple(self.value.shape) # pyrefly: ignore[bad-return, missing-attribute]
11201120

11211121
@functools.cached_property
11221122
def inner_shape(self) -> Shape:
11231123
"""Returns the the static shape resolved for the current value."""
11241124

11251125
if not self.inner_shape_non_static:
11261126
return ()
1127-
static_shape = self.full_shape[-len(self.inner_shape_non_static) :]
1127+
static_shape = self.full_shape[-len(self.inner_shape_non_static) :] # pyrefly: ignore[unsupported-operation]
11281128

11291129
def err_msg() -> ValueError:
11301130
return ValueError(
@@ -1177,8 +1177,8 @@ def host_shape(self) -> Shape:
11771177
if not self.inner_shape_non_static:
11781178
shape = self.full_shape
11791179
else:
1180-
shape = self.full_shape[: -len(self.inner_shape_non_static)]
1181-
return shape
1180+
shape = self.full_shape[: -len(self.inner_shape_non_static)] # pyrefly: ignore[unsupported-operation]
1181+
return shape # pyrefly: ignore[bad-return]
11821182

11831183
def assert_shape(self) -> None:
11841184
if self.host_shape + self.inner_shape != self.full_shape:
@@ -1191,7 +1191,7 @@ def broadcast_to(self, shape: Shape) -> DcOrArrayT:
11911191
"""Broadcast the host_shape."""
11921192
final_shape = shape + self.inner_shape
11931193
if self.is_dataclass:
1194-
return self.value.broadcast_to(final_shape)
1194+
return self.value.broadcast_to(final_shape) # pyrefly: ignore[missing-attribute]
11951195
else:
11961196
return self.xnp.broadcast_to(self.value, final_shape)
11971197

@@ -1214,7 +1214,7 @@ def _is_called_inside_torch_vmap() -> bool:
12141214

12151215
def _make_field_metadata(
12161216
field: dataclasses.Field[Any],
1217-
hints: dict[str, TypeAlias],
1217+
hints: dict[str, TypeAlias], # pyrefly: ignore[invalid-annotation]
12181218
) -> Optional[_ArrayFieldMetadata]:
12191219
"""Make the array field class."""
12201220
# TODO(epot): One possible confusion is if user define
@@ -1233,7 +1233,7 @@ def _make_field_metadata(
12331233
return field_metadata
12341234

12351235

1236-
def _type_to_field_metadata(hint: TypeAlias) -> Optional[_ArrayFieldMetadata]:
1236+
def _type_to_field_metadata(hint: TypeAlias) -> Optional[_ArrayFieldMetadata]: # pyrefly: ignore[invalid-annotation]
12371237
"""Converts type hint to extract `inner_shape`, `dtype`."""
12381238
array_type = type_parsing.get_array_type(hint)
12391239

dataclass_array/array_dataclass_test.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -50,8 +50,8 @@ def assert_val(
5050

5151
@dca.dataclass_array(broadcast=True, cast_dtype=True)
5252
class Point(dca.DataclassArray):
53-
x: f32['*shape']
54-
y: f32['*shape']
53+
x: f32['*shape'] # pyrefly: ignore[not-a-type]
54+
y: f32['*shape'] # pyrefly: ignore[not-a-type]
5555

5656
@staticmethod
5757
def make(shape: Shape, xnp: enp.NpModule) -> Point:
@@ -77,8 +77,8 @@ def assert_val(p: Point, shape: Shape, xnp: enp.NpModule = None):
7777

7878
@dca.dataclass_array(broadcast=True, cast_dtype=True)
7979
class Isometrie(dca.DataclassArray):
80-
r: f32['... 3 3']
81-
t: i32[..., 2]
80+
r: f32['... 3 3'] # pyrefly: ignore[not-a-type]
81+
t: i32[..., 2] # pyrefly: ignore[not-a-type]
8282

8383
@staticmethod
8484
def make(shape: Shape, xnp: enp.NpModule) -> Isometrie:
@@ -198,8 +198,8 @@ class WithStatic(dca.DataclassArray):
198198

199199
static: str
200200
nested_static: NestedOnlyStatic
201-
x: f32['... 3']
202-
y: Any = dca.field(shape=(2, 2), dtype=np.float32)
201+
x: f32['... 3'] # pyrefly: ignore[not-a-type]
202+
y: Any = dca.field(shape=(2, 2), dtype=np.float32) # pyrefly: ignore[bad-argument-type]
203203

204204
@staticmethod
205205
def make(shape: Shape, xnp: enp.NpModule) -> WithStatic:
@@ -410,7 +410,7 @@ def test_wrong_input_type():
410410
@dca.dataclass_array(broadcast=True, cast_dtype=True)
411411
class PointWrapper(dca.DataclassArray):
412412
pts: Point
413-
rgb: f32['*shape 3']
413+
rgb: f32['*shape 3'] # pyrefly: ignore[not-a-type]
414414

415415
# ndarray instead of DataclassArray
416416
with pytest.raises(TypeError, match='Invalid PointWrapper.pts:'):
@@ -646,8 +646,8 @@ def fn(p: WithStatic) -> WithStatic:
646646
@enp.testing.parametrize_xnp()
647647
def test_dataclass_params_no_cast(xnp: enp.NpModule):
648648
class PointNoCast(dca.DataclassArray):
649-
x: FloatArray['*shape']
650-
y: IntArray['*shape']
649+
x: FloatArray['*shape'] # pyrefly: ignore[not-a-type]
650+
y: IntArray['*shape'] # pyrefly: ignore[not-a-type]
651651

652652
with pytest.raises(ValueError, match='Cannot cast float16'):
653653
PointNoCast(
@@ -668,8 +668,8 @@ class PointNoCast(dca.DataclassArray):
668668
def test_dataclass_params_no_list(xnp: enp.NpModule):
669669
@dca.dataclass_array(cast_list=False)
670670
class PointNoList(dca.DataclassArray):
671-
x: FloatArray['*shape']
672-
y: IntArray['*shape']
671+
x: FloatArray['*shape'] # pyrefly: ignore[not-a-type]
672+
y: IntArray['*shape'] # pyrefly: ignore[not-a-type]
673673

674674
with pytest.raises(TypeError, match='Could not infer numpy module'):
675675
PointNoList(
@@ -681,8 +681,8 @@ class PointNoList(dca.DataclassArray):
681681
@enp.testing.parametrize_xnp()
682682
def test_dataclass_params_no_broadcast(xnp: enp.NpModule):
683683
class PointNoBroadcast(dca.DataclassArray):
684-
x: FloatArray['*shape']
685-
y: IntArray['*shape']
684+
x: FloatArray['*shape'] # pyrefly: ignore[not-a-type]
685+
y: IntArray['*shape'] # pyrefly: ignore[not-a-type]
686686

687687
with pytest.raises(ValueError, match='Cannot broadcast'):
688688
PointNoBroadcast(
@@ -695,8 +695,8 @@ class PointNoBroadcast(dca.DataclassArray):
695695
@pytest.mark.parametrize('batch_shape', [(), (1, 3)])
696696
def test_dataclass_none_shape(xnp: enp.NpModule, batch_shape: Shape):
697697
class PointDynamicShape(dca.DataclassArray):
698-
x: FloatArray[..., None, None]
699-
y: IntArray['... 3 _']
698+
x: FloatArray[..., None, None] # pyrefly: ignore[not-a-type]
699+
y: IntArray['... 3 _'] # pyrefly: ignore[not-a-type]
700700

701701
p = PointDynamicShape(
702702
x=xnp.zeros(batch_shape + (2, 3), dtype=xnp.float32),
@@ -751,8 +751,8 @@ class PointDynamicShape(dca.DataclassArray):
751751
@pytest.mark.parametrize('batch_shape', [(1,), (3,)])
752752
def test_concatenate(xnp: enp.NpModule, batch_shape: Shape):
753753
class TestConcatenateClass(dca.DataclassArray):
754-
x: FloatArray['*shape 3']
755-
y: FloatArray['*shape']
754+
x: FloatArray['*shape 3'] # pyrefly: ignore[not-a-type]
755+
y: FloatArray['*shape'] # pyrefly: ignore[not-a-type]
756756

757757
p = TestConcatenateClass(
758758
x=xnp.zeros(batch_shape + (3,), dtype=xnp.float32),

dataclass_array/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/field_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/import_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727

2828

2929
class A(dca.DataclassArray):
30-
x: dca.typing.f32['*s']
30+
x: dca.typing.f32['*s'] # pyrefly: ignore[not-a-type]
3131

3232

3333
def test_lazy():

dataclass_array/ops.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ def _ops_base(
3636
int,
3737
Any, # array_dataclass._ArrayField[Array['*din']],
3838
],
39-
Array['*dout'],
39+
Array['*dout'], # pyrefly: ignore[not-a-type]
4040
],
4141
dc_fn: Optional[
4242
Callable[
@@ -73,7 +73,7 @@ def _ops_base(
7373
xnp = first_arr.xnp
7474
# If axis < 0, normalize the axis such as the last axis is before the inner
7575
# shape
76-
axis = np_utils.to_absolute_axis(axis, ndim=first_arr.ndim + 1)
76+
axis = np_utils.to_absolute_axis(axis, ndim=first_arr.ndim + 1) # pyrefly: ignore[bad-assignment]
7777

7878
# Iterating over only the fields of the `first_arr` will skip optional fields
7979
# if those are not set in `first_arr`, even if they are present in others.
@@ -82,7 +82,7 @@ def _ops_base(
8282
# Similarly, static values will be the ones from the first element.
8383
merged_arr = first_arr._map_field( # pylint: disable=protected-access
8484
array_fn=functools.partial(array_fn, xnp, axis),
85-
dc_fn=functools.partial(dc_fn, xnp, axis),
85+
dc_fn=functools.partial(dc_fn, xnp, axis), # pyrefly: ignore[bad-argument-type]
8686
)
8787
return merged_arr
8888

dataclass_array/shape_parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/shape_parsing_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

dataclass_array/testing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The dataclass_array Authors.
1+
# Copyright 2026 The dataclass_array Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -30,8 +30,8 @@
3030
class Ray(array_dataclass.DataclassArray):
3131
"""Dummy dataclass array for testing."""
3232

33-
pos: FloatArray[..., 3]
34-
dir: FloatArray[..., 3]
33+
pos: FloatArray[..., 3] # pyrefly: ignore[not-a-type]
34+
dir: FloatArray[..., 3] # pyrefly: ignore[not-a-type]
3535

3636

3737
# TODO(epot): Should use `chex.assert_xyz` once dataclasses support DM `tree`

0 commit comments

Comments
 (0)