Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: more Ruff #2184

Merged
merged 7 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion awkward-cpp/src/awkward_cpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import awkward_cpp.cpu_kernels # noqa: F401
import awkward_cpp.cpu_kernels
import awkward_cpp.libawkward # noqa: F401
3 changes: 2 additions & 1 deletion dev/generate-kernel-signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
"awkward_reduce_max",
"awkward_reduce_min",
"awkward_reduce_sum",
"awkward_reduce_sum_int32_bool_64" "awkward_reduce_sum_int64_bool_64",
"awkward_reduce_sum_int32_bool_64",
"awkward_reduce_sum_int64_bool_64",
Comment on lines +96 to +97
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes! Nice catch.

Copy link
Member Author

@henryiii henryiii Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what linters are for. :) (though Black helps here too by making it easy to see by eye, if someone was looking)

My hand rewrite of the template code is still slightly off, will have to find where things changed.

"awkward_reduce_sum_bool",
"awkward_reduce_prod_bool",
"awkward_reduce_countnonzero",
Expand Down
22 changes: 13 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ ignore_errors = true
ignore_missing_imports = true

[tool.ruff]
exclude = [
extend-exclude = [
"studies",
"pybind11",
"rapidjson",
Expand All @@ -269,17 +269,23 @@ ignore = [
"UP030", # {0} -> {}
]
select = [
"B0", # flake8-bugbear
"C", "E", "F", "W", # flake8
"I", # isort
"T20", # flake8-print
"UP", # pyupgrade
"TID251" # flake8-tidy-imports
"C", "E", "F", "W", # flake8
"B0", "B904", # flake8-bugbear
"T20", # flake8-print
"TID251", # flake8-tidy-imports
"ISC", # flake8-implicit-str-concat
"I", # isort
"UP", # pyupgrade
"PGH002", "PGH003", "PGH004", # pygrep-hooks
"C4", # flake8-comprehensions
"YTT", # flake8-2020
"RUF0", "RUF1" # Ruff-specific
]
target-version = "py37"
typing-modules = ["awkward.typing"]
src = ["src"]
unfixable = ["T20"]
external = ["AK101"]

mccabe.max-complexity = 100

Expand All @@ -293,8 +299,6 @@ mccabe.max-complexity = 100
"E402",
"F401",
"F403",
"F401",
"F403",
"I001",
]
"src/awkward/_connect/*" = [
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/_connect/numba/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def getitem_range(self, viewtype):
def getitem_field(self, viewtype, key):
if self.has_field(key):
return ak._connect.numba.arrayview.wrap(
self, viewtype, viewtype.fields + (key,)
self, viewtype, (*viewtype.fields, key)
)
else:
raise TypeError(f"array does not have a field with key {repr(key)}")
Expand Down
82 changes: 38 additions & 44 deletions src/awkward/_connect/rdataframe/from_rdataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ctypes
import os
import textwrap

import cppyy
import ROOT
Expand Down Expand Up @@ -55,11 +56,7 @@
compiler = ROOT.gInterpreter.Declare


done = compiler(
"""
#include "rdataframe/jagged_builders.h"
"""
)
done = compiler('\n#include "rdataframe/jagged_builders.h"\n')
assert done is True


Expand All @@ -68,55 +65,52 @@ def cpp_builder_type(depth, data_type):
if depth == 1:
return f"awkward::LayoutBuilder::Numpy<{data_type}>>"
else:
return (
"awkward::LayoutBuilder::ListOffset<int64_t, "
+ cpp_builder_type(depth - 1, data_type)
+ ">"
)
return f"awkward::LayoutBuilder::ListOffset<int64_t, {cpp_builder_type(depth - 1, data_type)}>"

def cpp_fill_offsets_and_flatten(depth):
if depth == 1:
return (
"\nfor (auto const& it : vec1) {\n" + " builder1.append(it);\n" + "}\n"
return textwrap.dedent(
"""
for (auto const& it : vec1) {
builder1.append(it);
}
"""
)
else:
return (
f"for (auto const& vec{depth - 1} : vec{depth}) "
+ "{\n"
+ f" auto& builder{depth - 1} = builder{depth}.begin_list();\n"
+ " "
+ cpp_fill_offsets_and_flatten(depth - 1)
+ "\n"
+ f" builder{depth}.end_list();\n"
+ "}\n"
return textwrap.dedent(
f"""
for (auto const& vec{depth - 1} : vec{depth}) {{
auto& builder{depth - 1} = builder{depth}.begin_list();
{cpp_fill_offsets_and_flatten(depth - 1)}
builder{depth}.end_list();
}}
"""
)

def cpp_fill_function(depth):
if depth == 1:
return (
"template<class BUILDER, typename PRIMITIVE>\n"
+ "void\n"
+ "fill_from(BUILDER& builder, ROOT::RDF::RResultPtr<std::vector<PRIMITIVE>>& result) {"
+ " for (auto const& it : result) {\n"
+ " builder.append(it);\n"
+ " }\n"
+ "}\n"
return textwrap.dedent(
"""
template<class BUILDER, typename PRIMITIVE>
void fill_from(BUILDER& builder, ROOT::RDF::RResultPtr<std::vector<PRIMITIVE>>& result) {
for (auto const& it : result) {
builder.append(it);
}
}
"""
)
else:
return (
"template<class BUILDER, typename PRIMITIVE>\n"
+ "void\n"
+ f"fill_offsets_and_flatten{depth}(BUILDER& builder{depth}, ROOT::RDF::RResultPtr<std::vector<PRIMITIVE>>& result) "
+ "{\n"
+ f" for (auto const& vec{depth - 1} : result) "
+ "{\n"
+ f" auto& builder{depth - 1} = builder{depth}.begin_list();\n"
+ " "
+ cpp_fill_offsets_and_flatten(depth - 1)
+ "\n"
+ f" builder{depth}.end_list();\n"
+ "}\n"
+ "}\n"
return textwrap.dedent(
f"""
template<class BUILDER, typename PRIMITIVE>
void fill_offsets_and_flatten{depth}(BUILDER& builder{depth}, ROOT::RDF::RResultPtr<std::vector<PRIMITIVE>>& result) {{
for (auto const& vec{depth - 1} : result) {{
auto& builder{depth - 1} = builder{depth}.begin_list();
{cpp_fill_offsets_and_flatten(depth - 1)}
builder{depth}.end_list();
}}
}}
"""
)

def form_dtype(form):
Expand All @@ -132,7 +126,7 @@ def form_dtype(form):
contents = {}
awkward_type_cols = {}

columns = columns + ("rdfentry_",)
columns = (*columns, "rdfentry_")
maybe_indexed = False

# Important note: This loop is separate from the next one
Expand Down
8 changes: 4 additions & 4 deletions src/awkward/_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ def _cast(cls, x, t):
return x

def __call__(self, *args) -> None:
assert len(args) == len(self._impl.argtypes) # type: ignore
assert len(args) == len(self._impl.argtypes)

return self._impl(
*(self._cast(x, t) for x, t in zip(args, self._impl.argtypes)) # type: ignore
*(self._cast(x, t) for x, t in zip(args, self._impl.argtypes))
)


class JaxKernel(NumpyKernel):
def __call__(self, *args) -> None:
assert len(args) == len(self._impl.argtypes) # type: ignore
assert len(args) == len(self._impl.argtypes)

if not any(Jax.is_tracer(arg) for arg in args):
return super().__call__(*args)
Expand Down Expand Up @@ -133,7 +133,7 @@ def __call__(self, *args) -> None:
[],
)

assert len(args) == len(self._impl.dir) # type: ignore
assert len(args) == len(self._impl.dir)
# The first arg is the invocation index which raises itself by 8 in the kernel if there was no error before.
# The second arg is the error_code.
args = (
Expand Down
6 changes: 3 additions & 3 deletions src/awkward/_nplikes/numpylike.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def T(self) -> Self:
...

@abstractmethod
def __getitem__( # noqa: F811
def __getitem__(
self,
key: SupportsIndex
| slice
Expand All @@ -69,7 +69,7 @@ def __setitem__(
...

@overload
def __setitem__( # noqa: F811
def __setitem__(
self,
key: slice
| Ellipsis
Expand All @@ -80,7 +80,7 @@ def __setitem__( # noqa: F811
...

@abstractmethod
def __setitem__(self, key, value): # noqa: F811
def __setitem__(self, key, value):
...

@abstractmethod
Expand Down
8 changes: 4 additions & 4 deletions src/awkward/_nplikes/typetracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def strides(self):
self.touch_shape()
out = (self._dtype.itemsize,)
for x in self._shape[:0:-1]:
out = (x * out[0],) + out
out = (x * out[0], *out)
return out

@property
Expand Down Expand Up @@ -384,7 +384,7 @@ def __getitem__(
| Ellipsis
| tuple[SupportsIndex | slice | Ellipsis | ArrayLike, ...]
| ArrayLike,
) -> Self: # noqa: F811
) -> Self:
if not isinstance(key, tuple):
key = (key,)

Expand Down Expand Up @@ -514,7 +514,7 @@ def __setitem__(
| tuple[SupportsIndex | slice | Ellipsis | ArrayLike, ...]
| ArrayLike,
value: int | float | bool | complex | ArrayLike,
): # noqa: F811 existing_value = self.__getitem__(key)
):
existing_value = self.__getitem__(key)
if isinstance(value, TypeTracerArray) and value.ndim > existing_value.ndim:
raise wrap_error(ValueError("cannot assign shape larger than destination"))
Expand Down Expand Up @@ -1071,7 +1071,7 @@ def concat(self, arrays, *, axis: int | None = 0) -> TypeTracerArray:
)

return TypeTracerArray._new(
numpy.concatenate(emptyarrays).dtype, (unknown_length,) + inner_shape
numpy.concatenate(emptyarrays).dtype, (unknown_length, *inner_shape)
)

def repeat(
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/behaviors/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _HashableDict:
def __init__(self, obj):
self.keys = tuple(sorted(obj))
self.values = tuple(_as_hashable(obj[k]) for k in self.keys)
self.hash = hash((_HashableDict,) + self.keys, self.values)
self.hash = hash((_HashableDict, *self.keys), self.values)

def __hash__(self):
return self.hash
Expand All @@ -35,7 +35,7 @@ def __eq__(self, other):
class _HashableList:
def __init__(self, obj):
self.values = tuple(obj)
self.hash = hash((_HashableList,) + self.values)
self.hash = hash((_HashableList, *self.values))

def __hash__(self):
return self.hash
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/behaviors/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def register(cls):
if rhs is None:
registry.setdefault((ufunc, behavior_name), method)
continue
for rhs_name in list(rhs) + [behavior_name]:
for rhs_name in [*list(rhs), behavior_name]:
registry.setdefault((ufunc, behavior_name, rhs_name), method)
if transpose is not None and rhs_name != behavior_name:
registry.setdefault(
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def _getitem_next_ellipsis(self, tail, advanced: Index | None):
)

else:
return self._getitem_next(slice(None), (Ellipsis,) + tail, advanced)
return self._getitem_next(slice(None), (Ellipsis, *tail), advanced)

def _getitem_next_regular_missing(
self,
Expand Down
21 changes: 11 additions & 10 deletions src/awkward/contents/numpyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,24 +340,24 @@ def _getitem_next(
return self

elif isinstance(head, int):
where = (slice(None), head) + tail
where = (slice(None), head, *tail)

try:
out = self._data[where]
except IndexError as err:
raise ak._errors.index_error(self, (head,) + tail, str(err)) from err
raise ak._errors.index_error(self, (head, *tail), str(err)) from err

if hasattr(out, "shape") and len(out.shape) != 0:
return NumpyArray(out, parameters=None, backend=self._backend)
else:
return out

elif isinstance(head, slice) or head is np.newaxis or head is Ellipsis:
where = (slice(None), head) + tail
where = (slice(None), head, *tail)
try:
out = self._data[where]
except IndexError as err:
raise ak._errors.index_error(self, (head,) + tail, str(err)) from err
raise ak._errors.index_error(self, (head, *tail), str(err)) from err

return NumpyArray(out, parameters=self._parameters, backend=self._backend)

Expand All @@ -369,26 +369,27 @@ def _getitem_next(

elif isinstance(head, ak.index.Index64):
if advanced is None:
where = (slice(None), head.data) + tail
where = (slice(None), head.data, *tail)
else:
where = (
self._backend.index_nplike.asarray(advanced.data),
head.data,
) + tail
*tail,
)

try:
out = self._data[where]
except IndexError as err:
raise ak._errors.index_error(self, (head,) + tail, str(err)) from err
raise ak._errors.index_error(self, (head, *tail), str(err)) from err

return NumpyArray(out, parameters=self._parameters, backend=self._backend)

elif isinstance(head, ak.contents.ListOffsetArray):
where = (slice(None), head) + tail
where = (slice(None), head, *tail)
try:
out = self._data[where]
except IndexError as err:
raise ak._errors.index_error(self, (head,) + tail, str(err)) from err
raise ak._errors.index_error(self, (head, *tail), str(err)) from err

return NumpyArray(out, parameters=self._parameters, backend=self._backend)

Expand Down Expand Up @@ -745,7 +746,7 @@ def _unique(self, negaxis, starts, parents, outlength):
"awkward_unique",
out.dtype.type,
nextlength.dtype.type,
]( # noqa: E231
](
out,
out.shape[0],
nextlength.data,
Expand Down
Loading