Skip to content

Commit

Permalink
refactor: more type hints (unrelated)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Oct 30, 2023
1 parent c797e74 commit cbca32e
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 56 deletions.
29 changes: 25 additions & 4 deletions src/awkward/contents/bitmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@
)
from awkward._regularize import is_integer, is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.bytemaskedarray import ByteMaskedArray
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.forms.bitmaskedform import BitMaskedForm
from awkward.forms.form import Form
from awkward.index import Index
Expand Down Expand Up @@ -675,15 +687,24 @@ def _nbytes_part(self):
def _pad_none(self, target, axis, depth, clip):
return self.to_ByteMaskedArray()._pad_none(target, axis, depth, clip)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
return self.to_ByteMaskedArray()._to_arrow(
pyarrow, mask_node, validbytes, length, options
)

def _to_backend_array(self, allow_missing, backend):
return self.to_ByteMaskedArray()._to_backend_array(allow_missing, backend)

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
branch, depth = self.branch_depth
if branch or options["drop_nones"] or depth > 1:
return self.project()._remove_structure(backend, options)
Expand Down
29 changes: 25 additions & 4 deletions src/awkward/contents/bytemaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@
)
from awkward._regularize import is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.errors import AxisError
from awkward.forms.bytemaskedform import ByteMaskedForm
from awkward.forms.form import Form
Expand Down Expand Up @@ -1031,7 +1043,14 @@ def _pad_none(self, target, axis, depth, clip):
parameters=self._parameters,
)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
this_validbytes = self.mask_as_bool(valid_when=True)

return self._content._to_arrow(
Expand All @@ -1045,7 +1064,9 @@ def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_backend_array(self, allow_missing, backend):
return self.to_IndexedOptionArray64()._to_backend_array(allow_missing, backend)

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
branch, depth = self.branch_depth
if branch or options["drop_nones"] or depth > 1:
return self.project()._remove_structure(backend, options)
Expand Down
34 changes: 28 additions & 6 deletions src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ class RecursivelyApplyOptionsType(TypedDict):
function_name: str | None


class RemoveStructureOptionsType(TypedDict):
flatten_records: bool
function_name: str
drop_nones: bool
keepdims: bool
allow_records: bool
list_to_regular: bool


class ToArrowOptionsType(TypedDict):
list_to32: bool
string_to32: bool
bytestring_to32: bool
emptyarray_to: np.dtype | None
categorical_as_dictionary: bool
extensionarray: bool
count_nulls: bool
record_is_scalar: bool


class Content:
is_numpy = False
is_unknown = False
Expand Down Expand Up @@ -1066,10 +1086,10 @@ def to_arrow(
def _to_arrow(
self,
pyarrow: Any,
mask_node: Any,
validbytes: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: dict[str, Any],
options: ToArrowOptionsType,
):
raise NotImplementedError

Expand All @@ -1091,16 +1111,18 @@ def drop_none(self):
def _drop_none(self) -> Content:
raise NotImplementedError

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
raise NotImplementedError

def _recursively_apply(
self,
action: ActionType,
behavior: dict | None,
depth: int,
depth_context: dict | None,
lateral_context: dict | None,
depth_context: dict[str, Any] | None,
lateral_context: dict[str, Any] | None,
options: RecursivelyApplyOptionsType,
) -> Content | None:
raise NotImplementedError
Expand Down
29 changes: 25 additions & 4 deletions src/awkward/contents/emptyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@
from awkward._nplikes.shape import ShapeItem
from awkward._regularize import is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.errors import AxisError
from awkward.forms.emptyform import EmptyForm
from awkward.forms.form import Form
Expand Down Expand Up @@ -344,7 +356,14 @@ def _pad_none(self, target, axis, depth, clip):
else:
return self._pad_none_axis0(target, True)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
if options["emptyarray_to"] is None:
return pyarrow.Array.from_buffers(
ak._connect.pyarrow.to_awkwardarrow_type(
Expand Down Expand Up @@ -373,7 +392,9 @@ def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_backend_array(self, allow_missing, backend):
return backend.nplike.empty(0, dtype=np.float64)

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
return [self]

def _recursively_apply(
Expand Down
29 changes: 25 additions & 4 deletions src/awkward/contents/indexedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@
)
from awkward._regularize import is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.indexedform import IndexedForm
Expand Down Expand Up @@ -984,7 +996,14 @@ def _pad_none(self, target, axis, depth, clip):
parameters=self._parameters,
)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
if (
not options["categorical_as_dictionary"]
and self.parameter("__array__") == "categorical"
Expand Down Expand Up @@ -1036,7 +1055,9 @@ def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_backend_array(self, allow_missing, backend):
return self.project()._to_backend_array(allow_missing, backend)

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
return self.project()._remove_structure(backend, options)

def _recursively_apply(
Expand Down
29 changes: 25 additions & 4 deletions src/awkward/contents/indexedoptionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@
)
from awkward._regularize import is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.indexedoptionform import IndexedOptionForm
Expand Down Expand Up @@ -1522,7 +1534,14 @@ def _pad_none(self, target, axis, depth, clip):
parameters=self._parameters,
)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
index = numpy.asarray(self._index.data, copy=True)
this_validbytes = self.mask_as_bool(valid_when=True)
index[~this_validbytes] = 0
Expand Down Expand Up @@ -1600,7 +1619,9 @@ def _to_backend_array(self, allow_missing, backend):
else:
return content

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
branch, depth = self.branch_depth
if branch or options["drop_nones"] or depth > 1:
return self.project()._remove_structure(backend, options)
Expand Down
29 changes: 25 additions & 4 deletions src/awkward/contents/listarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@
)
from awkward._regularize import is_integer_like
from awkward._slicing import NO_HEAD
from awkward._typing import TYPE_CHECKING, Callable, Final, Self, SupportsIndex, final
from awkward._typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Self,
SupportsIndex,
final,
)
from awkward._util import UNSET
from awkward.contents.content import Content
from awkward.contents.content import (
Content,
RemoveStructureOptionsType,
ToArrowOptionsType,
)
from awkward.contents.listoffsetarray import ListOffsetArray
from awkward.forms.form import Form
from awkward.forms.listform import ListForm
Expand Down Expand Up @@ -1476,7 +1488,14 @@ def _pad_none(self, target, axis, depth, clip):
target, axis, depth, clip=True
)

def _to_arrow(self, pyarrow, mask_node, validbytes, length, options):
def _to_arrow(
self,
pyarrow: Any,
mask_node: Content | None,
validbytes: Content | None,
length: int,
options: ToArrowOptionsType,
):
return self.to_ListOffsetArray64(False)._to_arrow(
pyarrow, mask_node, validbytes, length, options
)
Expand All @@ -1490,7 +1509,9 @@ def _to_backend_array(self, allow_missing, backend):
else:
return self.to_RegularArray()._to_backend_array(allow_missing, backend)

def _remove_structure(self, backend, options):
def _remove_structure(
self, backend: Backend, options: RemoveStructureOptionsType
) -> list[Content]:
return self.to_ListOffsetArray64(False)._remove_structure(backend, options)

def _drop_none(self) -> Content:
Expand Down
Loading

0 comments on commit cbca32e

Please sign in to comment.