Skip to content

Commit bec6d2e

Browse files
authored
Do not capture DeprecationWarning and PendingDeprecationWarning by default. (#277)
1 parent e60c8a0 commit bec6d2e

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

docs/source/changes.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
88
## 0.2.3 - 2022-xx-xx
99

1010
- {pull}`276` fixes `pytask clean` when git is not installed. Fixes {issue}`275`.
11+
- {pull}`277` ignores `DeprecationWarning` and `PendingDeprecationWarning` by default.
12+
Previously, they were enabled, but they should be shown when testing the project with
13+
pytest, not after the execution with pytask. Fixes {issue}`269`.
1114

1215
## 0.2.2 - 2022-05-14
1316

docs/source/how_to_guides/capture_warnings.md

-22
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,6 @@ configured by the `filterwarnings` configuration option.
7878
Although not recommended, you can use the `--disable-warnings` command-line option to
7979
suppress the warning summary entirely from the test run output.
8080

81-
## `DeprecationWarning` and `PendingDeprecationWarning`
82-
83-
By default pytask will display `DeprecationWarning` and `PendingDeprecationWarning`
84-
warnings from user code and third-party libraries. This helps users keep their code
85-
modern and avoid breakages when deprecated warnings are effectively removed.
86-
87-
Sometimes it is useful to hide some specific deprecation warnings that happen in code
88-
that you have no control over (such as third-party libraries), in which case you might
89-
use the warning filters options (ini or marks) to ignore those warnings.
90-
91-
For example:
92-
93-
```toml
94-
[tool.pytask.ini_options]
95-
filterwarnings = [
96-
"ignore:.*U.*mode is deprecated:DeprecationWarning"
97-
]
98-
```
99-
100-
This will ignore all warnings of type `DeprecationWarning` where the start of the
101-
message matches the regular expression `".*U.*mode is deprecated"`.
102-
10381
## Debugging warnings
10482

10583
Sometimes it is not clear which line of code triggered a warning. To find the location,

src/_pytask/collect_command.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def _find_common_ancestor_of_all_nodes(
126126
for task in tasks:
127127
all_paths.append(task.path)
128128
if show_nodes:
129-
all_paths.extend(map(lambda x: x.path, tree_just_flatten(task.depends_on)))
130-
all_paths.extend(map(lambda x: x.path, tree_just_flatten(task.produces)))
129+
all_paths.extend(x.path for x in tree_just_flatten(task.depends_on))
130+
all_paths.extend(x.path for x in tree_just_flatten(task.produces))
131131

132132
common_ancestor = find_common_ancestor(*all_paths, *paths)
133133

src/_pytask/warnings.py

-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import functools
55
import re
6-
import sys
76
import textwrap
87
import warnings
98
from collections import defaultdict
@@ -90,12 +89,6 @@ def catch_warnings_for_item(
9089
# mypy can't infer that record=True means log is not None; help it.
9190
assert log is not None
9291

93-
if not sys.warnoptions:
94-
# If user is not explicitly configuring warning filters, show deprecation
95-
# warnings by default (#2908).
96-
warnings.filterwarnings("always", category=DeprecationWarning)
97-
warnings.filterwarnings("always", category=PendingDeprecationWarning)
98-
9992
for arg in session.config["filterwarnings"]:
10093
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
10194

tests/test_warnings.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import subprocess
34
import textwrap
45

56
import pytest
@@ -119,3 +120,38 @@ def task_example():
119120
assert result.exit_code == ExitCode.OK
120121
assert ("Warnings" in result.output) is not add_config
121122
assert ("warning!!!" in result.output) is not add_config
123+
124+
125+
@pytest.mark.parametrize("warning", ["DeprecationWarning", "PendingDeprecationWarning"])
126+
def test_deprecation_warnings_are_not_captured(tmp_path, warning):
127+
path_to_warn_module = tmp_path.joinpath("warning.py")
128+
source = """
129+
import importlib.util
130+
import sys
131+
from pathlib import Path
132+
133+
def task_example():
134+
spec = importlib.util.spec_from_file_location(
135+
"warning", Path(__file__).parent / "warning.py"
136+
)
137+
warning_module = importlib.util.module_from_spec(spec)
138+
sys.modules["warning"] = warning_module
139+
spec.loader.exec_module(warning_module)
140+
warning_module.warn_now()
141+
"""
142+
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
143+
144+
warn_module = f"""
145+
import warnings
146+
147+
def warn_now():
148+
warnings.warn("warning!!!", {warning})
149+
"""
150+
path_to_warn_module.write_text(textwrap.dedent(warn_module))
151+
152+
# Cannot use runner since then warnings are not ignored by default.
153+
result = subprocess.run(("pytask"), cwd=tmp_path, capture_output=True)
154+
155+
assert result.returncode == ExitCode.OK
156+
assert "Warnings" not in result.stdout.decode()
157+
assert "warning!!!" not in result.stdout.decode()

0 commit comments

Comments
 (0)