Skip to content

Commit

Permalink
Switch to ruff, fix flagged issues
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jul 1, 2024
1 parent 092a823 commit 71c7cc7
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 105 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ on:
- cron: '17 3 * * 0'

jobs:
flake8:
name: Flake8
ruff:
name: Ruff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
uses: actions/setup-python@v5
with:
# matches compat target in setup.py
python-version: '3.8'
- name: "Main Script"
run: |
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
. ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" test examples
pip install ruff
ruff check
pylint:
name: Pylint
Expand Down
12 changes: 5 additions & 7 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ Pylint:
except:
- tags


Flake8:
script:
- curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
- . ./prepare-and-run-flake8.sh ${CI_PROJECT_NAME} test examples
Ruff:
script: |
pipx install ruff
ruff check
tags:
- python3
- docker-runner
except:
- tags


Mypy:
script: |
curl -L -O https://tiker.net/ci-support-v0
Expand Down
61 changes: 61 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[tool.ruff.lint]
preview = true
extend-select = [
"B", # flake8-bugbear
"C", # flake8-comprehensions
"E", # pycodestyle
"F", # pyflakes
# not yet
# "I", # flake8-isort
"N", # pep8-naming
"Q", # flake8-quotes
"W", # pycodestyle
"NPY" # numpy
]
extend-ignore = [
"E226",
"E241",
"E242",
"E265",
"N802",
"E402",
"N814",
"N817",
"C90",

# numpy random generators---disable for now
"NPY002",
]
[tool.ruff.lint.per-file-ignores]
"examples/advection.py" = ["B023"]
"test/test_linalg.py" = ["N806"]

[tool.ruff.lint.isort]
known-first-party = ["pytools", "pymbolic", "loopy"]
known-local-folder = ["pytato"]
lines-after-imports = 2
combine-as-imports = true

[tool.ruff.lint.flake8-quotes]
inline-quotes = "double"
docstring-quotes = "double"
multiline-quotes = "double"

[[tool.mypy.overrides]]
module = [
"islpy",
"loopy.*",
"pymbolic.*",
"pyopencl.*",
"jax.*",
"pygments.*",
"mako.*",
]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = [
"pytato.transform",
"pytato.scalar_expr",
]
allow_subclassing_any = true
8 changes: 5 additions & 3 deletions pytato/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,8 @@ def __init__(self, data: Mapping[str, Array], *,
from warnings import warn
warn("Passing `tags=None` is deprecated and will result"
" in an error from 2023. To remove this message either"
" call make_dict_of_named_arrays or pass the `tags` argument.")
" call make_dict_of_named_arrays or pass the `tags` argument.",
DeprecationWarning, stacklevel=2)
tags = frozenset()

object.__setattr__(self, "_data", data)
Expand Down Expand Up @@ -1170,8 +1171,9 @@ def with_tagged_reduction(self,
if isinstance(redn_axis, str):
try:
redn_axis_ = self.index_to_access_descr[redn_axis]
except KeyError:
raise InvalidEinsumIndex(f"'{redn_axis}': not a valid axis index.")
except KeyError as err:
raise InvalidEinsumIndex(
f"'{redn_axis}': not a valid axis index.") from err
if isinstance(redn_axis_, EinsumReductionAxis):
redn_axis = redn_axis_
else:
Expand Down
8 changes: 4 additions & 4 deletions pytato/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ class CannotBroadcastError(ValueError):
pass


class UnknownIndexLambdaExpr(ValueError):
class UnknownIndexLambdaExpr(ValueError): # noqa: N818
"""
Raised when the structure :class:`pytato.array.IndexLambda` could not be
inferred.
"""
pass


class InvalidEinsumIndex(ValueError):
class InvalidEinsumIndex(ValueError): # noqa: N818
"""
Raised when an einsum index was referred by an invalid value.
"""


class NotAReductionAxis(ValueError):
class NotAReductionAxis(ValueError): # noqa: N818
"""
Raised when a :class:`pytato.ReductionDescriptor` was referred by an invalid
value.
"""


class CannotBeLoweredToIndexLambda(ValueError):
class CannotBeLoweredToIndexLambda(ValueError): # noqa: N818
"""
Raised when a :class:`pytato.Array` was expected to be lowered to an
:class:`~pytato.array.IndexLambda`, but it cannot be. For ex. a
Expand Down
8 changes: 4 additions & 4 deletions pytato/distributed/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ def add_needed_pid(pid: _DistributedPartId,
# Add edges between sends and receives (cross-rank)
try:
sending_pid = comm_id_to_sending_pid[comm_id]
except KeyError:
except KeyError as err:
raise MissingSendError(
f"no matching send for recv on '{comm_id}'")
f"no matching send for recv on '{comm_id}'") from err

add_needed_pid(sumpart.pid, sending_pid)

Expand Down Expand Up @@ -371,8 +371,8 @@ def add_needed_pid(pid: _DistributedPartId,
from pytato.distributed.verify import PartitionInducedCycleError
try:
compute_topological_order(pid_to_needed_pids)
except CycleError:
raise PartitionInducedCycleError
except CycleError as err:
raise PartitionInducedCycleError from err

logger.info("verify_distributed_partition completed successfully.")

Expand Down
2 changes: 1 addition & 1 deletion pytato/loopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def call_loopy(translation_unit: "lp.TranslationUnit",

# {{{ shape inference

class ShapeInferenceFailure(RuntimeError):
class ShapeInferenceFailure(RuntimeError): # noqa: N818
pass


Expand Down
5 changes: 3 additions & 2 deletions pytato/target/loopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ def _get_processed_bound_arguments(self,
if self.program.default_entrypoint.options.no_numpy:
raise TypeError(f"Got numpy array for the DataWrapper {name}"
", in no_numpy=True mode. Expects a"
" pyopencl.array.Array.")
" pyopencl.array.Array.") from None
proc_bnd_args[name] = cla.to_device(queue, bnd_arg, allocator)
elif isinstance(bnd_arg, cla.Array):
proc_bnd_args[name] = bnd_arg
else:
raise TypeError("Data in a bound argument can be one of"
" numpy array, pyopencl array or scalar."
f" Got {type(bnd_arg).__name__} for '{name}'.")
f" Got {type(bnd_arg).__name__} for '{name}'."
) from None

result: Mapping[str, Any] = immutabledict(proc_bnd_args)
assert set(result.keys()) == set(self.bound_arguments.keys())
Expand Down
4 changes: 2 additions & 2 deletions pytato/target/loopy/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ def map_reduce(self, expr: scalar_expr.Reduce,

try:
loopy_redn = PYTATO_REDUCTION_TO_LOOPY_REDUCTION[type(expr.op)]
except KeyError:
raise NotImplementedError(expr.op)
except KeyError as err:
raise NotImplementedError(expr.op) from err

unique_names_mapping = {
old_name: state.var_name_gen(f"_pt_{loopy_redn}" + old_name)
Expand Down
2 changes: 1 addition & 1 deletion pytato/target/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _compiled_function(self) -> Callable[..., Any]:
def _bound_argment_names(self) -> Set[str]:
return set(self.bound_arguments.keys())

def __call__(self, *args: Any, **kwargs: Any) -> Any:
def __call__(self, *args: Any, **kwargs: Any) -> Any:

if args:
raise ValueError(f"'{type(self).__call__}' does not take positional"
Expand Down
2 changes: 1 addition & 1 deletion pytato/transform/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def map_index_lambda(self, expr: IndexLambda) -> None:
except UnknownIndexLambdaExpr:
from warnings import warn
warn(f"'{expr}' is an unknown index lambda type"
" no tags were propagated across it.")
" no tags were propagated across it.", stacklevel=1)
# no propagation semantics implemented for such cases
return

Expand Down
2 changes: 1 addition & 1 deletion pytato/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def _index_into(
array_idx_shape = get_shape_after_broadcasting(
[idx for idx in indices if isinstance(idx, Array)])
except CannotBroadcastError as e:
raise IndexError(str(e))
raise IndexError(str(e)) from None

# }}}

Expand Down
4 changes: 2 additions & 2 deletions pytato/visualization/fancy_placeholder_data_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ def show_fancy_placeholder_data_flow(dag: Union[Array, DictOfNamedArrays],
"""
try:
from mako.template import Template
except ImportError:
except ImportError as err:
raise RuntimeError("'show_fancy_placeholder_data_flow' requires"
" mako. Install as `pip install mako`.")
" mako. Install as `pip install mako`.") from err

if isinstance(dag, Array):
from pytato.array import make_dict_of_named_arrays
Expand Down
45 changes: 0 additions & 45 deletions setup.cfg

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_trace_fft(ctx_factory):
prg = pt.generate_loopy(result).program

x = np.random.randn(n).astype(np.complex128)
evt, (result,) = prg(queue, x=x)
_evt, (result,) = prg(queue, x=x)

ref_result = fft(x)

Expand Down
Loading

0 comments on commit 71c7cc7

Please sign in to comment.