pytest-dev / pytest-dev/pytest

pytest failed to parse the filterwarnings option due to subtle issues related to python path

Open
#13,274 5 comments 0 reactions 1 assignee View on GitHub

@harsh21234i is already working on this.

Since Apr 3, 2026.

plugin: warnings topic: collection
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

env

pytest: 7.2.0
system: macOS Sonaoma 14.7.1
python: 3.10.9

Problem description

  1. create a project folder,
    create mod1.py:
import warnings


class MyWarning(UserWarning):
    pass


def useful_func():
    warnings.warn(
        MyWarning("some message"),
        stacklevel=1,
    )
    print("this is useful func")

    return 1

create test_mod1.py under the project

import sys
print(sys.path)

from mod1 import useful_func

def test_useful_func():
    assert 1 == useful_func()
  1. run pytest -s under the project and you see the test pass, but with a warning
  2. create a pyproject.toml under the project directory:
[tool.pytest.ini_options]
filterwarnings = [
"ignore::mod1.MyWarning",
]
  1. run pytest again, you see the following error when pytest is trying to parse the warning filter:
ERROR: while parsing the following warning configuration:

  ignore::mod1.MyWarning

This error occurred:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1690, in parse_warning_filter
    category: Type[Warning] = _resolve_warning_category(category_)
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1728, in _resolve_warning_category
    m = __import__(module, None, None, [klass])
ModuleNotFoundError: No module named 'mod1'

Additional info

I think the error is because when pytest is trying to parse the filterwarnings option, it does not add the project root to the sys.path yet. In a later step of pytest startup, the project root is eventually added to sys.path, which you can see from the step 2 above (when pyproject.toml is not used).

One way to fix the error is to change the warning filter a bit, e.g., to match the message instead:

[tool.pytest.ini_options]
filterwarnings = [
# "ignore::mod1.MyWarning",
"ignore:some message"
]

but this just evade the issue.

Another way is to run pytest via python -m pytest without changing the filter format in pyproject.toml. So I think it is a time issue related to when current directory is added to sys.path.

Not sure if this is intended or really a bug, thanks for reading!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.