Skip to content

Commit ccc08ba

Browse files
juanitorduzmaresb
andauthored
Ruff linter (#586)
Co-authored-by: Ben Mares <[email protected]>
1 parent a10c217 commit ccc08ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+399
-669
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,13 @@ repos:
1919
pytensor/tensor/variable\.py|
2020
)$
2121
- id: check-merge-conflict
22-
- repo: https://github.com/asottile/pyupgrade
23-
rev: v3.3.1
22+
- repo: https://github.com/astral-sh/ruff-pre-commit
23+
rev: v0.1.13
2424
hooks:
25-
- id: pyupgrade
26-
args: [--py39-plus]
27-
- repo: https://github.com/psf/black
28-
rev: 23.1.0
29-
hooks:
30-
- id: black
31-
language_version: python3
32-
- repo: https://github.com/pycqa/flake8
33-
rev: 6.0.0
34-
hooks:
35-
- id: flake8
36-
additional_dependencies:
37-
- flake8-comprehensions
38-
- repo: https://github.com/pycqa/isort
39-
rev: 5.12.0
40-
hooks:
41-
- id: isort
42-
- repo: https://github.com/humitos/mirrors-autoflake.git
43-
rev: v1.1
44-
hooks:
45-
- id: autoflake
46-
exclude: |
47-
(?x)^(
48-
.*/?__init__\.py|
49-
pytensor/graph/toolbox\.py|
50-
pytensor/link/jax/jax_dispatch\.py|
51-
pytensor/link/jax/jax_linker\.py|
52-
pytensor/scalar/basic_scipy\.py|
53-
pytensor/tensor/linalg\.py
54-
)$
55-
args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable']
25+
- id: ruff
26+
args: ["--fix", "--show-source"]
27+
- id: ruff-format
28+
args: ["--line-length=88"]
5629
- repo: https://github.com/pre-commit/mirrors-mypy
5730
rev: v1.0.0
5831
hooks:

bin/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import warnings
22

3+
34
warnings.warn(
45
message= "Importing 'bin.pytensor_cache' is deprecated. Import from "
56
"'pytensor.bin.pytensor_cache' instead.",

environment.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ dependencies:
3939
- pydot
4040
- ipython
4141
# code style
42-
- black
43-
- isort
44-
# For linting
45-
- flake8
46-
- pep8
47-
- pyflakes
42+
- ruff
4843
# developer tools
4944
- pre-commit
5045
- packaging

pyproject.toml

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
[build-system]
22
requires = [
3-
"setuptools>=48.0.0",
4-
"cython",
5-
"numpy>=1.17.0",
6-
"versioneer[toml]>=0.28",
3+
"setuptools>=48.0.0",
4+
"cython",
5+
"numpy>=1.17.0",
6+
"versioneer[toml]>=0.28",
77
]
88
build-backend = "setuptools.build_meta"
99

1010
[project]
1111
name = "pytensor"
12-
dynamic = [
13-
'version'
14-
]
12+
dynamic = ['version']
1513
requires-python = ">=3.9,<3.12"
16-
authors = [
17-
{name = "pymc-devs", email = "[email protected]"}
18-
]
14+
authors = [{ name = "pymc-devs", email = "[email protected]" }]
1915
description = "Optimizing compiler for evaluating mathematical expressions on CPUs and GPUs."
2016
readme = "README.rst"
21-
license = {file = "LICENSE.txt"}
17+
license = { file = "LICENSE.txt" }
2218
classifiers = [
2319
"Development Status :: 6 - Mature",
2420
"Intended Audience :: Education",
@@ -71,15 +67,8 @@ documentation = "https://pytensor.readthedocs.io/en/latest/"
7167
pytensor-cache = "pytensor.bin.pytensor_cache:main"
7268

7369
[project.optional-dependencies]
74-
complete = [
75-
"pytensor[jax]",
76-
"pytensor[numba]",
77-
]
78-
development = [
79-
"pytensor[complete]",
80-
"pytensor[tests]",
81-
"pytensor[rtd]"
82-
]
70+
complete = ["pytensor[jax]", "pytensor[numba]"]
71+
development = ["pytensor[complete]", "pytensor[tests]", "pytensor[rtd]"]
8372
tests = [
8473
"pytest",
8574
"pre-commit",
@@ -88,34 +77,16 @@ tests = [
8877
"pytest-benchmark",
8978
"pytest-mock",
9079
]
91-
rtd = [
92-
"sphinx>=5.1.0,<6",
93-
"pygments",
94-
"pydot",
95-
"pydot2",
96-
"pydot-ng",
97-
]
98-
jax = [
99-
"jax",
100-
"jaxlib",
101-
]
102-
numba = [
103-
"numba>=0.55",
104-
"numba-scipy>=0.3.0"
105-
]
80+
rtd = ["sphinx>=5.1.0,<6", "pygments", "pydot", "pydot2", "pydot-ng"]
81+
jax = ["jax", "jaxlib"]
82+
numba = ["numba>=0.55", "numba-scipy>=0.3.0"]
10683

10784
[tool.setuptools.packages.find]
10885
include = ["pytensor*", "bin"]
10986

11087
[tool.setuptools.package-data]
111-
pytensor = [
112-
"py.typed"
113-
]
114-
"pytensor.d3viz" = [
115-
"html/*",
116-
"css/*",
117-
"js/*",
118-
]
88+
pytensor = ["py.typed"]
89+
"pytensor.d3viz" = ["html/*", "css/*", "js/*"]
11990

12091
[tool.coverage.run]
12192
omit = [
@@ -136,14 +107,8 @@ branch = true
136107
relative_files = true
137108

138109
[tool.coverage.report]
139-
omit = [
140-
"pytensor/_version.py",
141-
"tests/",
142-
]
143-
exclude_lines = [
144-
"pragma: no cover",
145-
"if TYPE_CHECKING:",
146-
]
110+
omit = ["pytensor/_version.py", "tests/"]
111+
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
147112
show_missing = true
148113

149114
[tool.versioneer]
@@ -163,41 +128,36 @@ max-line-length = 88
163128
[tool.pylint.messages_control]
164129
disable = ["C0330", "C0326"]
165130

166-
[tool.isort]
167-
profile = "black"
168-
lines_after_imports = 2
169-
lines_between_sections = 1
170-
honor_noqa = true
171-
skip_gitignore = true
172-
skip = "pytensor/version.py"
173-
skip_glob = "**/*.pyx"
174131

175132
[tool.ruff]
176-
select=["C","E","F","W"]
177-
ignore=["E501","E741","C408","C901"]
178-
exclude = [
179-
"doc/",
180-
"pytensor/_version.py",
181-
"bin/pytensor_cache.py",
182-
]
133+
select = ["C", "E", "F", "I", "UP", "W"]
134+
ignore = ["C408", "C901", "E501", "E741", "UP031"]
135+
exclude = ["doc/", "pytensor/_version.py", "bin/pytensor_cache.py"]
136+
137+
138+
[tool.ruff.isort]
139+
lines-after-imports = 2
183140

184141
[tool.ruff.per-file-ignores]
185142
# TODO: Get rid of these:
186-
"**/__init__.py"=["F401","E402","F403"]
187-
"pytensor/tensor/linalg.py"=["F401","F403"]
188-
"pytensor/scalar/basic_scipy.py"=["E402","F403","F401"]
189-
"pytensor/graph/toolbox.py"=["E402","F403","F401"]
190-
"pytensor/link/jax/jax_dispatch.py"=["E402","F403","F401"]
191-
"pytensor/link/jax/jax_linker.py"=["E402","F403","F401"]
192-
"pytensor/sparse/sandbox/sp2.py"=["F401"]
193-
"tests/tensor/test_math_scipy.py"=["E402"]
194-
"tests/sparse/test_basic.py"=["E402"]
195-
"tests/sparse/test_opt.py"=["E402"]
196-
"tests/sparse/test_sp2.py"=["E402"]
197-
"tests/sparse/test_utils.py"=["E402","F401"]
198-
"tests/sparse/sandbox/test_sp.py"=["E402","F401"]
199-
"tests/scalar/test_basic_sympy.py"=["E402"]
200-
"pytensor/graph/rewriting/unify.py"=["F811"]
143+
"**/__init__.py" = ["F401", "E402", "F403"]
144+
"pytensor/tensor/linalg.py" = ["F401", "F403"]
145+
"pytensor/scalar/basic_scipy.py" = ["E402"]
146+
"pytensor/graph/toolbox.py" = ["E402"]
147+
# For the tests we skip because `pytest.importorskip` is used:
148+
"tests/link/jax/test_scalar.py" = ["E402"]
149+
"tests/link/jax/test_tensor_basic.py" = ["E402"]
150+
"tests/link/numba/test_basic.py" = ["E402"]
151+
"tests/link/numba/test_cython_support.py" = ["E402"]
152+
"tests/link/numba/test_performance.py" = ["E402"]
153+
"tests/link/numba/test_sparse.py" = ["E402"]
154+
"tests/link/numba/test_tensor_basic.py" = ["E402"]
155+
"tests/tensor/test_math_scipy.py" = ["E402"]
156+
"tests/sparse/test_basic.py" = ["E402"]
157+
"tests/sparse/test_sp2.py" = ["E402"]
158+
"tests/sparse/test_utils.py" = ["E402"]
159+
"tests/sparse/sandbox/test_sp.py" = ["E402", "F401"]
160+
"versioneer.py" = ["I"]
201161

202162

203163
[tool.mypy]

pytensor/compile/debugmode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ def thunk():
18661866
# Nothing should be in storage map after evaluating
18671867
# each the thunk (specifically the last one)
18681868
for r, s in storage_map.items():
1869-
assert type(s) is list
1869+
assert isinstance(s, list)
18701870
assert s[0] is None
18711871

18721872
# store our output variables to their respective storage lists

pytensor/graph/basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
TYPE_CHECKING,
1818
Any,
1919
Callable,
20-
Deque,
2120
Generic,
2221
Optional,
2322
TypeVar,
@@ -62,6 +61,7 @@ class Node(MetaObject):
6261
keeps track of its parents via `Variable.owner` / `Apply.inputs`.
6362
6463
"""
64+
6565
name: Optional[str]
6666

6767
def get_parents(self):
@@ -1375,7 +1375,7 @@ def _compute_deps_cache(io):
13751375
)
13761376

13771377
_clients: dict[T, list[T]] = {}
1378-
sources: Deque[T] = deque()
1378+
sources: deque[T] = deque()
13791379
search_res_len: int = 0
13801380
for snode, children in search_res:
13811381
search_res_len += 1

pytensor/graph/destroyhandler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,7 @@ def on_attach(self, fgraph):
366366
OrderedSet()
367367
) # set of Apply instances with non-null destroy_map
368368
self.view_i = {} # variable -> variable used in calculation
369-
self.view_o = (
370-
{}
371-
) # variable -> set of variables that use this one as a direct input
369+
self.view_o = {} # variable -> set of variables that use this one as a direct input
372370
# clients: how many times does an apply use a given variable
373371
self.clients = OrderedDict() # variable -> apply -> ninputs
374372
self.stale_droot = True

pytensor/graph/fg.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
import pytensor
88
from pytensor.configdefaults import config
9-
from pytensor.graph.basic import Apply, AtomicVariable, Variable, applys_between
10-
from pytensor.graph.basic import as_string as graph_as_string
119
from pytensor.graph.basic import (
10+
Apply,
11+
AtomicVariable,
12+
Variable,
13+
applys_between,
1214
clone_get_equiv,
1315
graph_inputs,
1416
io_toposort,
1517
vars_between,
1618
)
19+
from pytensor.graph.basic import as_string as graph_as_string
1720
from pytensor.graph.features import AlreadyThere, Feature, ReplaceValidate
1821
from pytensor.graph.utils import MetaObject, MissingInputError, TestValueError
1922
from pytensor.misc.ordered_set import OrderedSet

pytensor/graph/rewriting/basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import traceback
1111
import warnings
1212
from collections import UserList, defaultdict, deque
13-
from collections.abc import Iterable
13+
from collections.abc import Iterable, Sequence
1414
from collections.abc import Iterable as IterableType
15-
from collections.abc import Sequence
1615
from functools import _compose_mro, partial, reduce # type: ignore
1716
from itertools import chain
1817
from typing import TYPE_CHECKING, Callable, Literal, Optional, Union, cast

0 commit comments

Comments
 (0)