Skip to content

Cannot ignore internal pytest EncodingWarning warnings #10326

@AA-Turner

Description

@AA-Turner

pytest has several internal EncodingWarnings when running tests with python -X warn_default_encoding -m pytest ...:

=============================== warnings summary ===============================
.tox/du19/lib/python3.10/site-packages/_pytest/capture.py:357
  /home/runner/work/sphinx/sphinx/.tox/du19/lib/python3.10/site-packages/_pytest/capture.py:357: EncodingWarning: 'encoding' argument not specified
    self.tmpfile = open(os.devnull)

.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:199
  /home/runner/work/sphinx/sphinx/.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:199: EncodingWarning: 'encoding' argument not specified
    readme_path.write_text(README_CONTENT)

.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:189
.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:189
  /home/runner/work/sphinx/sphinx/.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:189: EncodingWarning: 'encoding' argument not specified
    f = path.open("w")

.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:160
  /home/runner/work/sphinx/sphinx/.tox/du19/lib/python3.10/site-packages/_pytest/cacheprovider.py:160: EncodingWarning: 'encoding' argument not specified
    with path.open("r") as f:

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

The last three have fixes (see #9910 and #9915, thanks to @bluetech and @peymanslh), though as a general case I tried ignoring the warnings with the below in my pyproject.toml:

[tool.pytest.ini_options]
filterwarnings = [
    "all",
    "ignore::EncodingWarning:_pytest/cacheprovider",
    "ignore::EncodingWarning:_pytest/capture",
]

This failed with the below on Python versions prior to 3.10, as EncodingWarning doesn't exist:

Traceback (most recent call last):
  File "/home/runner/work/sphinx/sphinx/.tox/du14/lib/python3.6/site-packages/_pytest/config/__init__.py", line 1642, in parse_warning_filter
Warning: ory: Type[Warning] = _resolve_warning_category(category_)
  File "/home/runner/work/sphinx/sphinx/.tox/du14/lib/python3.6/site-packages/_pytest/config/__init__.py", line 1681, in _resolve_warning_category
    cat = getattr(m, klass)
AttributeError: module 'builtins' has no attribute 'EncodingWarning'

I have two questions:

  1. Is this considered a bug in pytest?
  2. Is there a way to conditionally filter warnings by Python version?

Please may you also consider resolving the EncodingWarning in _pytest.capture (i.e. open(os.devnull, encoding='utf-8')) and consider enabling EncodingWarning tests in pytest's own tests?

Thanks,
Adam


Test preamble with pytest, Python, versions:

============================= test session starts ==============================
platform linux -- Python 3.10.7, pytest-7.1.3, pluggy-1.0.0 -- /home/runner/work/sphinx/sphinx/.tox/du19/bin/python
cachedir: .tox/du19/.pytest_cache
libraries: Sphinx-5.3.0+/19e0393, docutils-0.19
base tempdir: /tmp/pytest-of-runner/pytest-0
rootdir: /home/runner/work/sphinx/sphinx, configfile: pyproject.toml, testpaths: tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributorplugin: warningsrelated to the warnings builtin plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions