Skip to content

Commit

Permalink
Enable, fix RUF lint rules, a bit more dataclass conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Nov 22, 2024
1 parent 90e75e9 commit 1e86127
Show file tree
Hide file tree
Showing 72 changed files with 427 additions and 423 deletions.
4 changes: 2 additions & 2 deletions contrib/c-integer-semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def main():
func.argtypes = [ctypes.c_longlong, ctypes.c_longlong]
func.restype = ctypes.c_longlong

cdiv = int_exp.cdiv # noqa
cmod = int_exp.cmod # noqa
cdiv = int_exp.cdiv
cmod = int_exp.cmod
int_floor_div = int_exp.loopy_floor_div_int64
int_floor_div_pos_b = int_exp.loopy_floor_div_pos_b_int64
int_mod_pos_b = int_exp.loopy_mod_pos_b_int64
Expand Down
6 changes: 3 additions & 3 deletions contrib/mem-pattern-explorer/pattern_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, gsize, lsize, subgroup_size=32, decay_constant=0.75):

self.arrays = []

def l(self, index): # noqa: E741,E743
def l(self, index): # noqa: E743
subscript = [np.newaxis] * self.ind_length
subscript[len(self.gsize) + index] = slice(None)

Expand Down Expand Up @@ -147,7 +147,7 @@ def get_plot_data(self):
div_ceil(nelements, self.elements_per_row),
self.elements_per_row,)
shaped_array = np.zeros(
base_shape + (self.nattributes,),
(*base_shape, self.nattributes),
dtype=np.float32)
shaped_array.reshape(-1, self.nattributes)[:nelements] = self.array

Expand All @@ -160,7 +160,7 @@ def get_plot_data(self):
else:
subgroup.fill(1)

rgb_array = np.zeros(base_shape + (3,))
rgb_array = np.zeros((*base_shape, 3))
if 1:
if len(self.ctx.gsize) > 1:
# g.0 -> red
Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.request import urlopen


_conf_url = "https://raw.githubusercontent.com/inducer/sphinxconfig/main/sphinxconfig.py" # noqa
_conf_url = "https://raw.githubusercontent.com/inducer/sphinxconfig/main/sphinxconfig.py"
with urlopen(_conf_url) as _inf:
exec(compile(_inf.read(), _conf_url, "exec"), globals())

Expand Down
2 changes: 1 addition & 1 deletion examples/python/ispc-stream-harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def transform(knl, vars, stream_dtype):

knl = lp.add_and_infer_dtypes(knl, dict.fromkeys(vars, stream_dtype))

knl = lp.set_argument_order(knl, vars + ["n"])
knl = lp.set_argument_order(knl, [*vars, "n"])

return knl

Expand Down
4 changes: 2 additions & 2 deletions loopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def register_preamble_generators(kernel: LoopKernel, preamble_generators):
"and would thus disrupt loopy's caches"
% pgen)

new_pgens = (pgen,) + new_pgens
new_pgens = (pgen, *new_pgens)

return kernel.copy(preamble_generators=new_pgens)

Expand All @@ -483,7 +483,7 @@ def register_symbol_manglers(kernel, manglers):
"and would disrupt loopy's caches"
% m)

new_manglers = (m,) + new_manglers
new_manglers = (m, *new_manglers)

return kernel.copy(symbol_manglers=new_manglers)

Expand Down
4 changes: 2 additions & 2 deletions loopy/auto_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ def auto_test_vs_ref(
if ref_entrypoint is None:
if len(ref_prog.entrypoints) != 1:
raise LoopyError("Unable to guess entrypoint for ref_prog.")
ref_entrypoint = list(ref_prog.entrypoints)[0]
ref_entrypoint = next(iter(ref_prog.entrypoints))

if test_entrypoint is None:
if len(test_prog.entrypoints) != 1:
raise LoopyError("Unable to guess entrypoint for ref_prog.")
test_entrypoint = list(test_prog.entrypoints)[0]
test_entrypoint = next(iter(test_prog.entrypoints))

ref_prog = lp.preprocess_kernel(ref_prog)
test_prog = lp.preprocess_kernel(test_prog)
Expand Down
4 changes: 2 additions & 2 deletions loopy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def check_separated_array_consistency(kernel: LoopKernel) -> None:
for attr_name in ["address_space", "is_input", "is_output"]:
if getattr(arg, attr_name) != getattr(sub_arg, attr_name):
raise LoopyError(
"Attribute '{attr_name}' of "
f"Attribute '{attr_name}' of "
f"'{arg.name}' and associated sep array "
f"'{sub_arg.name}' is not consistent.")

Expand Down Expand Up @@ -266,7 +266,7 @@ def ensure_depends_only_on_arguments(
raise LoopyError(f"invalid value of {what}")

assert new_dim_tags is not None
new_dim_tags = new_dim_tags + (dim_tag,)
new_dim_tags = (*new_dim_tags, dim_tag)

arg = arg.copy(dim_tags=new_dim_tags)

Expand Down
6 changes: 3 additions & 3 deletions loopy/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@


if getattr(sys, "_BUILDING_SPHINX_DOCS", False):
from loopy.codegen.result import GeneratedProgram # noqa: F811
from loopy.codegen.tools import CodegenOperationCacheManager # noqa: F811
from loopy.codegen.result import GeneratedProgram
from loopy.codegen.tools import CodegenOperationCacheManager


__doc__ = """
Expand Down Expand Up @@ -666,7 +666,7 @@ def generate_code_v2(t_unit: TranslationUnit) -> CodeGenerationResult:
# adding the callee fdecls to the device_programs
device_programs = ([device_programs[0].copy(
ast=t_unit.target.get_device_ast_builder().ast_module.Collection(
callee_fdecls+[device_programs[0].ast]))] +
[*callee_fdecls, device_programs[0].ast]))] +
device_programs[1:])

def not_reduction_op(name: str | ReductionOpFunction) -> str:
Expand Down
2 changes: 1 addition & 1 deletion loopy/codegen/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def gen_code(inner_codegen_state):

prev_gen_code = gen_code

def gen_code(inner_codegen_state): # noqa pylint:disable=function-redefined
def gen_code(inner_codegen_state):
condition_exprs = [
constraint_to_cond_expr(cns)
for cns in bounds_checks] + list(pred_checks)
Expand Down
2 changes: 1 addition & 1 deletion loopy/codegen/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def generate_assignment_instruction_code(codegen_state, insn):
from pymbolic.mapper.stringifier import PREC_NONE
lhs_code = codegen_state.expression_to_code_mapper(insn.assignee, PREC_NONE)

from cgen import Statement as S # noqa
from cgen import Statement as S

gs, ls = kernel.get_grid_size_upper_bounds(codegen_state.callables_table)

Expand Down
4 changes: 1 addition & 3 deletions loopy/codegen/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ def with_new_program(self, codegen_state, program):
assert program.is_device_program
return self.copy(
device_programs=(
list(self.device_programs[:-1])
+
[program]))
[*list(self.device_programs[:-1]), program]))
else:
assert program.name == codegen_state.gen_program_name
assert not program.is_device_program
Expand Down
2 changes: 1 addition & 1 deletion loopy/frontend/fortran/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def parse_transformed_fortran(source, free_form=True, strict=True,
prev_sys_path = sys.path
try:
if infile_dirname:
sys.path = prev_sys_path + [infile_dirname]
sys.path = [*prev_sys_path, infile_dirname]

if pre_transform_code is not None:
proc_dict["_MODULE_SOURCE_CODE"] = pre_transform_code
Expand Down
7 changes: 5 additions & 2 deletions loopy/frontend/fortran/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
"""

import re
from collections.abc import Mapping
from sys import intern
from typing import ClassVar

import numpy as np

import pytools.lex
from pymbolic.parser import Parser as ExpressionParserBase

from loopy.frontend.fortran.diagnostic import TranslationError
from loopy.symbolic import LexTable


_less_than = intern("less_than")
Expand Down Expand Up @@ -65,7 +68,7 @@ def tuple_to_complex_literal(expr):
# {{{ expression parser

class FortranExpressionParser(ExpressionParserBase):
lex_table = [
lex_table: ClassVar[LexTable] = [
(_less_than, pytools.lex.RE(r"\.lt\.", re.I)),
(_greater_than, pytools.lex.RE(r"\.gt\.", re.I)),
(_less_equal, pytools.lex.RE(r"\.le\.", re.I)),
Expand Down Expand Up @@ -142,7 +145,7 @@ def parse_terminal(self, pstate):
return ExpressionParserBase.parse_terminal(
self, pstate)

COMP_MAP = {
COMP_MAP: ClassVar[Mapping[str, str]] = {
_less_than: "<",
_less_equal: "<=",
_greater_than: ">",
Expand Down
11 changes: 6 additions & 5 deletions loopy/frontend/fortran/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import re
from sys import intern
from typing import ClassVar
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(self, scope):
super().__init__()

def get_cache_key(self, expr):
return super().get_cache_key(expr) + (self.scope,)
return (*super().get_cache_key(expr), self.scope)

def map_subscript(self, expr):
from pymbolic.primitives import Variable
Expand Down Expand Up @@ -441,7 +442,7 @@ def map_Implicit(self, node):
def map_Equivalence(self, node):
raise NotImplementedError("equivalence")

TYPE_MAP = {
TYPE_MAP: ClassVar[dict[tuple[str, str], type[np.generic]]] = {
("real", ""): np.float32,
("real", "4"): np.float32,
("real", "8"): np.float64,
Expand All @@ -455,9 +456,9 @@ def map_Equivalence(self, node):
("integer", "8"): np.int64,
}
if hasattr(np, "float128"):
TYPE_MAP[("real", "16")] = np.float128 # pylint:disable=no-member
TYPE_MAP["real", "16"] = np.float128 # pylint:disable=no-member
if hasattr(np, "complex256"):
TYPE_MAP[("complex", "32")] = np.complex256 # pylint:disable=no-member
TYPE_MAP["complex", "32"] = np.complex256 # pylint:disable=no-member

def dtype_from_stmt(self, stmt):
length, kind = stmt.selector
Expand All @@ -471,7 +472,7 @@ def dtype_from_stmt(self, stmt):
else:
raise RuntimeError("both length and kind specified")

return np.dtype(self.TYPE_MAP[(type(stmt).__name__.lower(), length)])
return np.dtype(self.TYPE_MAP[type(stmt).__name__.lower(), length])

def map_type_decl(self, node):
scope = self.scope_stack[-1]
Expand Down
2 changes: 1 addition & 1 deletion loopy/frontend/fortran/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def rec(self, expr, *args, **kwargs):
r"^(?P<name>[_0-9a-zA-Z]+)\s*"
r"(\((?P<shape>[-+*/0-9:a-zA-Z, \t]+)\))?"
r"(\s*=\s*(?P<initializer>.+))?"
"$")
r"$")

def parse_dimension_specs(self, node, dim_decls):
def parse_bounds(bounds_str):
Expand Down
7 changes: 4 additions & 3 deletions loopy/kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Dict,
FrozenSet,
Iterator,
Expand Down Expand Up @@ -85,7 +86,7 @@

# {{{ loop kernel object

class KernelState(IntEnum): # noqa
class KernelState(IntEnum):
INITIAL = 0
CALLS_RESOLVED = 1
PREPROCESSED = 2
Expand Down Expand Up @@ -199,7 +200,7 @@ class LoopKernel(Taggable):
were applied to the kernel. These are stored so that they may be repeated
on expressions the user specifies later.
"""
index_dtype: NumpyType = NumpyType(np.dtype(np.int32))
index_dtype: NumpyType = NumpyType(np.dtype(np.int32)) # noqa: RUF009
silenced_warnings: FrozenSet[str] = frozenset()

# FIXME Yuck, this should go.
Expand Down Expand Up @@ -1310,7 +1311,7 @@ def __setstate__(self, state):

# {{{ persistent hash key generation / comparison

hash_fields = [
hash_fields: ClassVar[Sequence[str]] = [
"domains",
"instructions",
"args",
Expand Down
4 changes: 2 additions & 2 deletions loopy/kernel/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
from loopy.target import TargetBase

if getattr(sys, "_BUILDING_SPHINX_DOCS", False):
from loopy.target import TargetBase # noqa: F811
from loopy.target import TargetBase


T = TypeVar("T")
Expand Down Expand Up @@ -1217,7 +1217,7 @@ def _apply_offset(sub: Expression, ary: ArrayBase) -> Expression:
else:
# assume it's an expression
# FIXME: mypy can't figure out that ExpressionT + ExpressionT works
return ary.offset + sub # type: ignore[call-overload, arg-type, operator] # noqa: E501
return ary.offset + sub # type: ignore[call-overload, arg-type, operator]
else:
return sub

Expand Down
16 changes: 7 additions & 9 deletions loopy/kernel/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ def expand_defines(insn, defines, single_valued=True):
"in this context (when expanding '%s')" % define_name)

replacements = [
rep+((replace_pattern % define_name, subval),)
(*rep, (replace_pattern % define_name, subval))
for rep in replacements
for subval in value
]
else:
replacements = [
rep+((replace_pattern % define_name, value),)
(*rep, (replace_pattern % define_name, value))
for rep in replacements]

for rep in replacements:
Expand Down Expand Up @@ -285,14 +285,12 @@ def parse_nosync_option(opt_value):
arrow_idx = value.find("->")
if arrow_idx >= 0:
result["inames_to_dup"] = (
result.get("inames_to_dup", [])
+
[(value[:arrow_idx], value[arrow_idx+2:])])
[*result.get("inames_to_dup", []),
(value[:arrow_idx], value[arrow_idx + 2:])
])
else:
result["inames_to_dup"] = (
result.get("inames_to_dup", [])
+
[(value, None)])
[*result.get("inames_to_dup", []), (value, None)])

elif opt_key == "dep" and opt_value is not None:
if opt_value.startswith("*"):
Expand Down Expand Up @@ -2403,7 +2401,7 @@ def make_function(domains, instructions, kernel_data=None, **kwargs):
kernel_args.append(dat)
continue

if isinstance(dat, ArrayBase) and isinstance(dat.shape, tuple): # noqa pylint:disable=no-member
if isinstance(dat, ArrayBase) and isinstance(dat.shape, tuple):
new_shape = []
for shape_axis in dat.shape: # pylint:disable=no-member
if shape_axis is not None:
Expand Down
4 changes: 2 additions & 2 deletions loopy/kernel/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

# {{{ utilities

def _names_from_expr(expr: Union[None, Expression, str]) -> FrozenSet[str]:
def _names_from_expr(expr: Union[Expression, str, None]) -> FrozenSet[str]:
from numbers import Number

from loopy.symbolic import DependencyMapper
Expand Down Expand Up @@ -303,7 +303,7 @@ def __str__(self):
return "ord"


ToInameTagConvertible = Union[str, None, Tag]
ToInameTagConvertible = Union[str, Tag, None]


def parse_tag(tag: ToInameTagConvertible) -> Optional[Tag]:
Expand Down
Loading

0 comments on commit 1e86127

Please sign in to comment.