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: move to using Ruff #315

Merged
merged 2 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
6 changes: 0 additions & 6 deletions .flake8

This file was deleted.

44 changes: 7 additions & 37 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ ci:
autofix_commit_msg: "style: pre-commit fixes"

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: &pyupver
- --py37-plus

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
Expand All @@ -30,11 +23,11 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.247"
hooks:
- id: isort
args: ["-a", "from __future__ import annotations"]
- id: ruff
args: ["--fix", "--show-fixes"]

- repo: https://github.com/tox-dev/pyproject-fmt
rev: "0.8.0"
Expand All @@ -48,18 +41,9 @@ repos:
files: src
args: []
additional_dependencies:
- numpy~=1.24.0
- numpy
- packaging

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-docstrings
Copy link
Member Author

Choose a reason for hiding this comment

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

For some reason, Ruff's "D" code catches a lot of stuff the old one didn't. So I've removed it for now, and we can enable new codes (including "D") in the future.

- flake8-print

- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
Expand All @@ -77,35 +61,21 @@ repos:
hooks:
- id: blacken-docs
args: ["-E"]
additional_dependencies: [black==22.8.0]
additional_dependencies: [black==23.1.0]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
exclude: ^src/vector/backends/_numba_object.py$
- id: python-no-log-warn
- id: python-no-eval
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/asottile/yesqa
rev: "v1.4.0"
hooks:
- id: yesqa

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.6.1
hooks:
- id: nbqa-pyupgrade
args: *pyupver
args: ["--py37-plus"]
- id: nbqa-isort
args: ["--float-to-top"]

- repo: https://github.com/hadialqattan/pycln
rev: "v2.1.3"
hooks:
- id: pycln
40 changes: 37 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ filterwarnings = [
"ignore::UserWarning",
]

[tool.isort]
profile = "black"

[tool.pylint]
master.py-version = "3.7"
reports.output-format = "colorized"
Expand Down Expand Up @@ -204,3 +201,40 @@ module = [
ignore_missing_imports = true
disallow_untyped_defs = false
disallow_untyped_calls = false

[tool.ruff]
select = [
"E", "F", "W", # flake8
"B", "B904", # flake8-bugbear
"I", # isort
"C4", # flake8-comprehensions
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
]
extend-ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
]
target-version = "py37"
typing-modules = ["vector._typeutils"]
src = ["src"]
unfixable = [
"T20", # Removes print statements
"F841", # Removes unused variables
]
exclude = []
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.per-file-ignores]
"noxfile.py" = ["T20"]
"tests/*" = ["T20"]
"src/vector/backends/_numba_object.py" = ["PGH003"]
"tests/backends/test_operators.py" = ["SIM201", "SIM202"]
5 changes: 2 additions & 3 deletions src/vector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
Vector4D,
dim,
)
from vector.backends.awkward_constructors import Array
from vector.backends.awkward_constructors import Array, zip
from vector.backends.awkward_constructors import Array as awk
from vector.backends.awkward_constructors import zip
from vector.backends.numpy import (
MomentumNumpy2D,
MomentumNumpy3D,
Expand All @@ -43,8 +42,8 @@
VectorNumpy2D,
VectorNumpy3D,
VectorNumpy4D,
array,
)
from vector.backends.numpy import array
from vector.backends.numpy import array as arr
from vector.backends.object import (
MomentumObject2D,
Expand Down
8 changes: 3 additions & 5 deletions src/vector/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2962,15 +2962,15 @@ def to_Vector2D(self) -> VectorProtocolPlanar:
def to_Vector3D(self) -> VectorProtocolSpatial:
return self._wrap_result(
type(self),
self.azimuthal.elements + (0,),
(*self.azimuthal.elements, 0),
[_aztype(self), LongitudinalZ, None],
1,
)

def to_Vector4D(self) -> VectorProtocolLorentz:
return self._wrap_result(
type(self),
self.azimuthal.elements + (0, 0),
(*self.azimuthal.elements, 0, 0),
[_aztype(self), LongitudinalZ, TemporalT],
1,
)
Expand Down Expand Up @@ -4026,9 +4026,7 @@ def _handler_of(*objects: VectorProtocol) -> VectorProtocol:
for obj in objects:
if not isinstance(obj, Vector):
continue
if handler is None:
handler = obj
elif _get_handler_index(obj) > _get_handler_index(handler):
if handler is None or _get_handler_index(obj) > _get_handler_index(handler):
handler = obj

assert handler is not None
Expand Down
5 changes: 2 additions & 3 deletions src/vector/backends/_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@
numba.extending.register_jitable(obj)
registered.add(obj)

# MyPy doesn't know that submodule contains dispatch_map, so getattr
for key, value in getattr(submodule, "dispatch_map").items():
for key, value in submodule.dispatch_map.items():
function, *returns = value
if function not in registered:
numba.extending.register_jitable(function)
registered.add(function)

numba_modules[groupname][modname][key] = tuple([function] + returns)
numba_modules[groupname][modname][key] = (function, *returns)
3 changes: 2 additions & 1 deletion src/vector/backends/_numba_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# or https://github.com/scikit-hep/vector for details.

# type: ignore
from __future__ import annotations

"""
Implements VectorObjects in Numba.
Every function should be made usable in Numba.
"""

from __future__ import annotations

import operator

import numba
Expand Down
12 changes: 0 additions & 12 deletions src/vector/backends/awkward.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,6 @@ class VectorRecord2D(VectorAwkward2D, ak.Record): # type: ignore[misc]
for the corresponding ``Vector`` and ``Momentum`` classes.
"""

pass


behavior["Vector2D"] = VectorRecord2D

Expand Down Expand Up @@ -1288,8 +1286,6 @@ class VectorRecord3D(VectorAwkward3D, ak.Record): # type: ignore[misc]
for the corresponding ``Vector`` and ``Momentum`` classes.
"""

pass


behavior["Vector3D"] = VectorRecord3D

Expand Down Expand Up @@ -1324,8 +1320,6 @@ class VectorRecord4D(VectorAwkward4D, ak.Record): # type: ignore[misc]
for the corresponding ``Vector`` and ``Momentum`` classes.
"""

pass


behavior["Vector4D"] = VectorRecord4D

Expand Down Expand Up @@ -1360,8 +1354,6 @@ class MomentumRecord2D(MomentumAwkward2D, ak.Record): # type: ignore[misc]
for the corresponding ``Momentum`` and ``Vector`` classes.
"""

pass


behavior["Momentum2D"] = MomentumRecord2D

Expand Down Expand Up @@ -1395,8 +1387,6 @@ class MomentumRecord3D(MomentumAwkward3D, ak.Record): # type: ignore[misc]
for the corresponding ``Momentum`` and ``Vector`` classes.
"""

pass


behavior["Momentum3D"] = MomentumRecord3D

Expand Down Expand Up @@ -1430,8 +1420,6 @@ class MomentumRecord4D(MomentumAwkward4D, ak.Record): # type: ignore[misc]
for the corresponding ``Momentum`` and ``Vector`` classes.
"""

pass


behavior["Momentum4D"] = MomentumRecord4D

Expand Down
75 changes: 3 additions & 72 deletions src/vector/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ def _wrap_result(

elif (
len(returns) == 1
or (len(returns) == 2 and returns[1] is None)
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
):
Expand All @@ -1044,55 +1045,11 @@ def _wrap_result(
return out.view(cls.ProjectionClass2D)

elif (
len(returns) == 2
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
and returns[1] is None
):
result = _toarrays(result)
dtype = [
(name, result[i].dtype)
for i, name in enumerate(_coordinate_class_to_names[returns[0]])
]

out = numpy.empty(_shape_of(result), dtype=dtype)
for i, name in enumerate(_coordinate_class_to_names[returns[0]]):
out[name] = result[i]
return out.view(cls.ProjectionClass2D)

elif (
len(returns) == 2
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
and isinstance(returns[1], type)
and issubclass(returns[1], Longitudinal)
):
result = _toarrays(result)
dtype = []
i = 0
for name in _coordinate_class_to_names[returns[0]]:
dtype.append((name, result[i].dtype))
i += 1
for name in _coordinate_class_to_names[returns[1]]:
dtype.append((name, result[i].dtype))
i += 1
out = numpy.empty(_shape_of(result), dtype=dtype)
i = 0
for name in _coordinate_class_to_names[returns[0]]:
out[name] = result[i]
i += 1
for name in _coordinate_class_to_names[returns[1]]:
out[name] = result[i]
i += 1
return out.view(cls.ProjectionClass3D)

elif (
len(returns) == 3
(len(returns) == 2 or (len(returns) == 3 and returns[2] is None))
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
and isinstance(returns[1], type)
and issubclass(returns[1], Longitudinal)
and returns[2] is None
):
result = _toarrays(result)
dtype = []
Expand Down Expand Up @@ -1343,6 +1300,7 @@ def _wrap_result(

elif (
len(returns) == 2
or (len(returns) == 3 and returns[2] is None)
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
and isinstance(returns[1], type)
Expand All @@ -1367,33 +1325,6 @@ def _wrap_result(
i += 1
return out.view(cls.ProjectionClass3D)

elif (
len(returns) == 3
and isinstance(returns[0], type)
and issubclass(returns[0], Azimuthal)
and isinstance(returns[1], type)
and issubclass(returns[1], Longitudinal)
and returns[2] is None
):
result = _toarrays(result)
dtype = []
i = 0
for name in _coordinate_class_to_names[returns[0]]:
dtype.append((name, result[i].dtype))
i += 1
for name in _coordinate_class_to_names[returns[1]]:
dtype.append((name, result[i].dtype))
i += 1
out = numpy.empty(_shape_of(result), dtype=dtype)
i = 0
for name in _coordinate_class_to_names[returns[0]]:
out[name] = result[i]
i += 1
for name in _coordinate_class_to_names[returns[1]]:
out[name] = result[i]
i += 1
return out.view(cls.ProjectionClass3D)

elif (
len(returns) == 3
and isinstance(returns[0], type)
Expand Down
Loading