Skip to content

Commit 7df3645

Browse files
committed
fix: merging
1 parent 573b64d commit 7df3645

File tree

12 files changed

+42
-45
lines changed

12 files changed

+42
-45
lines changed

src/awkward/_do/content.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
import awkward as ak
1010
from awkward._backends.backend import Backend
1111
from awkward._nplikes.numpy_like import NumpyMetadata
12-
from awkward._typing import Any, AxisMaybeNone, Literal
13-
from awkward.contents.content import ActionType, Content
12+
from awkward._typing import TYPE_CHECKING, Any, AxisMaybeNone, Literal
1413
from awkward.errors import AxisError
1514
from awkward.forms import form
16-
from awkward.record import Record
1715

1816
np = NumpyMetadata.instance()
1917

18+
if TYPE_CHECKING:
19+
from awkward.contents.content import ActionType, Content
20+
from awkward.record import Record
21+
2022

2123
def recursively_apply(
2224
layout: Content | Record,
@@ -32,6 +34,9 @@ def recursively_apply(
3234
function_name: str | None = None,
3335
regular_to_jagged=False,
3436
) -> Content | Record | None:
37+
from awkward.contents.content import Content
38+
from awkward.record import Record
39+
3540
if isinstance(layout, Content):
3641
return layout._recursively_apply(
3742
action,
@@ -201,6 +206,8 @@ def remove_structure(
201206
allow_records: bool = False,
202207
list_to_regular: bool = False,
203208
):
209+
from awkward.record import Record
210+
204211
if isinstance(layout, Record):
205212
return remove_structure(
206213
layout._array[layout._at : layout._at + 1],

src/awkward/_do/meta.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@
2727

2828
def is_option(
2929
meta: Meta
30-
) -> TypeGuard[
31-
IndexedOptionMeta | BitMaskedMeta | ByteMaskedMeta | UnmaskedMeta
32-
]:
30+
) -> TypeGuard[IndexedOptionMeta | BitMaskedMeta | ByteMaskedMeta | UnmaskedMeta]:
3331
return meta.is_option
3432

3533

36-
def is_list(
37-
meta: Meta
38-
) -> TypeGuard[RegularMeta | ListOffsetMeta | ListMeta]:
34+
def is_list(meta: Meta) -> TypeGuard[RegularMeta | ListOffsetMeta | ListMeta]:
3935
return meta.is_list
4036

4137

@@ -59,19 +55,13 @@ def is_indexed(meta: Meta) -> TypeGuard[IndexedOptionMeta, IndexedMeta]:
5955
return meta.is_indexed
6056

6157

62-
class ImplementsTuple(RecordMeta): # Intersection
63-
_fields: None
64-
65-
66-
def is_record_tuple(meta: Meta) -> TypeGuard[ImplementsTuple]:
58+
# FIXME: narrow this to have `is_tuple` be a const True
59+
def is_record_tuple(meta: Meta) -> TypeGuard[RecordMeta]:
6760
return meta.is_record and meta.is_tuple
6861

6962

70-
class ImplementsRecord(RecordMeta):
71-
_fields: list[str]
72-
73-
74-
def is_record_record(meta: Meta) -> TypeGuard[ImplementsRecord]:
63+
# FIXME: narrow this to have `is_tuple` be a const False
64+
def is_record_record(meta: Meta) -> TypeGuard[RecordMeta]:
7565
return meta.is_record and not meta.is_tuple
7666

7767

src/awkward/_meta/numpymeta.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
from awkward._do.meta import is_indexed, is_numpy, is_option
66
from awkward._meta.meta import Meta
7+
from awkward._nplikes.numpy_like import NumpyMetadata
78
from awkward._nplikes.shape import ShapeItem
89
from awkward._parameters import type_parameters_equal
9-
from awkward._typing import TYPE_CHECKING, JSONSerializable
10+
from awkward._typing import TYPE_CHECKING, DType, JSONSerializable
1011

12+
np = NumpyMetadata.instance()
1113
if TYPE_CHECKING:
1214
from awkward._meta.regularmeta import RegularMeta
1315

@@ -17,6 +19,10 @@ class NumpyMeta(Meta):
1719
is_leaf = True
1820
inner_shape: tuple[ShapeItem, ...]
1921

22+
@property
23+
def dtype(self) -> DType:
24+
raise NotImplementedError
25+
2026
def purelist_parameters(self, *keys: str) -> JSONSerializable:
2127
if self._parameters is not None:
2228
for key in keys:
@@ -104,9 +110,9 @@ def _mergeable_next(self, other: Meta, mergebool: bool) -> bool:
104110

105111
# Default merging (can we cast one to the other)
106112
else:
107-
return self.backend.nplike.can_cast(
108-
self.dtype, other.dtype
109-
) or self.backend.nplike.can_cast(other.dtype, self.dtype)
113+
return np.can_cast(
114+
self.dtype, other.dtype, casting="same_kind"
115+
) or np.can_cast(other.dtype, self.dtype, casting="same_kind")
110116

111117
else:
112118
return False

src/awkward/_meta/recordmeta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ def _mergeable_next(self, other: Meta, mergebool: bool) -> bool:
170170
return False
171171

172172
elif is_record_record(self) and is_record_record(other):
173-
if set(self._fields) != set(other._fields):
173+
if set(self._fields) != set(other._fields): # type: ignore[arg-type]
174174
return False
175175

176-
for i, field in enumerate(self._fields):
176+
for i, field in enumerate(self._fields): # type: ignore[arg-type]
177177
x = self._contents[i]
178178
y = other.contents[other.field_to_index(field)]
179179
if not x._mergeable_next(y, mergebool):

src/awkward/_nplikes/array_module.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,6 @@ def astype(
653653
assert not isinstance(x, PlaceholderArray)
654654
return x.astype(dtype, copy=copy) # type: ignore[attr-defined]
655655

656-
def can_cast(
657-
self, from_: DTypeLike | ArrayLikeT, to: DTypeLike | ArrayLikeT
658-
) -> bool:
659-
return self._module.can_cast(from_, to, casting="same_kind")
660-
661656
@classmethod
662657
def is_own_array(cls, obj) -> bool:
663658
return cls.is_own_array_type(type(obj))

src/awkward/_nplikes/numpy_like.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class NumpyMetadata(PublicSingleton):
8383
datetime_data = staticmethod(numpy.datetime_data)
8484
issubdtype = staticmethod(numpy.issubdtype)
8585

86-
AxisError = numpy.AxisError
86+
AxisError = staticmethod(numpy.AxisError)
87+
can_cast = staticmethod(numpy.can_cast)
8788

8889

8990
if hasattr(numpy, "float16"):
@@ -537,10 +538,6 @@ def astype(
537538
) -> ArrayLikeT:
538539
...
539540

540-
@abstractmethod
541-
def can_cast(self, from_: DType | ArrayLikeT, to: DType | ArrayLikeT) -> bool:
542-
...
543-
544541
@abstractmethod
545542
def is_c_contiguous(self, x: ArrayLikeT | PlaceholderArray) -> bool:
546543
...

src/awkward/_nplikes/typetracer.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,11 +1512,6 @@ def astype(
15121512
x.touch_data()
15131513
return TypeTracerArray._new(np.dtype(dtype), x.shape)
15141514

1515-
def can_cast(
1516-
self, from_: DTypeLike | TypeTracerArray, to: DTypeLike | TypeTracerArray
1517-
) -> bool:
1518-
return numpy.can_cast(from_, to, casting="same_kind")
1519-
15201515
@classmethod
15211516
def is_own_array_type(cls, type_: type) -> bool:
15221517
return issubclass(type_, TypeTracerArray)

src/awkward/forms/numpyform.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,9 @@ def _expected_from_buffers(
273273

274274
def _to_regular_primitive(self) -> RegularForm | NumpyForm:
275275
return self.to_RegularForm()
276+
277+
@property
278+
def dtype(self) -> DType:
279+
from awkward.types.numpytype import primitive_to_dtype
280+
281+
return primitive_to_dtype(self.primitive)

src/awkward/operations/ak_strings_astype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import awkward as ak
66
from awkward._dispatch import high_level_function
7-
from awkward._do.content import recursively_apply
7+
from awkward._do.content import pad_none, recursively_apply
88
from awkward._layout import HighLevelContext
99
from awkward._nplikes.numpy import Numpy
1010
from awkward._nplikes.numpy_like import NumpyMetadata
@@ -68,7 +68,7 @@ def action(layout, **kwargs):
6868
layout, highlevel=False, behavior=behavior
6969
)
7070
max_length = ak.operations.max(ak.operations.num(layout, behavior=behavior))
71-
regulararray = ak._do.pad_none(layout, max_length, 1)
71+
regulararray = pad_none(layout, max_length, 1)
7272
maskedarray = ak.operations.to_numpy(regulararray, allow_missing=True)
7373
npstrings = maskedarray.data
7474
if maskedarray.mask is not False:

src/awkward/operations/ak_values_astype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import awkward as ak
66
from awkward._dispatch import high_level_function
7+
from awkward._do.content import numbers_to_type
78
from awkward._layout import HighLevelContext
89
from awkward._nplikes.numpy_like import NumpyMetadata
910

@@ -73,5 +74,5 @@ def _impl(array, to, including_unknown, highlevel, behavior, attrs):
7374
with HighLevelContext(behavior=behavior, attrs=attrs) as ctx:
7475
layout = ctx.unwrap(array, allow_record=False, primitive_policy="error")
7576
to_str = ak.types.numpytype.dtype_to_primitive(np.dtype(to))
76-
out = ak._do.numbers_to_type(layout, to_str, including_unknown)
77+
out = numbers_to_type(layout, to_str, including_unknown)
7778
return ctx.wrap(out, highlevel=highlevel)

0 commit comments

Comments
 (0)