Skip to content

Commit fe5c164

Browse files
committed
feat: add flake8 plugins and enable pre-commit hook
1 parent f5b292c commit fe5c164

File tree

15 files changed

+105
-26
lines changed

15 files changed

+105
-26
lines changed

.flake8

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[flake8]
2+
max-line-length = 120
3+
max-doc-length = 100
4+
select = B,C,E,F,W,Y,SIM
5+
ignore =
6+
# E203: whitespace before ':'
7+
# W503: line break before binary operator
8+
# W504: line break after binary operator
9+
# format by black
10+
E203,W503,W504,
11+
# E501: line too long
12+
# W505: doc line too long
13+
# too long docstring due to long example blocks
14+
E501,W505,
15+
per-file-ignores =
16+
# F401: module imported but unused
17+
# intentionally unused imports
18+
__init__.py: F401
19+
# F401: module imported but unused
20+
# F403: unable to detect undefined names
21+
# F405: member mey be undefined, or defined from star imports
22+
# members populated from optree
23+
torchopt/pytree.py: F401,F403,F405
24+
# E301: expected 1 blank line
25+
# E302: expected 2 blank lines
26+
# E305: expected 2 blank lines after class or function definition
27+
# E701: multiple statements on one line (colon)
28+
# E704: multiple statements on one line (def)
29+
# format by black
30+
*.pyi: E301,E302,E305,E701,E704
31+
exclude =
32+
.git,
33+
.vscode,
34+
venv,
35+
third-party,
36+
__pycache__,
37+
docs/source/conf.py,
38+
build,
39+
dist,
40+
examples,
41+
tests

.pre-commit-config.yaml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
ci:
44
skip: [pylint]
55
autofix_prs: true
6-
autofix_commit_msg: 'fix: [pre-commit.ci] auto fixes [...]'
7-
autoupdate_commit_msg: 'chore(pre-commit): [pre-commit.ci] autoupdate'
6+
autofix_commit_msg: "fix: [pre-commit.ci] auto fixes [...]"
7+
autoupdate_commit_msg: "chore(pre-commit): [pre-commit.ci] autoupdate"
88
repos:
99
- repo: https://github.com/pre-commit/pre-commit-hooks
1010
rev: v4.4.0
@@ -26,8 +26,8 @@ repos:
2626
- repo: https://github.com/pre-commit/mirrors-clang-format
2727
rev: v15.0.7
2828
hooks:
29-
- id: clang-format
30-
stages: [commit, push, manual]
29+
- id: clang-format
30+
stages: [commit, push, manual]
3131
- repo: https://github.com/PyCQA/isort
3232
rev: 5.12.0
3333
hooks:
@@ -48,6 +48,22 @@ repos:
4848
(?x)(
4949
^examples/
5050
)
51+
- repo: https://github.com/pycqa/flake8
52+
rev: 6.0.0
53+
hooks:
54+
- id: flake8
55+
additional_dependencies:
56+
- flake8-bugbear
57+
- flake8-comprehensions
58+
- flake8-docstrings
59+
- flake8-pyi
60+
- flake8-simplify
61+
exclude: |
62+
(?x)(
63+
^examples/|
64+
^tests/|
65+
^docs/source/conf.py$
66+
)
5167
- repo: local
5268
hooks:
5369
- id: pylint
@@ -68,7 +84,7 @@ repos:
6884
rev: 6.3.0
6985
hooks:
7086
- id: pydocstyle
71-
additional_dependencies: ['.[toml]']
87+
additional_dependencies: [".[toml]"]
7288
exclude: |
7389
(?x)(
7490
^.github/|

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ pylint-install:
4646

4747
flake8-install:
4848
$(call check_pip_install,flake8)
49-
$(call check_pip_install_extra,flake8-bugbear,flake8-bugbear)
49+
$(call check_pip_install,flake8-bugbear)
50+
$(call check_pip_install,flake8-comprehensions)
51+
$(call check_pip_install,flake8-docstrings)
52+
$(call check_pip_install,flake8-pyi)
53+
$(call check_pip_install,flake8-simplify)
5054

5155
py-format-install:
5256
$(call check_pip_install,isort)
@@ -122,7 +126,7 @@ pylint: pylint-install
122126
$(PYTHON) -m pylint $(PROJECT_PATH)
123127

124128
flake8: flake8-install
125-
$(PYTHON) -m flake8 $(PYTHON_FILES) --count --select=E9,F63,F7,F82,E225,E251 --show-source --statistics
129+
$(PYTHON) -m flake8 --count --show-source --statistics
126130

127131
py-format: py-format-install
128132
$(PYTHON) -m isort --project $(PROJECT_NAME) --check $(PYTHON_FILES) && \

conda-recipe.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ dependencies:
9090
- mypy >= 0.990
9191
- flake8
9292
- flake8-bugbear
93-
- doc8 < 1.0.0a0
93+
- flake8-comprehensions
94+
- flake8-docstrings
95+
- flake8-pyi
96+
- flake8-simplify
97+
- doc8
9498
- pydocstyle
9599
- clang-format >= 14
96100
- clang-tools >= 14 # clang-tidy

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ lint = [
7474
"mypy >= 0.990",
7575
"flake8",
7676
"flake8-bugbear",
77+
"flake8-comprehensions",
78+
"flake8-docstrings",
79+
"flake8-pyi",
80+
"flake8-simplify",
7781
"doc8 < 1.0.0a0", # unpin this when we drop support for Python 3.7
7882
"pydocstyle[toml]",
7983
"pyenchant",

setup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ def build_extension(self, ext):
9696
LINUX = platform.system() == 'Linux'
9797
MACOS = platform.system() == 'Darwin'
9898
WINDOWS = platform.system() == 'Windows'
99-
ext_kwargs = dict(
100-
cmdclass={'build_ext': cmake_build_ext},
101-
ext_modules=[
99+
ext_kwargs = {
100+
'cmdclass': {'build_ext': cmake_build_ext},
101+
'ext_modules': [
102102
CMakeExtension(
103103
'torchopt._C',
104104
source_dir=HERE,
105105
optional=not (LINUX and CIBUILDWHEEL),
106106
)
107107
],
108-
)
108+
}
109109

110110
TORCHOPT_NO_EXTENSIONS = (
111111
bool(os.getenv('TORCHOPT_NO_EXTENSIONS', '')) or WINDOWS or (MACOS and CIBUILDWHEEL)
@@ -123,7 +123,7 @@ def build_extension(self, ext):
123123
VERSION_FILE.write_text(
124124
data=re.sub(
125125
r"""__version__\s*=\s*('[^']+'|"[^"]+")""",
126-
f"__version__ = '{version.__version__}'",
126+
f'__version__ = {version.__version__!r}',
127127
string=VERSION_CONTENT,
128128
),
129129
encoding='UTF-8',

tests/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pylint[spelling] >= 2.15.0
1616
mypy >= 0.990
1717
flake8
1818
flake8-bugbear
19+
flake8-comprehensions
20+
flake8-docstrings
21+
flake8-pyi
22+
flake8-simplify
1923
# https://github.com/PyCQA/doc8/issues/112
2024
doc8 < 1.0.0a0
2125
pydocstyle[toml]

torchopt/_C/adam_op.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
# pylint: disable=all
1717

18-
from __future__ import annotations
19-
2018
import torch
2119

2220
def forward_(

torchopt/diff/implicit/decorator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _root_vjp(
9191
grad_outputs: TupleOfTensors,
9292
output_is_tensor: bool,
9393
argnums: tuple[int, ...],
94-
solve: Callable[..., TensorOrTensors] = linear_solve.solve_normal_cg(),
94+
solve: Callable[..., TensorOrTensors],
9595
) -> TupleOfOptionalTensors:
9696
if output_is_tensor:
9797

@@ -414,7 +414,7 @@ def custom_root(
414414
optimality_fn: Callable[..., TensorOrTensors],
415415
argnums: int | tuple[int, ...],
416416
has_aux: bool = False,
417-
solve: Callable[..., TensorOrTensors] = linear_solve.solve_normal_cg(),
417+
solve: Callable[..., TensorOrTensors] | None = None,
418418
) -> Callable[
419419
[Callable[..., TensorOrTensors | tuple[TensorOrTensors, Any]]],
420420
Callable[..., TensorOrTensors | tuple[TensorOrTensors, Any]],
@@ -465,6 +465,9 @@ def solver_fn(params, arg1, arg2, ...):
465465
else:
466466
assert 0 not in argnums
467467

468+
if solve is None:
469+
solve = linear_solve.solve_normal_cg()
470+
468471
return functools.partial(
469472
_custom_root,
470473
optimality_fn=optimality_fn,

torchopt/diff/zero_order/decorator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def __init__(self, sample_fn: SampleFunc) -> None:
3535
"""Wrap a sample function to make it a :class:`Samplable` object."""
3636
self.sample_fn = sample_fn
3737

38-
def sample(self, sample_shape: torch.Size = torch.Size()) -> torch.Tensor | Sequence[Numeric]:
38+
def sample(
39+
self,
40+
sample_shape: torch.Size = torch.Size(), # noqa: B008
41+
) -> torch.Tensor | Sequence[Numeric]:
3942
# pylint: disable-next=line-too-long
4043
"""Generate a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched."""
4144
return self.sample_fn(sample_shape)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy