Skip to content

Commit fda6343

Browse files
Add mypy, ruff, and refurb. (#51)
1 parent 7976315 commit fda6343

20 files changed

+180
-132
lines changed

.pre-commit-config.yaml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ repos:
2525
- id: python-no-log-warn
2626
- id: python-use-type-annotations
2727
- id: text-unicode-replacement-char
28-
- repo: https://github.com/asottile/pyupgrade
29-
rev: v3.3.1
30-
hooks:
31-
- id: pyupgrade
32-
args: [--py37-plus]
3328
- repo: https://github.com/asottile/reorder_python_imports
3429
rev: v3.9.0
3530
hooks:
@@ -40,7 +35,7 @@ repos:
4035
hooks:
4136
- id: setup-cfg-fmt
4237
- repo: https://github.com/PyCQA/docformatter
43-
rev: v1.5.0
38+
rev: v1.5.1
4439
hooks:
4540
- id: docformatter
4641
args: [--in-place, --wrap-summaries, "88", --wrap-descriptions, "88", --blank]
@@ -53,27 +48,15 @@ repos:
5348
hooks:
5449
- id: blacken-docs
5550
additional_dependencies: [black]
56-
- repo: https://github.com/PyCQA/flake8
57-
rev: 5.0.4
51+
- repo: https://github.com/charliermarsh/ruff-pre-commit
52+
rev: v0.0.215
5853
hooks:
59-
- id: flake8
60-
types: [python]
61-
additional_dependencies: [
62-
flake8-alfred,
63-
flake8-bugbear,
64-
flake8-builtins,
65-
flake8-comprehensions,
66-
flake8-docstrings,
67-
flake8-eradicate,
68-
flake8-print,
69-
flake8-pytest-style,
70-
flake8-todo,
71-
flake8-typing-imports,
72-
flake8-unused-arguments,
73-
pep8-naming,
74-
pydocstyle,
75-
Pygments,
76-
]
54+
- id: ruff
55+
- repo: https://github.com/dosisod/refurb
56+
rev: v1.9.1
57+
hooks:
58+
- id: refurb
59+
args: [--ignore, FURB126]
7760
- repo: https://github.com/econchick/interrogate
7861
rev: 1.5.0
7962
hooks:
@@ -93,6 +76,21 @@ repos:
9376
hooks:
9477
- id: codespell
9578
args: [-L als, -L falsy]
79+
- repo: https://github.com/pre-commit/mirrors-mypy
80+
rev: 'v0.991'
81+
hooks:
82+
- id: mypy
83+
args: [
84+
--no-strict-optional,
85+
--ignore-missing-imports,
86+
]
87+
additional_dependencies: [
88+
attrs>=21.3.0,
89+
click,
90+
types-PyYAML,
91+
types-setuptools
92+
]
93+
pass_filenames: false
9694
- repo: https://github.com/mgedmin/check-manifest
9795
rev: "0.49"
9896
hooks:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ releases are available on [Anaconda.org](https://anaconda.org/conda-forge/pytask
88

99
- {pull}`49` removes support for INI configurations.
1010
- {pull}`50` removes the deprecation message and related code to the old API.
11+
- {pull}`51` adds mypy, ruff, and refurb.
1112

1213
## 0.2.1 - 2022-04-19
1314

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ exclude tox.ini
77

88
include README.md
99
include LICENSE
10+
11+
recursive-include src *.typed

pyproject.toml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,58 @@ build-backend = "setuptools.build_meta"
55

66
[tool.setuptools_scm]
77
write_to = "src/pytask_latex/_version.py"
8+
9+
10+
[tool.mypy]
11+
files = ["src", "tests"]
12+
check_untyped_defs = true
13+
disallow_any_generics = true
14+
disallow_incomplete_defs = true
15+
disallow_untyped_defs = true
16+
no_implicit_optional = true
17+
warn_redundant_casts = true
18+
warn_unused_ignores = true
19+
20+
21+
[[tool.mypy.overrides]]
22+
module = "tests.*"
23+
disallow_untyped_defs = false
24+
ignore_errors = true
25+
26+
27+
[tool.ruff]
28+
target-version = "py37"
29+
select = ["ALL"]
30+
fix = true
31+
extend-ignore = [
32+
# Numpy docstyle
33+
"D107",
34+
"D203",
35+
"D212",
36+
"D213",
37+
"D402",
38+
"D413",
39+
"D415",
40+
"D416",
41+
"D417",
42+
# Others.
43+
"D404", # Do not start module docstring with "This".
44+
"RET504", # unnecessary variable assignment before return.
45+
"S101", # raise errors for asserts.
46+
"B905", # strict parameter for zip that was implemented in py310.
47+
"I", # ignore isort
48+
"ANN101", # type annotating self
49+
"ANN102", # type annotating cls
50+
"FBT", # flake8-boolean-trap
51+
"EM", # flake8-errmsg
52+
"ANN401", # flake8-annotate typing.Any
53+
"PD", # pandas-vet
54+
]
55+
56+
57+
[tool.ruff.per-file-ignores]
58+
"tests/*" = ["D", "ANN"]
59+
60+
61+
[tool.ruff.pydocstyle]
62+
convention = "numpy"

src/pytask_latex/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""This module contains the main namespace."""
12
from __future__ import annotations
23

34
try:

src/pytask_latex/collect.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,19 @@
2727
from pytask_latex.utils import to_list
2828

2929

30-
_ERROR_MSG = """The old syntax for @pytask.mark.latex was suddenly deprecated starting \
31-
with pytask-latex v0.2 to provide a better user experience. Thank you for your \
32-
understanding!
33-
34-
It is recommended to upgrade to the new syntax, so you enjoy all the benefits of v0.2 of
35-
pytask and a better interface for pytask-latex.
36-
37-
You can find a manual here: \
38-
https://github.com/pytask-dev/pytask-latex/blob/v0.2.0/README.md
39-
40-
Upgrading can be as easy as rewriting your current task from
41-
42-
@pytask.mark.latex("--some-option")
43-
@pytask.mark.depends_on({"source": "script.tex")
44-
@pytask.mark.produces("document.pdf")
45-
def task_latex():
46-
...
47-
48-
to
49-
50-
from pytask_latex import compilation_steps as cs
51-
52-
53-
@pytask.mark.latex(
54-
script="script.tex",
55-
document="document.pdf",
56-
compilation_steps=cs.latexmk(options="--some-options"),
57-
)
58-
def task_latex():
59-
...
60-
61-
You can also fix the version of pytask and pytask-latex to <0.2, so you do not have to \
62-
to upgrade. At the same time, you will not enjoy the improvements released with \
63-
version v0.2 of pytask and pytask-latex.
64-
65-
"""
66-
67-
6830
def latex(
6931
*,
7032
script: str | Path,
7133
document: str | Path,
7234
compilation_steps: str
7335
| Callable[..., Any]
74-
| Sequence[str | Callable[..., Any]] = None,
75-
) -> tuple[str | Path | None, list[Callable[..., Any]]]:
36+
| Sequence[str | Callable[..., Any]]
37+
| None = None,
38+
) -> tuple[
39+
str | Path,
40+
str | Path,
41+
str | Callable[..., Any] | Sequence[str | Callable[..., Any]] | None,
42+
]:
7643
"""Specify command line options for latexmk.
7744
7845
Parameters
@@ -88,8 +55,16 @@ def latex(
8855
return script, document, compilation_steps
8956

9057

91-
def compile_latex_document(compilation_steps, path_to_tex, path_to_document):
92-
"""Replaces the dummy function provided by the user."""
58+
def compile_latex_document(
59+
compilation_steps: list[Callable[..., Any]],
60+
path_to_tex: Path,
61+
path_to_document: Path,
62+
) -> None:
63+
"""Compile a LaTeX document iterating over compilations steps.
64+
65+
Replaces the placeholder function provided by the user.
66+
67+
"""
9368
for step in compilation_steps:
9469
try:
9570
step(path_to_tex=path_to_tex, path_to_document=path_to_document)
@@ -98,7 +73,9 @@ def compile_latex_document(compilation_steps, path_to_tex, path_to_document):
9873

9974

10075
@hookimpl
101-
def pytask_collect_task(session, path, name, obj):
76+
def pytask_collect_task(
77+
session: Session, path: Path, name: str, obj: Any
78+
) -> Task | None:
10279
"""Perform some checks."""
10380
__tracebackhide__ = True
10481

@@ -130,7 +107,7 @@ def pytask_collect_task(session, path, name, obj):
130107
task = Task(
131108
base_name=name,
132109
path=path,
133-
function=_copy_func(compile_latex_document),
110+
function=_copy_func(compile_latex_document), # type: ignore[arg-type]
134111
depends_on=dependencies,
135112
produces=products,
136113
markers=markers,
@@ -154,7 +131,7 @@ def pytask_collect_task(session, path, name, obj):
154131

155132
if not (
156133
isinstance(document_node, FilePathNode)
157-
and document_node.value.suffix in [".pdf", ".ps", ".dvi"]
134+
and document_node.value.suffix in (".pdf", ".ps", ".dvi")
158135
):
159136
raise ValueError(
160137
"The 'document' keyword of the @pytask.mark.latex decorator must point "
@@ -181,9 +158,10 @@ def pytask_collect_task(session, path, name, obj):
181158
task = _add_latex_dependencies_retroactively(task, session)
182159

183160
return task
161+
return None
184162

185163

186-
def _add_latex_dependencies_retroactively(task, session):
164+
def _add_latex_dependencies_retroactively(task: Task, session: Session) -> Task:
187165
"""Add dependencies from LaTeX document to task.
188166
189167
Unfortunately, the dependencies have to be added retroactively, after the task has
@@ -291,7 +269,9 @@ def _collect_node(
291269
return collected_node
292270

293271

294-
def _parse_compilation_steps(compilation_steps):
272+
def _parse_compilation_steps(
273+
compilation_steps: str | Callable[..., Any] | Sequence[str | Callable[..., Any]]
274+
) -> list[Callable[..., Any]]:
295275
"""Parse compilation steps."""
296276
__tracebackhide__ = True
297277

@@ -303,7 +283,7 @@ def _parse_compilation_steps(compilation_steps):
303283
try:
304284
parsed_step = getattr(cs, step)
305285
except AttributeError:
306-
raise ValueError(f"Compilation step {step!r} is unknown.")
286+
raise ValueError(f"Compilation step {step!r} is unknown.") from None
307287
parsed_compilation_steps.append(parsed_step())
308288
elif callable(step):
309289
parsed_compilation_steps.append(step)

src/pytask_latex/compilation_steps.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ def compilation_step(path_to_tex: Path, path_to_document: Path):
1313
from __future__ import annotations
1414

1515
import subprocess
16+
from pathlib import Path
17+
from typing import Any
18+
from typing import Callable
1619

1720
from pytask_latex.path import relative_to
1821
from pytask_latex.utils import to_list
1922

2023

21-
def latexmk(options=("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")):
24+
def latexmk(
25+
options: str
26+
| list[str]
27+
| tuple[str, ...] = ("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
28+
) -> Callable[..., Any]:
2229
"""Compilation step that calls latexmk."""
2330
options = [str(i) for i in to_list(options)]
2431

25-
def run_latexmk(path_to_tex, path_to_document):
32+
def run_latexmk(path_to_tex: Path, path_to_document: Path) -> None:
2633
job_name_opt = [f"--jobname={path_to_document.stem}"]
2734
out_dir_opt = [
2835
f"--output-directory={relative_to(path_to_tex, path_to_document.parent)}"

src/pytask_latex/execute.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
from pytask import has_mark
77
from pytask import hookimpl
8+
from pytask import Task
89

910

1011
@hookimpl
11-
def pytask_execute_task_setup(task):
12+
def pytask_execute_task_setup(task: Task) -> None:
1213
"""Check that latexmk is found on the PATH if a LaTeX task should be executed."""
13-
if has_mark(task, "latex"):
14-
if shutil.which("latexmk") is None:
15-
raise RuntimeError(
16-
"latexmk is needed to compile LaTeX documents, but it is not found on "
17-
"your PATH."
18-
)
14+
if has_mark(task, "latex") and shutil.which("latexmk") is None:
15+
raise RuntimeError(
16+
"latexmk is needed to compile LaTeX documents, but it is not found on "
17+
"your PATH."
18+
)

src/pytask_latex/parametrize.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""Parametrize tasks."""
22
from __future__ import annotations
33

4+
from typing import Any
5+
46
import pytask
57
from pytask import hookimpl
68

79

810
@hookimpl
9-
def pytask_parametrize_kwarg_to_marker(obj, kwargs):
11+
def pytask_parametrize_kwarg_to_marker(obj: Any, kwargs: dict[str, Any]) -> None:
1012
"""Register kwargs as latex marker."""
11-
if callable(obj):
12-
if "latex" in kwargs:
13-
pytask.mark.latex(**kwargs.pop("latex"))(obj)
13+
if callable(obj) and "latex" in kwargs:
14+
pytask.mark.latex(**kwargs.pop("latex"))(obj)

src/pytask_latex/plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Entry-point for the plugin."""
22
from __future__ import annotations
33

4+
from pluggy import PluginManager
45
from pytask import hookimpl
56
from pytask_latex import collect
67
from pytask_latex import config
@@ -9,7 +10,7 @@
910

1011

1112
@hookimpl
12-
def pytask_add_hooks(pm):
13+
def pytask_add_hooks(pm: PluginManager) -> None:
1314
"""Register some plugins."""
1415
pm.register(collect)
1516
pm.register(config)

src/pytask_latex/py.typed

Whitespace-only changes.

src/pytask_latex/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
"""This module contains shared functions."""
12
from __future__ import annotations
23

4+
from typing import Any
35
from typing import Sequence
46

57

6-
def to_list(scalar_or_iter):
8+
def to_list(scalar_or_iter: Any) -> list[Any]:
79
"""Convert scalars and iterables to list.
810
911
Parameters

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)