Skip to content

Commit 836518a

Browse files
authored
Deprecate @pytask.mark.parametrize. (#381)
1 parent 53c3c7b commit 836518a

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

docs/source/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
2020
documentation.
2121
- {pull}`368` fixes an error in `update_plugin_list.py` introduced by {pull}`364`.
2222
- {pull}`369` reverts the changes that turn `Node.state()` into a hook.
23+
- {pull}`381` deprecates `@pytask.mark.parametrize`. (Closes {issue}`233`.)
2324

2425
## 0.3.1 - 2023-12-25
2526

src/_pytask/collect.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
import time
9+
import warnings
910
from importlib import util as importlib_util
1011
from pathlib import Path
1112
from typing import Any
@@ -105,6 +106,15 @@ def pytask_collect_file_protocol(
105106
return flat_reports
106107

107108

109+
_PARAMETRIZE_DEPRECATION_WARNING = """\
110+
The @pytask.mark.parametrize decorator is deprecated and will be removed in pytask \
111+
v0.4. Either upgrade your code to the new syntax explained in \
112+
https://tinyurl.com/pytask-loops or silence the warning by setting \
113+
`silence_parametrize_deprecation = true` in your pyproject.toml under \
114+
[tool.pytask.ini_options] and pin pytask to <0.4.
115+
"""
116+
117+
108118
@hookimpl
109119
def pytask_collect_file(
110120
session: Session, path: Path, reports: list[CollectionReport]
@@ -126,6 +136,13 @@ def pytask_collect_file(
126136
continue
127137

128138
if has_mark(obj, "parametrize"):
139+
if not session.config.get("silence_parametrize_deprecation", False):
140+
warnings.warn(
141+
message=_PARAMETRIZE_DEPRECATION_WARNING,
142+
category=FutureWarning,
143+
stacklevel=1,
144+
)
145+
129146
names_and_objects = session.hook.pytask_parametrize_task(
130147
session=session, name=name, obj=obj
131148
)

src/_pytask/warnings_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ def catch_warnings_for_item(
155155
) -> Generator[None, None, None]:
156156
"""Context manager that catches warnings generated in the contained execution block.
157157
158-
``item`` can be None if we are not in the context of an item execution. Each warning
159-
captured triggers the ``pytest_warning_recorded`` hook.
158+
``item`` can be None if we are not in the context of an item execution.
160159
161160
"""
162161
with warnings.catch_warnings(record=True) as log:

tests/test_parametrize.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,40 @@ def task_write_numbers_to_file(i):
492492
session = main({"paths": tmp_path})
493493

494494
assert session.exit_code == ExitCode.OK
495+
496+
497+
@pytest.mark.end_to_end()
498+
def test_deprecation_warning_for_parametrizing_tasks(runner, tmp_path):
499+
source = """
500+
import pytask
501+
502+
@pytask.mark.parametrize('i, produces', [(1, "1.txt"), (2, "2.txt")])
503+
def task_write_numbers_to_file(produces, i):
504+
produces.write_text(str(i))
505+
"""
506+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
507+
508+
result = runner.invoke(cli, [tmp_path.as_posix()])
509+
510+
assert result.exit_code == ExitCode.OK
511+
assert "FutureWarning" in result.output
512+
513+
514+
@pytest.mark.end_to_end()
515+
def test_silence_deprecation_warning_for_parametrizing_tasks(runner, tmp_path):
516+
source = """
517+
import pytask
518+
519+
@pytask.mark.parametrize('i, produces', [(1, "1.txt"), (2, "2.txt")])
520+
def task_write_numbers_to_file(produces, i):
521+
produces.write_text(str(i))
522+
"""
523+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
524+
tmp_path.joinpath("pyproject.toml").write_text(
525+
"[tool.pytask.ini_options]\nsilence_parametrize_deprecation = true"
526+
)
527+
528+
result = runner.invoke(cli, [tmp_path.as_posix()])
529+
530+
assert result.exit_code == ExitCode.OK
531+
assert "FutureWarning" not in result.output

0 commit comments

Comments
 (0)