Skip to content

Commit

Permalink
fix typing & enforce tuple record field names as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pfackeldey committed Nov 22, 2024
1 parent 6007156 commit 05a0fd2
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/awkward/contents/bitmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
ToArrowOptions,
)
from awkward.forms.bitmaskedform import BitMaskedForm
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.index import Index

if TYPE_CHECKING:
Expand Down Expand Up @@ -288,7 +288,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> BitMaskedFo
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> BitMaskedForm:
def _form_with_key_path(self, path: FormKeyPathT) -> BitMaskedForm:
return self.form_cls(
self._mask.form,
self._content._form_with_key_path((*path, None)),
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/bytemaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
)
from awkward.errors import AxisError
from awkward.forms.bytemaskedform import ByteMaskedForm
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.index import Index

if TYPE_CHECKING:
Expand Down Expand Up @@ -218,7 +218,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ByteMaskedF
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> ByteMaskedForm:
def _form_with_key_path(self, path: FormKeyPathT) -> ByteMaskedForm:
return self.form_cls(
self._mask.form,
self._content._form_with_key_path((*path, None)),
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
TypedDict,
)
from awkward._util import UNSET
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.index import Index, Index64

if TYPE_CHECKING:
Expand Down Expand Up @@ -241,7 +241,7 @@ def _form_with_key(
def form_with_key_path(self, root: (str | int | None) = ()) -> Form:
return self._form_with_key_path(root)

def _form_with_key_path(self, path: (str | int | None)) -> Form:
def _form_with_key_path(self, path: FormKeyPathT) -> Form:
raise NotImplementedError

@property
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/emptyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from awkward.errors import AxisError
from awkward.forms.emptyform import EmptyForm
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.index import Index

if TYPE_CHECKING:
Expand Down Expand Up @@ -118,7 +118,7 @@ def simplified(cls, *, parameters=None, backend=None):
def _form_with_key(self, getkey: Callable[[Content], str | None]) -> EmptyForm:
return self.form_cls(form_key=getkey(self))

def _form_with_key_path(self, path: (str | int | None)) -> EmptyForm:
def _form_with_key_path(self, path: FormKeyPathT) -> EmptyForm:
return self.form_cls(form_key=repr(path))

def _to_buffers(
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/indexedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.indexedform import IndexedForm
from awkward.index import Index

Expand Down Expand Up @@ -214,7 +214,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> IndexedForm
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> IndexedForm:
def _form_with_key_path(self, path: FormKeyPathT) -> IndexedForm:
return self.form_cls(
self._index.form,
self._content._form_with_key_path((*path, None)),
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/indexedoptionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.indexedoptionform import IndexedOptionForm
from awkward.index import Index

Expand Down Expand Up @@ -202,7 +202,7 @@ def _form_with_key(
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> IndexedOptionForm:
def _form_with_key_path(self, path: FormKeyPathT) -> IndexedOptionForm:
return self.form_cls(
self._index.form,
self._content._form_with_key_path((*path, None)),
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/listarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
ToArrowOptions,
)
from awkward.contents.listoffsetarray import ListOffsetArray
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.listform import ListForm
from awkward.index import Index

Expand Down Expand Up @@ -207,7 +207,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ListForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> ListForm:
def _form_with_key_path(self, path: FormKeyPathT) -> ListForm:
return self.form_cls(
self._starts.form,
self._stops.form,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/listoffsetarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.listoffsetform import ListOffsetForm
from awkward.index import Index, Index64

Expand Down Expand Up @@ -199,7 +199,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ListOffsetF
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> ListOffsetForm:
def _form_with_key_path(self, path: FormKeyPathT) -> ListOffsetForm:
return self.form_cls(
self._offsets.form,
self._content._form_with_key_path((*path, None)),
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/numpyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.numpyform import NumpyForm
from awkward.index import Index
from awkward.types.numpytype import primitive_to_dtype
Expand Down Expand Up @@ -200,7 +200,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> NumpyForm:
form_key=getkey(self),
)

def _form_with_key_path(self, path: (str | int | None)) -> NumpyForm:
def _form_with_key_path(self, path: FormKeyPathT) -> NumpyForm:
return self.form_cls(
ak.types.numpytype.dtype_to_primitive(self._data.dtype),
self.inner_shape,
Expand Down
20 changes: 9 additions & 11 deletions src/awkward/contents/recordarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.recordform import RecordForm
from awkward.index import Index
from awkward.record import Record
Expand Down Expand Up @@ -316,16 +316,14 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> RecordForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> RecordForm:
if self._fields is None:
contents = [
x._form_with_key_path((*path, i)) for i, x in enumerate(self._contents)
]
else:
contents = [
x._form_with_key_path((*path, k))
for k, x in zip(self._fields, self._contents)
]
def _form_with_key_path(self, path: FormKeyPathT) -> RecordForm:
# explicitely use `self.fields` instead of `self._fields`,
# because we want string-typed field names in the path -
# also for tuple records
contents = [
x._form_with_key_path((*path, k))
for k, x in zip(self.fields, self._contents)
]

return self.form_cls(
contents,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/regulararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
RemoveStructureOptions,
ToArrowOptions,
)
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.regularform import RegularForm
from awkward.index import Index

Expand Down Expand Up @@ -211,7 +211,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> RegularForm
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> RegularForm:
def _form_with_key_path(self, path: FormKeyPathT) -> RegularForm:
return self.form_cls(
self._content._form_with_key_path((*path, None)),
self._size,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/unionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.unionform import UnionForm
from awkward.index import Index, Index8, Index64

Expand Down Expand Up @@ -460,7 +460,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> UnionForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> UnionForm:
def _form_with_key_path(self, path: FormKeyPathT) -> UnionForm:
return self.form_cls(
self._tags.form,
self._index.form,
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/contents/unmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToArrowOptions,
)
from awkward.errors import AxisError
from awkward.forms.form import Form
from awkward.forms.form import Form, FormKeyPathT
from awkward.forms.unmaskedform import UnmaskedForm
from awkward.index import Index

Expand Down Expand Up @@ -138,7 +138,7 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> UnmaskedFor
form_key=form_key,
)

def _form_with_key_path(self, path: (str | int | None)) -> UnmaskedForm:
def _form_with_key_path(self, path: FormKeyPathT) -> UnmaskedForm:
return self.form_cls(
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
Expand Down
4 changes: 4 additions & 0 deletions src/awkward/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
Iterator,
JSONMapping,
Self,
TypeAlias,
Union,
Tuple,
)

__all__ = ("from_dict", "from_type", "from_json", "reserved_nominal_parameters", "Form")

np = NumpyMetadata.instance()
numpy_backend = NumpyBackend.instance()

FormKeyPathT: TypeAlias = Tuple[Union[str, int, None], ...]

reserved_nominal_parameters: Final = frozenset(
{
Expand Down

0 comments on commit 05a0fd2

Please sign in to comment.