Skip to content

Commit

Permalink
chore: run mypy on types (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Nov 1, 2023
1 parent 9dc10a0 commit aed7f20
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 160 deletions.
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,18 @@ module = [
'awkward._behavior.*',
'awkward._backends.*',
'awkward.forms.*',
'awkward.types.*',
]
ignore_errors = false
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = [
'awkward.types._awkward_datashape_parser'
]
ignore_errors = true
ignore_missing_imports = true

[tool.ruff]
src = ["src"]
extend-exclude = [
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def type_parameters_equal(


def parameters_are_equal(
one: JSONMapping, two: JSONMapping, only_array_record=False
one: JSONMapping | None, two: JSONMapping | None, only_array_record=False
) -> bool:
if one is None and two is None:
return True
Expand Down
29 changes: 17 additions & 12 deletions src/awkward/types/arraytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
from __future__ import annotations

import sys
from collections.abc import Mapping

import awkward as ak
from awkward._nplikes.shape import unknown_length
from awkward._nplikes.shape import ShapeItem, unknown_length
from awkward._regularize import is_integer
from awkward._typing import Any
from awkward.types.type import Type


class ArrayType:
def __init__(self, content, length, behavior=None):
def __init__(
self, content: Type, length: ShapeItem, behavior: Mapping | None = None
):
if not isinstance(content, ak.types.Type):
raise TypeError(
"{} all 'contents' must be Type subclasses, not {}".format(
Expand All @@ -22,29 +27,29 @@ def __init__(self, content, length, behavior=None):
type(self).__name__, repr(length)
)
)
self._content = content
self._length = length
self._behavior = behavior
self._content: Type = content
self._length: ShapeItem = length
self._behavior: Mapping | None = behavior

@property
def content(self):
def content(self) -> Type:
return self._content

@property
def length(self):
def length(self) -> ShapeItem:
return self._length

@property
def behavior(self):
def behavior(self) -> Mapping | None:
return self._behavior

def __str__(self):
def __str__(self) -> str:
return "".join(self._str("", True))

def show(self, stream=sys.stdout):
stream.write("".join([*self._str("", False), "\n"]))

def _str(self, indent, compact):
def _str(self, indent: str, compact: bool) -> list[str]:
return [
f"{self._length} * ",
*self._content._str(
Expand All @@ -54,11 +59,11 @@ def _str(self, indent, compact):
),
]

def __repr__(self):
def __repr__(self) -> str:
args = [repr(self._content), repr(self._length), repr(self._behavior)]
return "{}({})".format(type(self).__name__, ", ".join(args))

def is_equal_to(self, other, *, all_parameters: bool = False) -> bool:
def is_equal_to(self, other: Any, *, all_parameters: bool = False) -> bool:
return (
isinstance(other, type(self))
and (
Expand Down
35 changes: 21 additions & 14 deletions src/awkward/types/listtype.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
from __future__ import annotations

from collections.abc import Mapping

from awkward._behavior import find_array_typestr
from awkward._parameters import parameters_are_equal, type_parameters_equal
from awkward._typing import Self, final
from awkward._util import UNSET
from awkward._typing import Any, JSONMapping, Self, final
from awkward._util import UNSET, Sentinel
from awkward.types.type import Type


@final
class ListType(Type):
def copy(self, *, content: Type = UNSET, parameters=UNSET) -> Self:
def copy(
self,
*,
content: Type | Sentinel = UNSET,
parameters: JSONMapping | Sentinel | None = UNSET,
) -> Self:
return ListType(
self._content if content is UNSET else content,
parameters=self._parameters if parameters is UNSET else parameters,
self._content if content is UNSET else content, # type: ignore[arg-type]
parameters=self._parameters if parameters is UNSET else parameters, # type: ignore[arg-type]
)

def __init__(self, content, *, parameters=None):
def __init__(self, content: Type, *, parameters: JSONMapping | None = None):
if not isinstance(content, Type):
raise TypeError(
"{} 'content' must be a Type subtype, not {}".format(
type(self).__name__, repr(content)
)
)
if parameters is not None and not isinstance(parameters, dict):
if parameters is not None and not isinstance(parameters, Mapping):
raise TypeError(
"{} 'parameters' must be of type dict or None, not {}".format(
"{} 'parameters' must be of type Mapping or None, not {}".format(
type(self).__name__, repr(parameters)
)
)
self._content = content
self._parameters = parameters
self._content: Type = content
self._parameters: JSONMapping | None = parameters

@property
def content(self):
def content(self) -> Type:
return self._content

def _get_typestr(self, behavior) -> str | None:
def _get_typestr(self, behavior: Mapping | None) -> str | None:
typestr = find_array_typestr(behavior, self._parameters)
if typestr is not None:
return typestr
Expand All @@ -52,7 +59,7 @@ def _get_typestr(self, behavior) -> str | None:
else:
return None

def _str(self, indent, compact, behavior):
def _str(self, indent: str, compact: bool, behavior: Mapping | None) -> list[str]:
typestr = self._get_typestr(behavior)
if typestr is not None:
out = [typestr]
Expand All @@ -73,7 +80,7 @@ def __repr__(self):
args = [repr(self._content), *self._repr_args()]
return "{}({})".format(type(self).__name__, ", ".join(args))

def _is_equal_to(self, other, all_parameters: bool):
def _is_equal_to(self, other: Any, all_parameters: bool) -> bool:
compare_parameters = (
parameters_are_equal if all_parameters else type_parameters_equal
)
Expand Down
46 changes: 26 additions & 20 deletions src/awkward/types/numpytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import json
import re
from collections.abc import Mapping

from awkward._behavior import find_array_typestr
from awkward._nplikes.numpylike import NumpyMetadata
from awkward._parameters import parameters_are_equal, type_parameters_equal
from awkward._typing import Self, final
from awkward._util import UNSET
from awkward._typing import Any, JSONMapping, cast, final
from awkward._util import UNSET, Sentinel
from awkward.types.type import Type

np = NumpyMetadata.instance()
Expand Down Expand Up @@ -94,51 +95,56 @@ def dtype_to_primitive(dtype):

@final
class NumpyType(Type):
def copy(self, *, primitive: Type = UNSET, parameters=UNSET) -> Self:
def copy(
self,
*,
primitive: str | Sentinel = UNSET,
parameters: JSONMapping | Sentinel | None = UNSET,
) -> NumpyType:
return NumpyType(
self._primitive if primitive is UNSET else primitive,
parameters=self._parameters if parameters is UNSET else parameters,
self._primitive if primitive is UNSET else primitive, # type: ignore[arg-type]
parameters=self._parameters if parameters is UNSET else parameters, # type: ignore[arg-type]
)

def __init__(self, primitive, *, parameters=None):
def __init__(self, primitive: str, *, parameters: JSONMapping | None = None):
primitive = dtype_to_primitive(primitive_to_dtype(primitive))
if parameters is not None and not isinstance(parameters, dict):
if parameters is not None and not isinstance(parameters, Mapping):
raise TypeError(
"{} 'parameters' must be of type dict or None, not {}".format(
"{} 'parameters' must be of type Mapping or None, not {}".format(
type(self).__name__, repr(parameters)
)
)
self._primitive = primitive
self._parameters = parameters
self._primitive: str = primitive
self._parameters: JSONMapping | None = parameters

@property
def primitive(self):
def primitive(self) -> str:
return self._primitive

_str_parameters_exclude = ("__categorical__", "__unit__")
_str_parameters_exclude: tuple[str, ...] = ("__categorical__", "__unit__")

def _get_typestr(self, behavior) -> str | None:
def _get_typestr(self, behavior: Mapping | None) -> str | None:
typestr = find_array_typestr(behavior, self._parameters)
if typestr is not None:
return typestr

if self._parameters is None:
return None

name = self._parameters.get("__array__")
name = cast("str | None", self._parameters.get("__array__"))
if name in {"byte", "char"}:
return name

return None

def _str(self, indent, compact, behavior):
def _str(self, indent: str, compact: bool, behavior: Mapping | None) -> list[str]:
typestr = self._get_typestr(behavior)
if typestr is not None:
out = [typestr]

else:
if self.parameter("__unit__") is not None:
numpy_unit = str(np.dtype("M8[" + self._parameters["__unit__"] + "]"))
if (unit := cast(str, self.parameter("__unit__"))) is not None:
numpy_unit = str(np.dtype(f"M8[{unit}]"))
bracket_index = numpy_unit.index("[")
units = "unit=" + json.dumps(numpy_unit[bracket_index + 1 : -1])
else:
Expand All @@ -155,15 +161,15 @@ def _str(self, indent, compact, behavior):
units = ""
elif params is None:
params = ""
out = [self._primitive, "[", units, params, "]"]
out = [self._primitive, "[", units, params, "]"] # type: ignore[list-item]

return [self._str_categorical_begin(), *out, self._str_categorical_end()]

def __repr__(self):
def __repr__(self) -> str:
args = [repr(self._primitive), *self._repr_args()]
return "{}({})".format(type(self).__name__, ", ".join(args))

def _is_equal_to(self, other, all_parameters: bool) -> bool:
def _is_equal_to(self, other: Any, all_parameters: bool) -> bool:
compare_parameters = (
parameters_are_equal if all_parameters else type_parameters_equal
)
Expand Down
37 changes: 22 additions & 15 deletions src/awkward/types/optiontype.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
from __future__ import annotations

from collections.abc import Mapping

from awkward._behavior import find_array_typestr
from awkward._parameters import (
parameters_are_equal,
parameters_union,
type_parameters_equal,
)
from awkward._typing import Self, final
from awkward._util import UNSET
from awkward._typing import Any, JSONMapping, final
from awkward._util import UNSET, Sentinel
from awkward.types.listtype import ListType
from awkward.types.regulartype import RegularType
from awkward.types.type import Type
Expand All @@ -17,33 +19,38 @@

@final
class OptionType(Type):
def copy(self, *, content: Type = UNSET, parameters=UNSET) -> Self:
def copy(
self,
*,
content: Type | Sentinel = UNSET,
parameters: JSONMapping | Sentinel | None = UNSET,
) -> OptionType:
return OptionType(
self._content if content is UNSET else content,
parameters=self._parameters if parameters is UNSET else parameters,
self._content if content is UNSET else content, # type: ignore[arg-type]
parameters=self._parameters if parameters is UNSET else parameters, # type: ignore[arg-type]
)

def __init__(self, content, *, parameters=None):
def __init__(self, content: Type, *, parameters: JSONMapping | None = None):
if not isinstance(content, Type):
raise TypeError(
"{} 'content' must be a Type subclass, not {}".format(
type(self).__name__, repr(content)
)
)
if parameters is not None and not isinstance(parameters, dict):
if parameters is not None and not isinstance(parameters, Mapping):
raise TypeError(
"{} 'parameters' must be of type dict or None, not {}".format(
"{} 'parameters' must be of type Mapping or None, not {}".format(
type(self).__name__, repr(parameters)
)
)
self._content = content
self._parameters = parameters
self._content: Type = content
self._parameters: JSONMapping | None = parameters

@property
def content(self):
def content(self) -> Type:
return self._content

def _str(self, indent, compact, behavior):
def _str(self, indent: str, compact: bool, behavior: Mapping | None) -> list[str]:
typestr = find_array_typestr(behavior, self._parameters)

head = []
Expand Down Expand Up @@ -80,11 +87,11 @@ def _str(self, indent, compact, behavior):
*tail,
]

def __repr__(self):
def __repr__(self) -> str:
args = [repr(self._content), *self._repr_args()]
return "{}({})".format(type(self).__name__, ", ".join(args))

def simplify_option_union(self):
def simplify_option_union(self) -> Type:
if isinstance(self._content, UnionType):
contents = []
for content in self._content.contents:
Expand All @@ -106,7 +113,7 @@ def simplify_option_union(self):
else:
return self

def _is_equal_to(self, other, all_parameters: bool) -> bool:
def _is_equal_to(self, other: Any, all_parameters: bool) -> bool:
compare_parameters = (
parameters_are_equal if all_parameters else type_parameters_equal
)
Expand Down
Loading

0 comments on commit aed7f20

Please sign in to comment.