Skip to content

Commit

Permalink
Ruff linter (#586)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Mares <services-git-throwaway1@tensorial.com>
  • Loading branch information
juanitorduz and maresb committed Jan 15, 2024
1 parent a10c217 commit ccc08ba
Show file tree
Hide file tree
Showing 70 changed files with 399 additions and 669 deletions.
39 changes: 6 additions & 33 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,13 @@ repos:
pytensor/tensor/variable\.py|
)$
- id: check-merge-conflict
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: pyupgrade
args: [--py39-plus]
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-comprehensions
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/humitos/mirrors-autoflake.git
rev: v1.1
hooks:
- id: autoflake
exclude: |
(?x)^(
.*/?__init__\.py|
pytensor/graph/toolbox\.py|
pytensor/link/jax/jax_dispatch\.py|
pytensor/link/jax/jax_linker\.py|
pytensor/scalar/basic_scipy\.py|
pytensor/tensor/linalg\.py
)$
args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable']
- id: ruff
args: ["--fix", "--show-source"]
- id: ruff-format
args: ["--line-length=88"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.0.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions bin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings


warnings.warn(
message= "Importing 'bin.pytensor_cache' is deprecated. Import from "
"'pytensor.bin.pytensor_cache' instead.",
Expand Down
7 changes: 1 addition & 6 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ dependencies:
- pydot
- ipython
# code style
- black
- isort
# For linting
- flake8
- pep8
- pyflakes
- ruff
# developer tools
- pre-commit
- packaging
Expand Down
122 changes: 41 additions & 81 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
[build-system]
requires = [
"setuptools>=48.0.0",
"cython",
"numpy>=1.17.0",
"versioneer[toml]>=0.28",
"setuptools>=48.0.0",
"cython",
"numpy>=1.17.0",
"versioneer[toml]>=0.28",
]
build-backend = "setuptools.build_meta"

[project]
name = "pytensor"
dynamic = [
'version'
]
dynamic = ['version']
requires-python = ">=3.9,<3.12"
authors = [
{name = "pymc-devs", email = "pymc.devs@gmail.com"}
]
authors = [{ name = "pymc-devs", email = "pymc.devs@gmail.com" }]
description = "Optimizing compiler for evaluating mathematical expressions on CPUs and GPUs."
readme = "README.rst"
license = {file = "LICENSE.txt"}
license = { file = "LICENSE.txt" }
classifiers = [
"Development Status :: 6 - Mature",
"Intended Audience :: Education",
Expand Down Expand Up @@ -71,15 +67,8 @@ documentation = "https://pytensor.readthedocs.io/en/latest/"
pytensor-cache = "pytensor.bin.pytensor_cache:main"

[project.optional-dependencies]
complete = [
"pytensor[jax]",
"pytensor[numba]",
]
development = [
"pytensor[complete]",
"pytensor[tests]",
"pytensor[rtd]"
]
complete = ["pytensor[jax]", "pytensor[numba]"]
development = ["pytensor[complete]", "pytensor[tests]", "pytensor[rtd]"]
tests = [
"pytest",
"pre-commit",
Expand All @@ -88,34 +77,16 @@ tests = [
"pytest-benchmark",
"pytest-mock",
]
rtd = [
"sphinx>=5.1.0,<6",
"pygments",
"pydot",
"pydot2",
"pydot-ng",
]
jax = [
"jax",
"jaxlib",
]
numba = [
"numba>=0.55",
"numba-scipy>=0.3.0"
]
rtd = ["sphinx>=5.1.0,<6", "pygments", "pydot", "pydot2", "pydot-ng"]
jax = ["jax", "jaxlib"]
numba = ["numba>=0.55", "numba-scipy>=0.3.0"]

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

[tool.setuptools.package-data]
pytensor = [
"py.typed"
]
"pytensor.d3viz" = [
"html/*",
"css/*",
"js/*",
]
pytensor = ["py.typed"]
"pytensor.d3viz" = ["html/*", "css/*", "js/*"]

[tool.coverage.run]
omit = [
Expand All @@ -136,14 +107,8 @@ branch = true
relative_files = true

[tool.coverage.report]
omit = [
"pytensor/_version.py",
"tests/",
]
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
]
omit = ["pytensor/_version.py", "tests/"]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
show_missing = true

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

[tool.isort]
profile = "black"
lines_after_imports = 2
lines_between_sections = 1
honor_noqa = true
skip_gitignore = true
skip = "pytensor/version.py"
skip_glob = "**/*.pyx"

[tool.ruff]
select=["C","E","F","W"]
ignore=["E501","E741","C408","C901"]
exclude = [
"doc/",
"pytensor/_version.py",
"bin/pytensor_cache.py",
]
select = ["C", "E", "F", "I", "UP", "W"]
ignore = ["C408", "C901", "E501", "E741", "UP031"]
exclude = ["doc/", "pytensor/_version.py", "bin/pytensor_cache.py"]


[tool.ruff.isort]
lines-after-imports = 2

[tool.ruff.per-file-ignores]
# TODO: Get rid of these:
"**/__init__.py"=["F401","E402","F403"]
"pytensor/tensor/linalg.py"=["F401","F403"]
"pytensor/scalar/basic_scipy.py"=["E402","F403","F401"]
"pytensor/graph/toolbox.py"=["E402","F403","F401"]
"pytensor/link/jax/jax_dispatch.py"=["E402","F403","F401"]
"pytensor/link/jax/jax_linker.py"=["E402","F403","F401"]
"pytensor/sparse/sandbox/sp2.py"=["F401"]
"tests/tensor/test_math_scipy.py"=["E402"]
"tests/sparse/test_basic.py"=["E402"]
"tests/sparse/test_opt.py"=["E402"]
"tests/sparse/test_sp2.py"=["E402"]
"tests/sparse/test_utils.py"=["E402","F401"]
"tests/sparse/sandbox/test_sp.py"=["E402","F401"]
"tests/scalar/test_basic_sympy.py"=["E402"]
"pytensor/graph/rewriting/unify.py"=["F811"]
"**/__init__.py" = ["F401", "E402", "F403"]
"pytensor/tensor/linalg.py" = ["F401", "F403"]
"pytensor/scalar/basic_scipy.py" = ["E402"]
"pytensor/graph/toolbox.py" = ["E402"]
# For the tests we skip because `pytest.importorskip` is used:
"tests/link/jax/test_scalar.py" = ["E402"]
"tests/link/jax/test_tensor_basic.py" = ["E402"]
"tests/link/numba/test_basic.py" = ["E402"]
"tests/link/numba/test_cython_support.py" = ["E402"]
"tests/link/numba/test_performance.py" = ["E402"]
"tests/link/numba/test_sparse.py" = ["E402"]
"tests/link/numba/test_tensor_basic.py" = ["E402"]
"tests/tensor/test_math_scipy.py" = ["E402"]
"tests/sparse/test_basic.py" = ["E402"]
"tests/sparse/test_sp2.py" = ["E402"]
"tests/sparse/test_utils.py" = ["E402"]
"tests/sparse/sandbox/test_sp.py" = ["E402", "F401"]
"versioneer.py" = ["I"]


[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion pytensor/compile/debugmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ def thunk():
# Nothing should be in storage map after evaluating
# each the thunk (specifically the last one)
for r, s in storage_map.items():
assert type(s) is list
assert isinstance(s, list)
assert s[0] is None

# store our output variables to their respective storage lists
Expand Down
4 changes: 2 additions & 2 deletions pytensor/graph/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
TYPE_CHECKING,
Any,
Callable,
Deque,
Generic,
Optional,
TypeVar,
Expand Down Expand Up @@ -62,6 +61,7 @@ class Node(MetaObject):
keeps track of its parents via `Variable.owner` / `Apply.inputs`.
"""

name: Optional[str]

def get_parents(self):
Expand Down Expand Up @@ -1375,7 +1375,7 @@ def _compute_deps_cache(io):
)

_clients: dict[T, list[T]] = {}
sources: Deque[T] = deque()
sources: deque[T] = deque()
search_res_len: int = 0
for snode, children in search_res:
search_res_len += 1
Expand Down
4 changes: 1 addition & 3 deletions pytensor/graph/destroyhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,7 @@ def on_attach(self, fgraph):
OrderedSet()
) # set of Apply instances with non-null destroy_map
self.view_i = {} # variable -> variable used in calculation
self.view_o = (
{}
) # variable -> set of variables that use this one as a direct input
self.view_o = {} # variable -> set of variables that use this one as a direct input
# clients: how many times does an apply use a given variable
self.clients = OrderedDict() # variable -> apply -> ninputs
self.stale_droot = True
Expand Down
7 changes: 5 additions & 2 deletions pytensor/graph/fg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

import pytensor
from pytensor.configdefaults import config
from pytensor.graph.basic import Apply, AtomicVariable, Variable, applys_between
from pytensor.graph.basic import as_string as graph_as_string
from pytensor.graph.basic import (
Apply,
AtomicVariable,
Variable,
applys_between,
clone_get_equiv,
graph_inputs,
io_toposort,
vars_between,
)
from pytensor.graph.basic import as_string as graph_as_string
from pytensor.graph.features import AlreadyThere, Feature, ReplaceValidate
from pytensor.graph.utils import MetaObject, MissingInputError, TestValueError
from pytensor.misc.ordered_set import OrderedSet
Expand Down
3 changes: 1 addition & 2 deletions pytensor/graph/rewriting/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import traceback
import warnings
from collections import UserList, defaultdict, deque
from collections.abc import Iterable
from collections.abc import Iterable, Sequence
from collections.abc import Iterable as IterableType
from collections.abc import Sequence
from functools import _compose_mro, partial, reduce # type: ignore
from itertools import chain
from typing import TYPE_CHECKING, Callable, Literal, Optional, Union, cast
Expand Down

0 comments on commit ccc08ba

Please sign in to comment.