From c43e67a98079be29dcb1857939aec9426ae7e3b6 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Tue, 23 Apr 2024 16:38:14 +0200 Subject: [PATCH 1/2] Fix example about capturing warnings in docs. --- docs/source/how_to_guides/capture_warnings.md | 22 ++++++++++++------- .../how_to_guides/capturing_warnings_2.py | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/source/how_to_guides/capture_warnings.md b/docs/source/how_to_guides/capture_warnings.md index 2cd187662..627d59b57 100644 --- a/docs/source/how_to_guides/capture_warnings.md +++ b/docs/source/how_to_guides/capture_warnings.md @@ -21,7 +21,7 @@ The syntax for specifying warnings filters is the same as in the [Python standard library](https://docs.python.org/3/library/warnings.html#the-warnings-filter), i.e., a sequence of fields separated by colons: -``` +```text action:message:category:module:line ``` @@ -49,8 +49,8 @@ the Python documentation and there are also ## `@pytask.mark.filterwarnings` You can use the `@pytask.mark.filterwarnings` to add warning filters to specific test -items, allowing you to have finer control of which warnings should be captured at test, -class or even module level: +items, allowing you to have finer control of which warnings should be captured at the +test, class or even module level: ```{literalinclude} ../../../docs_src/how_to_guides/capturing_warnings_2.py ``` @@ -58,6 +58,12 @@ class or even module level: Filters applied using a mark take precedence over filters passed on the command line or configured by the `filterwarnings` configuration option. +```{important} +Note that the type of warning needs to be importable. For example, `UserWarning` is a +built-in warning with no module specified. `SettingWithCopyWarning` though needs to be +imported from {mod}`pandas.errors`. +``` + ## Disabling warnings summary Although not recommended, you can use the `--disable-warnings` command-line option to @@ -78,16 +84,16 @@ filterwarnings = ["error:.*"] and then run `pytask`. -Or, you use a temporary environment variable. Here is an example for bash +Or, you use a temporary environment variable. Here is an example for bash. ```console -$ PYTHONWARNINGS=error pytask --pdb +PYTHONWARNINGS=error pytask --pdb ``` and here for Powershell ```console -$ $env:PYTHONWARNINGS = 'error' -$ pytask -$ Remove-Item env:\PYTHONWARNINGS +$env:PYTHONWARNINGS = 'error' +pytask +Remove-Item env:\PYTHONWARNINGS ``` diff --git a/docs_src/how_to_guides/capturing_warnings_2.py b/docs_src/how_to_guides/capturing_warnings_2.py index 895dfc26d..6f79083a0 100644 --- a/docs_src/how_to_guides/capturing_warnings_2.py +++ b/docs_src/how_to_guides/capturing_warnings_2.py @@ -12,7 +12,7 @@ def _create_df() -> pd.DataFrame: return df -@pytask.mark.filterwarnings("ignore:.*:SettingWithCopyWarning") +@pytask.mark.filterwarnings("ignore:.*:pandas.errors.SettingWithCopyWarning") def task_warning(path: Annotated[Path, Product] = Path("df.pkl")) -> None: df = _create_df() df.to_pickle(path) From 486afd2d74bfff871e1b620e6fffba6e303190c9 Mon Sep 17 00:00:00 2001 From: Tobias Raabe Date: Tue, 23 Apr 2024 16:40:29 +0200 Subject: [PATCH 2/2] tio changes. --- docs/source/changes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/changes.md b/docs/source/changes.md index 53eeca489..6cb637f9c 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -44,6 +44,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and - {pull}`598` replaces requests with httpx. - {pull}`599` adds a test fixture for switching the cwd. - {pull}`600` refactors test using subprocesses. +- {pull}`603` fixes an example in the documentation about capturing warnings. ## 0.4.7 - 2024-03-19