Skip to content

PYTHONWARNDEFAULTENCODING causes unavoidable warnings #1966

Closed
@henryiii

Description

@henryiii

Describe the bug
If you set PYTHONWARNDEFAULTENCODING, which detects missing encoding settings when opening or reading files, coverage seems to spit out warnings that can't easily be skipped. Adding an encoding= (and testing with PYTHONWARNDEFAULTENCODING set) would fix this, and if it was set to utf-8 instead of native, could possibly provide a smoother transition to the version of Python where the default changes.

To Reproduce
How can we reproduce the problem? Please be specific. Don't link to a failing CI job. Answer the questions below:

  1. What version of Python are you using? 3.12 and 3.13
  2. What version of coverage.py shows the problem? The output of coverage debug sys is helpful. All
  3. What versions of what packages do you have installed? The output of pip freeze is helpful. Doesn't matter, checked the file in main.
  4. What code shows the problem? Give us a specific commit of a specific repo that we can check out. If you've already worked around the problem, please provide a commit before that fix. Set PYTHONWARNDEFAULTENCODING
  5. What commands should we run to reproduce the problem? Be specific. Include everything, even git clone, pip install, and so on. Explain like we're five!
git clone https://github.com/pypa/build
tox run -e py313

Expected behavior
No warnings.

Additional context
Add any other context about the problem here.

py313: commands[0]> pytest -ra --cov --cov-config pyproject.toml --cov-report=html:/Users/henryschreiner/git/software/build/.tox/py313/htmlcov --cov-context=test --cov-report=xml:/Users/henryschreiner/git/software/build/.tox/coverage.py313.xml -n auto
======================================================================================================================================================================== test session starts =========================================================================================================================================================================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.5.0
cachedir: .tox/py313/.pytest_cache
installed packages of interest: build==1.2.2.post1 filelock==3.18.0 packaging==25.0 pip==25.1.1 pyproject_hooks==1.2.0 setuptools==80.4.0 virtualenv==20.31.2 wheel==0.45.1
rootdir: /Users/henryschreiner/git/software/build
configfile: pyproject.toml
testpaths: tests
plugins: rerunfailures-15.1, mock-3.14.0, cov-6.1.1, xdist-3.6.1
16 workers [196 items]
..............................................s...................................................................ssssssssssssssssssssssssssssssssssssssssssss..................s...s.............../Users/henryschreiner/git/software/build/.tox/py313/lib/python3.13/site-packages/coverage/html.py:54: EncodingWarning: 'encoding' argument not specified
  with open(data_filename(fname)) as data_file:
/Users/henryschreiner/git/software/build/.tox/py313/lib/python3.13/site-packages/coverage/html.py:709: EncodingWarning: 'encoding' argument not specified
  with open(status_file) as fstatus:
/Users/henryschreiner/git/software/build/.tox/py313/lib/python3.13/site-packages/coverage/html.py:750: EncodingWarning: 'encoding' argument not specified
  with open(status_file, "w") as fout:

I believe you can simply set PYTHONWARNDEFAULTENCODING with tox, but I can't run your tox, it just reports "no module named pip" and dies on every job. (I do have tox-uv installed) If I add requires = tox pip, then I can get it working, I think, trying that., no, doesn't work. If I remove the line that injects a pip install, then it runs, though out of the box I get a lot of errors like "coverage: command not found". Ah, got it to pass, had to set up a venv with the dev deps in it. Why can't tox work out of the box? I thought that was the point of using a test runner? Ouch, I see setup.py <commands> in the run. Anyway, now I can test and see if adding the env var shows warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions