Skip to content

Add mypy, ruff, and refurb. #51

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

Merged
merged 6 commits into from
Jan 9, 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
50 changes: 24 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ repos:
- id: python-no-log-warn
- id: python-use-type-annotations
- id: text-unicode-replacement-char
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
Expand All @@ -40,7 +35,7 @@ repos:
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/docformatter
rev: v1.5.0
rev: v1.5.1
hooks:
- id: docformatter
args: [--in-place, --wrap-summaries, "88", --wrap-descriptions, "88", --blank]
Expand All @@ -53,27 +48,15 @@ repos:
hooks:
- id: blacken-docs
additional_dependencies: [black]
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.215
hooks:
- id: flake8
types: [python]
additional_dependencies: [
flake8-alfred,
flake8-bugbear,
flake8-builtins,
flake8-comprehensions,
flake8-docstrings,
flake8-eradicate,
flake8-print,
flake8-pytest-style,
flake8-todo,
flake8-typing-imports,
flake8-unused-arguments,
pep8-naming,
pydocstyle,
Pygments,
]
- id: ruff
- repo: https://github.com/dosisod/refurb
rev: v1.9.1
hooks:
- id: refurb
args: [--ignore, FURB126]
- repo: https://github.com/econchick/interrogate
rev: 1.5.0
hooks:
Expand All @@ -93,6 +76,21 @@ repos:
hooks:
- id: codespell
args: [-L als, -L falsy]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.991'
hooks:
- id: mypy
args: [
--no-strict-optional,
--ignore-missing-imports,
]
additional_dependencies: [
attrs>=21.3.0,
click,
types-PyYAML,
types-setuptools
]
pass_filenames: false
- repo: https://github.com/mgedmin/check-manifest
rev: "0.49"
hooks:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ releases are available on [Anaconda.org](https://anaconda.org/conda-forge/pytask

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

## 0.2.1 - 2022-04-19

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ exclude tox.ini

include README.md
include LICENSE

recursive-include src *.typed
55 changes: 55 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,58 @@ build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "src/pytask_latex/_version.py"


[tool.mypy]
files = ["src", "tests"]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true


[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false
ignore_errors = true


[tool.ruff]
target-version = "py37"
select = ["ALL"]
fix = true
extend-ignore = [
# Numpy docstyle
"D107",
"D203",
"D212",
"D213",
"D402",
"D413",
"D415",
"D416",
"D417",
# Others.
"D404", # Do not start module docstring with "This".
"RET504", # unnecessary variable assignment before return.
"S101", # raise errors for asserts.
"B905", # strict parameter for zip that was implemented in py310.
"I", # ignore isort
"ANN101", # type annotating self
"ANN102", # type annotating cls
"FBT", # flake8-boolean-trap
"EM", # flake8-errmsg
"ANN401", # flake8-annotate typing.Any
"PD", # pandas-vet
]


[tool.ruff.per-file-ignores]
"tests/*" = ["D", "ANN"]


[tool.ruff.pydocstyle]
convention = "numpy"
1 change: 1 addition & 0 deletions src/pytask_latex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This module contains the main namespace."""
from __future__ import annotations

try:
Expand Down
76 changes: 28 additions & 48 deletions src/pytask_latex/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,19 @@
from pytask_latex.utils import to_list


_ERROR_MSG = """The old syntax for @pytask.mark.latex was suddenly deprecated starting \
with pytask-latex v0.2 to provide a better user experience. Thank you for your \
understanding!

It is recommended to upgrade to the new syntax, so you enjoy all the benefits of v0.2 of
pytask and a better interface for pytask-latex.

You can find a manual here: \
https://github.com/pytask-dev/pytask-latex/blob/v0.2.0/README.md

Upgrading can be as easy as rewriting your current task from

@pytask.mark.latex("--some-option")
@pytask.mark.depends_on({"source": "script.tex")
@pytask.mark.produces("document.pdf")
def task_latex():
...

to

from pytask_latex import compilation_steps as cs


@pytask.mark.latex(
script="script.tex",
document="document.pdf",
compilation_steps=cs.latexmk(options="--some-options"),
)
def task_latex():
...

You can also fix the version of pytask and pytask-latex to <0.2, so you do not have to \
to upgrade. At the same time, you will not enjoy the improvements released with \
version v0.2 of pytask and pytask-latex.

"""


def latex(
*,
script: str | Path,
document: str | Path,
compilation_steps: str
| Callable[..., Any]
| Sequence[str | Callable[..., Any]] = None,
) -> tuple[str | Path | None, list[Callable[..., Any]]]:
| Sequence[str | Callable[..., Any]]
| None = None,
) -> tuple[
str | Path,
str | Path,
str | Callable[..., Any] | Sequence[str | Callable[..., Any]] | None,
]:
"""Specify command line options for latexmk.

Parameters
Expand All @@ -88,8 +55,16 @@ def latex(
return script, document, compilation_steps


def compile_latex_document(compilation_steps, path_to_tex, path_to_document):
"""Replaces the dummy function provided by the user."""
def compile_latex_document(
compilation_steps: list[Callable[..., Any]],
path_to_tex: Path,
path_to_document: Path,
) -> None:
"""Compile a LaTeX document iterating over compilations steps.

Replaces the placeholder function provided by the user.

"""
for step in compilation_steps:
try:
step(path_to_tex=path_to_tex, path_to_document=path_to_document)
Expand All @@ -98,7 +73,9 @@ def compile_latex_document(compilation_steps, path_to_tex, path_to_document):


@hookimpl
def pytask_collect_task(session, path, name, obj):
def pytask_collect_task(
session: Session, path: Path, name: str, obj: Any
) -> Task | None:
"""Perform some checks."""
__tracebackhide__ = True

Expand Down Expand Up @@ -130,7 +107,7 @@ def pytask_collect_task(session, path, name, obj):
task = Task(
base_name=name,
path=path,
function=_copy_func(compile_latex_document),
function=_copy_func(compile_latex_document), # type: ignore[arg-type]
depends_on=dependencies,
produces=products,
markers=markers,
Expand All @@ -154,7 +131,7 @@ def pytask_collect_task(session, path, name, obj):

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

return task
return None


def _add_latex_dependencies_retroactively(task, session):
def _add_latex_dependencies_retroactively(task: Task, session: Session) -> Task:
"""Add dependencies from LaTeX document to task.

Unfortunately, the dependencies have to be added retroactively, after the task has
Expand Down Expand Up @@ -291,7 +269,9 @@ def _collect_node(
return collected_node


def _parse_compilation_steps(compilation_steps):
def _parse_compilation_steps(
compilation_steps: str | Callable[..., Any] | Sequence[str | Callable[..., Any]]
) -> list[Callable[..., Any]]:
"""Parse compilation steps."""
__tracebackhide__ = True

Expand All @@ -303,7 +283,7 @@ def _parse_compilation_steps(compilation_steps):
try:
parsed_step = getattr(cs, step)
except AttributeError:
raise ValueError(f"Compilation step {step!r} is unknown.")
raise ValueError(f"Compilation step {step!r} is unknown.") from None
parsed_compilation_steps.append(parsed_step())
elif callable(step):
parsed_compilation_steps.append(step)
Expand Down
11 changes: 9 additions & 2 deletions src/pytask_latex/compilation_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ def compilation_step(path_to_tex: Path, path_to_document: Path):
from __future__ import annotations

import subprocess
from pathlib import Path
from typing import Any
from typing import Callable

from pytask_latex.path import relative_to
from pytask_latex.utils import to_list


def latexmk(options=("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")):
def latexmk(
options: str
| list[str]
| tuple[str, ...] = ("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
) -> Callable[..., Any]:
"""Compilation step that calls latexmk."""
options = [str(i) for i in to_list(options)]

def run_latexmk(path_to_tex, path_to_document):
def run_latexmk(path_to_tex: Path, path_to_document: Path) -> None:
job_name_opt = [f"--jobname={path_to_document.stem}"]
out_dir_opt = [
f"--output-directory={relative_to(path_to_tex, path_to_document.parent)}"
Expand Down
14 changes: 7 additions & 7 deletions src/pytask_latex/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

from pytask import has_mark
from pytask import hookimpl
from pytask import Task


@hookimpl
def pytask_execute_task_setup(task):
def pytask_execute_task_setup(task: Task) -> None:
"""Check that latexmk is found on the PATH if a LaTeX task should be executed."""
if has_mark(task, "latex"):
if shutil.which("latexmk") is None:
raise RuntimeError(
"latexmk is needed to compile LaTeX documents, but it is not found on "
"your PATH."
)
if has_mark(task, "latex") and shutil.which("latexmk") is None:
raise RuntimeError(
"latexmk is needed to compile LaTeX documents, but it is not found on "
"your PATH."
)
9 changes: 5 additions & 4 deletions src/pytask_latex/parametrize.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Parametrize tasks."""
from __future__ import annotations

from typing import Any

import pytask
from pytask import hookimpl


@hookimpl
def pytask_parametrize_kwarg_to_marker(obj, kwargs):
def pytask_parametrize_kwarg_to_marker(obj: Any, kwargs: dict[str, Any]) -> None:
"""Register kwargs as latex marker."""
if callable(obj):
if "latex" in kwargs:
pytask.mark.latex(**kwargs.pop("latex"))(obj)
if callable(obj) and "latex" in kwargs:
pytask.mark.latex(**kwargs.pop("latex"))(obj)
3 changes: 2 additions & 1 deletion src/pytask_latex/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Entry-point for the plugin."""
from __future__ import annotations

from pluggy import PluginManager
from pytask import hookimpl
from pytask_latex import collect
from pytask_latex import config
Expand All @@ -9,7 +10,7 @@


@hookimpl
def pytask_add_hooks(pm):
def pytask_add_hooks(pm: PluginManager) -> None:
"""Register some plugins."""
pm.register(collect)
pm.register(config)
Expand Down
Empty file added src/pytask_latex/py.typed
Empty file.
4 changes: 3 additions & 1 deletion src/pytask_latex/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""This module contains shared functions."""
from __future__ import annotations

from typing import Any
from typing import Sequence


def to_list(scalar_or_iter):
def to_list(scalar_or_iter: Any) -> list[Any]:
"""Convert scalars and iterables to list.

Parameters
Expand Down
Empty file added tests/__init__.py
Empty file.
Loading