Skip to content

Commit 0903af2

Browse files
committed
Ruff: enable SIM rules
1 parent 269a8d3 commit 0903af2

7 files changed

Lines changed: 35 additions & 44 deletions

File tree

arraycontext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _deprecated_acf():
186186

187187

188188
def __getattr__(name):
189-
replacement_and_obj = _depr_name_to_replacement_and_obj.get(name, None)
189+
replacement_and_obj = _depr_name_to_replacement_and_obj.get(name)
190190
if replacement_and_obj is not None:
191191
replacement, obj, year = replacement_and_obj
192192
from warnings import warn

arraycontext/container/arithmetic.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,18 @@ def wrap(cls: Any) -> Any:
373373
cls_has_array_context_attr: bool | None = _cls_has_array_context_attr
374374
bcast_actx_array_type: bool | None = _bcast_actx_array_type
375375

376-
if cls_has_array_context_attr is None:
377-
if hasattr(cls, "array_context"):
378-
raise TypeError(
379-
f"{cls} has an 'array_context' attribute, but it does not "
380-
"set '_cls_has_array_context_attr' to *True* when calling "
381-
"with_container_arithmetic. This is being interpreted "
382-
"as '.array_context' being permitted to fail "
383-
"with an exception, which is no longer allowed. "
384-
f"If {cls.__name__}.array_context will not fail, pass "
385-
"'_cls_has_array_context_attr=True'. "
386-
"If you do not want container arithmetic to make "
387-
"use of the array context, set "
388-
"'_cls_has_array_context_attr=False'.")
376+
if cls_has_array_context_attr is None and hasattr(cls, "array_context"):
377+
raise TypeError(
378+
f"{cls} has an 'array_context' attribute, but it does not "
379+
"set '_cls_has_array_context_attr' to *True* when calling "
380+
"with_container_arithmetic. This is being interpreted "
381+
"as '.array_context' being permitted to fail "
382+
"with an exception, which is no longer allowed. "
383+
f"If {cls.__name__}.array_context will not fail, pass "
384+
"'_cls_has_array_context_attr=True'. "
385+
"If you do not want container arithmetic to make "
386+
"use of the array context, set "
387+
"'_cls_has_array_context_attr=False'.")
389388

390389
if bcast_actx_array_type is None:
391390
if cls_has_array_context_attr:

arraycontext/container/traversal.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,7 @@ def rec(lines: list[str], ary_: ArrayOrContainerT, level: int) -> None:
252252
else:
253253
for key, subary in iterable:
254254
key = f"{key} ({type(subary).__name__})"
255-
if level == 0:
256-
indent = ""
257-
else:
258-
indent = f" | {' ' * 4 * (level - 1)}"
255+
indent = "" if level == 0 else f" | {' ' * 4 * (level - 1)}"
259256

260257
lines.append(f"{indent} +-- {key}")
261258
rec(lines, subary, level + 1)
@@ -833,7 +830,7 @@ def _unflatten(template_subary: ArrayOrContainer) -> ArrayOrContainer:
833830

834831
# {{{ check strides
835832

836-
if strict and hasattr(template_subary_c, "strides"):
833+
if strict and hasattr(template_subary_c, "strides"): # noqa: SIM102
837834
# Checking strides for 0 sized arrays is ill-defined
838835
# since they cannot be indexed
839836
if (

arraycontext/impl/pytato/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,9 @@ def _to_frozen(key: tuple[Any, ...], ary) -> TaggableCLArray:
526526

527527
from pytools import common_prefix
528528
name_hint = common_prefix([nh.prefix for nh in name_hint_tags])
529-
if name_hint:
530-
# All name_hint_tags shared at least some common prefix.
531-
function_name = f"frozen_{name_hint}"
532-
else:
533-
function_name = "frozen_result"
529+
530+
# All name_hint_tags shared at least some common prefix.
531+
function_name = f"frozen_{name_hint}" if name_hint else "frozen_result"
534532

535533
self._dag_transform_cache[normalized_expr] = (
536534
transformed_dag, function_name)
@@ -668,7 +666,7 @@ def preprocess_arg(name, arg):
668666
f"array type: got '{type(arg).__name__}', but expected one "
669667
f"of {self.array_types}")
670668

671-
if name is not None:
669+
if name is not None: # noqa: SIM102
672670
# Tagging Placeholders with naming-related tags is pointless:
673671
# They already have names. It's also counterproductive, as
674672
# multiple placeholders with the same name that are not
@@ -897,7 +895,7 @@ def preprocess_arg(name, arg):
897895
f"array type: got '{type(arg).__name__}', but expected one "
898896
f"of {self.array_types}")
899897

900-
if name is not None:
898+
if name is not None: # noqa: SIM102
901899
# Tagging Placeholders with naming-related tags is pointless:
902900
# They already have names. It's also counterproductive, as
903901
# multiple placeholders with the same name that are not

doc/make_numpy_coverage_table.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,19 @@ def write_searching_sorting_and_counting(outf, contexts):
232232
parser.add_argument("filename", nargs="?", type=pathlib.Path, default=None)
233233
args = parser.parse_args()
234234

235-
import sys
236-
if args.filename is not None:
237-
outf = open(args.filename, "w")
238-
else:
239-
outf = sys.stdout
235+
def write(outf):
236+
outf.write(HEADER)
237+
write_array_creation_routines(outf, ctxs)
238+
write_array_manipulation_routines(outf, ctxs)
239+
write_linear_algebra(outf, ctxs)
240+
write_logic_functions(outf, ctxs)
241+
write_mathematical_functions(outf, ctxs)
240242

241243
ctxs = initialize_contexts()
242244

243-
outf.write(HEADER)
244-
write_array_creation_routines(outf, ctxs)
245-
write_array_manipulation_routines(outf, ctxs)
246-
write_linear_algebra(outf, ctxs)
247-
write_logic_functions(outf, ctxs)
248-
write_mathematical_functions(outf, ctxs)
249-
250-
if args.filename is not None:
251-
outf.close()
245+
if args.filename:
246+
with open(args.filename, "w") as outf:
247+
write(outf)
248+
else:
249+
import sys
250+
write(sys.stdout)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extend-select = [
7474
"RUF", # ruff
7575
"UP", # pyupgrade
7676
"W", # pycodestyle
77+
"SIM",
7778
]
7879
extend-ignore = [
7980
"C90", # McCabe complexity

test/test_arraycontext.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ def randn(shape, dtype):
153153
rng = np.random.default_rng()
154154
dtype = np.dtype(dtype)
155155

156-
if shape == 0:
157-
ashape = 1
158-
else:
159-
ashape = shape
156+
ashape = 1 if shape == 0 else shape
160157

161158
if dtype.kind == "c":
162159
dtype = np.dtype(f"<f{dtype.itemsize // 2}")

0 commit comments

Comments
 (0)