From 08bed631bcf42b4c1cd13bed8c3ee4c956706acb Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sat, 13 May 2023 15:13:09 +0200 Subject: [PATCH 1/4] Deprecate @pytask.mark.parametrize. --- src/_pytask/collect.py | 17 ++++++++++++++++ src/_pytask/warnings_utils.py | 3 +-- tests/test_parametrize.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/_pytask/collect.py b/src/_pytask/collect.py index 58d481459..fd9471fd0 100644 --- a/src/_pytask/collect.py +++ b/src/_pytask/collect.py @@ -6,6 +6,7 @@ import os import sys import time +import warnings from importlib import util as importlib_util from pathlib import Path from typing import Any @@ -105,6 +106,15 @@ def pytask_collect_file_protocol( return flat_reports +_PARAMETRIZE_DEPRECATION_WARNING = """\ +The @pytask.mark.parametrize decorator is deprecated and will be removed in pytask \ +v0.4. Either upgrade your code to the new syntax explained in \ +https://tinyurl.com/pytask-loops or silence the warning by setting \ +`silence_parametrize_deprecation = true` in your pyproject.toml under \ +[tool.pytask.ini_options] and pin pytask to <0.4. +""" + + @hookimpl def pytask_collect_file( session: Session, path: Path, reports: list[CollectionReport] @@ -126,6 +136,13 @@ def pytask_collect_file( continue if has_mark(obj, "parametrize"): + if not session.config.get("silence_parametrize_deprecation", False): + warnings.warn( + message=_PARAMETRIZE_DEPRECATION_WARNING, + category=FutureWarning, + stacklevel=2, + ) + names_and_objects = session.hook.pytask_parametrize_task( session=session, name=name, obj=obj ) diff --git a/src/_pytask/warnings_utils.py b/src/_pytask/warnings_utils.py index bb1612aff..45a9e2862 100644 --- a/src/_pytask/warnings_utils.py +++ b/src/_pytask/warnings_utils.py @@ -155,8 +155,7 @@ def catch_warnings_for_item( ) -> Generator[None, None, None]: """Context manager that catches warnings generated in the contained execution block. - ``item`` can be None if we are not in the context of an item execution. Each warning - captured triggers the ``pytest_warning_recorded`` hook. + ``item`` can be None if we are not in the context of an item execution. """ with warnings.catch_warnings(record=True) as log: diff --git a/tests/test_parametrize.py b/tests/test_parametrize.py index 763f08f7f..2566ea61c 100644 --- a/tests/test_parametrize.py +++ b/tests/test_parametrize.py @@ -492,3 +492,40 @@ def task_write_numbers_to_file(i): session = main({"paths": tmp_path}) assert session.exit_code == ExitCode.OK + + +@pytest.mark.end_to_end() +def test_deprecation_warning_for_parametrizing_tasks(runner, tmp_path): + source = """ + import pytask + + @pytask.mark.parametrize('i, produces', [(1, "1.txt"), (2, "2.txt")]) + def task_write_numbers_to_file(produces, i): + produces.write_text(str(i)) + """ + tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source)) + + result = runner.invoke(cli, [tmp_path.as_posix()]) + + assert result.exit_code == ExitCode.OK + assert "The @pytask.mark.parametrize decorator" in result.output + + +@pytest.mark.end_to_end() +def test_silence_deprecation_warning_for_parametrizing_tasks(runner, tmp_path): + source = """ + import pytask + + @pytask.mark.parametrize('i, produces', [(1, "1.txt"), (2, "2.txt")]) + def task_write_numbers_to_file(produces, i): + produces.write_text(str(i)) + """ + tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source)) + tmp_path.joinpath("pyproject.toml").write_text( + "[tool.pytask.ini_options]\nsilence_parametrize_deprecation = true" + ) + + result = runner.invoke(cli, [tmp_path.as_posix()]) + + assert result.exit_code == ExitCode.OK + assert "The @pytask.mark.parametrize decorator is deprecated" not in result.output From 9740a79e0a91c269810b333c6edf029569a23741 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sat, 13 May 2023 15:15:52 +0200 Subject: [PATCH 2/4] to changes. --- docs/source/changes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/changes.md b/docs/source/changes.md index 5a79980a0..77a3cbb8e 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -20,6 +20,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and documentation. - {pull}`368` fixes an error in `update_plugin_list.py` introduced by {pull}`364`. - {pull}`369` reverts the changes that turn `Node.state()` into a hook. +- {pull}`381` deprecates `@pytask.mark.parametrize`. (Closes {issue}`233`.) ## 0.3.1 - 2023-12-25 From 91d65a0687373008aa5fad2b3651c335df70a5b6 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sat, 13 May 2023 15:25:55 +0200 Subject: [PATCH 3/4] FIx. --- tests/test_parametrize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_parametrize.py b/tests/test_parametrize.py index 2566ea61c..37994a8b5 100644 --- a/tests/test_parametrize.py +++ b/tests/test_parametrize.py @@ -508,7 +508,7 @@ def task_write_numbers_to_file(produces, i): result = runner.invoke(cli, [tmp_path.as_posix()]) assert result.exit_code == ExitCode.OK - assert "The @pytask.mark.parametrize decorator" in result.output + assert "FutureWarning" in result.output @pytest.mark.end_to_end() @@ -528,4 +528,4 @@ def task_write_numbers_to_file(produces, i): result = runner.invoke(cli, [tmp_path.as_posix()]) assert result.exit_code == ExitCode.OK - assert "The @pytask.mark.parametrize decorator is deprecated" not in result.output + assert "FutureWarning" not in result.output From 672629cb13923fd773e16de564e377b6a6319712 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Sun, 21 May 2023 23:02:50 +0200 Subject: [PATCH 4/4] last fixes. --- src/_pytask/collect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytask/collect.py b/src/_pytask/collect.py index fd9471fd0..f71cf80e0 100644 --- a/src/_pytask/collect.py +++ b/src/_pytask/collect.py @@ -140,7 +140,7 @@ def pytask_collect_file( warnings.warn( message=_PARAMETRIZE_DEPRECATION_WARNING, category=FutureWarning, - stacklevel=2, + stacklevel=1, ) names_and_objects = session.hook.pytask_parametrize_task(