Skip to content

Commit a7d5230

Browse files
authored
Add types to the package. (#31)
1 parent 49a58b5 commit a7d5230

File tree

10 files changed

+77
-19
lines changed

10 files changed

+77
-19
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ repos:
7474
rev: v2.1.0
7575
hooks:
7676
- id: codespell
77+
- repo: https://github.com/pre-commit/mirrors-mypy
78+
rev: 'v0.930'
79+
hooks:
80+
- id: mypy
81+
args: [
82+
--no-strict-optional,
83+
--ignore-missing-imports,
84+
]
85+
additional_dependencies: [
86+
types-attrs,
87+
types-click,
88+
types-setuptools
89+
]
90+
pass_filenames: false
91+
language_version: "3.9"
7792
- repo: https://github.com/mgedmin/check-manifest
7893
rev: "0.48"
7994
hooks:

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ chronological order. Releases follow [semantic versioning](https://semver.org/)
55
releases are available on [PyPI](https://pypi.org/project/pytask-parallel) and
66
[Anaconda.org](https://anaconda.org/conda-forge/pytask-parallel).
77

8-
## 0.1.2 - 2022-xx-xx
8+
## 0.2.0 - 2022-xx-xx
99

10+
- {pull}`31` adds types to the package.
1011
- {pull}`36` adds a test for <https://github.com/pytask-dev/pytask/issues/216>.
12+
- {pull}`37` aligns pytask-parallel with pytask v0.2.
1113

1214
## 0.1.1 - 2022-02-08
1315

pyproject.toml

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

66
[tool.setuptools_scm]
77
write_to = "src/pytask_parallel/_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

src/pytask_parallel/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
@hookimpl
11-
def pytask_extend_command_line_interface(cli):
11+
def pytask_extend_command_line_interface(cli: click.Group) -> None:
1212
"""Extend the command line interface."""
1313
additional_parameters = [
1414
click.Option(

src/pytask_parallel/callbacks.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Validate command line inputs and configuration values."""
22
from __future__ import annotations
33

4+
from typing import Any
5+
46
from pytask_parallel.backends import PARALLEL_BACKENDS
57

68

7-
def n_workers_callback(value):
9+
def n_workers_callback(value: Any) -> int:
810
"""Validate the n-workers option."""
911
if value == "auto":
1012
pass
@@ -20,7 +22,7 @@ def n_workers_callback(value):
2022
return value
2123

2224

23-
def parallel_backend_callback(value):
25+
def parallel_backend_callback(value: Any) -> str | None:
2426
"""Validate the input for the parallel backend."""
2527
if value in [None, "None", "none"]:
2628
value = None
@@ -33,7 +35,7 @@ def parallel_backend_callback(value):
3335
return value
3436

3537

36-
def delay_callback(value):
38+
def delay_callback(value: Any) -> float | None:
3739
"""Validate the delay option."""
3840
if value in [None, "None", "none"]:
3941
value = None

src/pytask_parallel/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414

1515
@hookimpl
16-
def pytask_parse_config(config, config_from_cli, config_from_file):
16+
def pytask_parse_config(
17+
config: dict[str, Any],
18+
config_from_cli: dict[str, Any],
19+
config_from_file: dict[str, Any],
20+
) -> None:
1721
"""Parse the configuration."""
1822
config["n_workers"] = _get_first_non_none_value(
1923
config_from_cli,
@@ -43,7 +47,7 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
4347

4448

4549
@hookimpl
46-
def pytask_post_parse(config):
50+
def pytask_post_parse(config: dict[str, Any]) -> None:
4751
"""Disable parallelization if debugging is enabled."""
4852
if config["pdb"] or config["trace"]:
4953
config["n_workers"] = 1

src/pytask_parallel/execute.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import inspect
55
import sys
66
import time
7+
from concurrent.futures import Future
8+
from types import TracebackType
79
from typing import Any
810

911
import cloudpickle
@@ -12,13 +14,15 @@
1214
from pytask import ExecutionReport
1315
from pytask import hookimpl
1416
from pytask import remove_internal_traceback_frames_from_exc_info
17+
from pytask import Session
18+
from pytask import Task
1519
from pytask_parallel.backends import PARALLEL_BACKENDS
1620
from rich.console import ConsoleOptions
1721
from rich.traceback import Traceback
1822

1923

2024
@hookimpl
21-
def pytask_post_parse(config):
25+
def pytask_post_parse(config: dict[str, Any]) -> None:
2226
"""Register the parallel backend."""
2327
if config["parallel_backend"] in ("loky", "processes"):
2428
config["pm"].register(ProcessesNameSpace)
@@ -27,7 +31,7 @@ def pytask_post_parse(config):
2731

2832

2933
@hookimpl(tryfirst=True)
30-
def pytask_execute_build(session):
34+
def pytask_execute_build(session: Session) -> bool | None:
3135
"""Execute tasks with a parallel backend.
3236
3337
There are three phases while the scheduler has tasks which need to be executed.
@@ -40,7 +44,7 @@ def pytask_execute_build(session):
4044
"""
4145
if session.config["n_workers"] > 1:
4246
reports = session.execution_reports
43-
running_tasks = {}
47+
running_tasks: dict[str, Future[Any]] = {}
4448

4549
parallel_backend = PARALLEL_BACKENDS[session.config["parallel_backend"]]
4650

@@ -137,13 +141,15 @@ def pytask_execute_build(session):
137141
break
138142

139143
return True
144+
return None
140145

141146

142147
class ProcessesNameSpace:
143148
"""The name space for hooks related to processes."""
144149

150+
@staticmethod
145151
@hookimpl(tryfirst=True)
146-
def pytask_execute_task(session, task): # noqa: N805
152+
def pytask_execute_task(session: Session, task: Task) -> Future[Any] | None:
147153
"""Execute a task.
148154
149155
Take a task, pickle it and send the bytes over to another process.
@@ -162,11 +168,15 @@ def pytask_execute_task(session, task): # noqa: N805
162168
show_locals=session.config["show_locals"],
163169
console_options=console.options,
164170
)
171+
return None
165172

166173

167174
def _unserialize_and_execute_task(
168-
bytes_function, bytes_kwargs, show_locals, console_options
169-
):
175+
bytes_function: bytes,
176+
bytes_kwargs: bytes,
177+
show_locals: bool,
178+
console_options: ConsoleOptions,
179+
) -> tuple[type[BaseException], BaseException, str] | None:
170180
"""Unserialize and execute task.
171181
172182
This function receives bytes and unpickles them to a task which is them execute
@@ -184,11 +194,14 @@ def _unserialize_and_execute_task(
184194
exc_info = sys.exc_info()
185195
processed_exc_info = _process_exception(exc_info, show_locals, console_options)
186196
return processed_exc_info
197+
return None
187198

188199

189200
def _process_exception(
190-
exc_info: tuple[Any], show_locals: bool, console_options: ConsoleOptions
191-
) -> tuple[Any]:
201+
exc_info: tuple[type[BaseException], BaseException, TracebackType | None],
202+
show_locals: bool,
203+
console_options: ConsoleOptions,
204+
) -> tuple[type[BaseException], BaseException, str]:
192205
"""Process the exception and convert the traceback to a string."""
193206
exc_info = remove_internal_traceback_frames_from_exc_info(exc_info)
194207
traceback = Traceback.from_exception(*exc_info, show_locals=show_locals)
@@ -200,8 +213,9 @@ def _process_exception(
200213
class DefaultBackendNameSpace:
201214
"""The name space for hooks related to threads."""
202215

216+
@staticmethod
203217
@hookimpl(tryfirst=True)
204-
def pytask_execute_task(session, task): # noqa: N805
218+
def pytask_execute_task(session: Session, task: Task) -> Future[Any] | None:
205219
"""Execute a task.
206220
207221
Since threads have shared memory, it is not necessary to pickle and unpickle the
@@ -211,9 +225,11 @@ def pytask_execute_task(session, task): # noqa: N805
211225
if session.config["n_workers"] > 1:
212226
kwargs = _create_kwargs_for_task(task)
213227
return session.executor.submit(task.execute, **kwargs)
228+
else:
229+
return None
214230

215231

216-
def _create_kwargs_for_task(task):
232+
def _create_kwargs_for_task(task: Task) -> dict[Any, Any]:
217233
"""Create kwargs for task function."""
218234
kwargs = {**task.kwargs}
219235

src/pytask_parallel/logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
from pytask import console
55
from pytask import hookimpl
6+
from pytask import Session
67

78

89
@hookimpl(trylast=True)
9-
def pytask_log_session_header(session):
10+
def pytask_log_session_header(session: Session) -> None:
1011
"""Add a note for how many workers are spawned."""
1112
n_workers = session.config["n_workers"]
1213
if n_workers > 1:

src/pytask_parallel/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_parallel import build
67
from pytask_parallel 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 plugins."""
1415
pm.register(build)
1516
pm.register(config)

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)