fix: add encoding arguments#1967
Merged
nedbat merged 4 commits intocoveragepy:masterfrom May 16, 2025
Merged
Conversation
henryiii
commented
May 14, 2025
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
d096fe4 to
2bfdb5f
Compare
Member
|
Thanks for starting this. It's turning out to be a real puzzle how to silence them all... I'm trying to tweak this to get it right. |
Contributor
Author
|
I can try to follow up, but traveling for PyCon is getting in the way. :) I think we just have to fix 3.9 and 3.10. |
Member
|
This gets us closer: diff --git a/tests/helpers.py b/tests/helpers.py
index b53ee53a..27d757da 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -43,12 +43,14 @@ def run_command(cmd: str) -> tuple[int, str]:
# Subprocesses are expensive, but convenient, and so may be over-used in
# the test suite. Use these lines to get a list of the tests using them:
if 0: # pragma: debugging
- pth = "/tmp/processes.txt"
- with open(pth, "a", encoding="utf-8") as proctxt: # type: ignore[unreachable]
+ pth = "/tmp/processes.txt" # type: ignore[unreachable]
+ with open(pth, "a", encoding="utf-8") as proctxt:
print(os.getenv("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True)
encoding = os.device_encoding(1) or (
- locale.getdefaultencoding() if sys.version_info < (3, 10) else locale.getencoding()
+ locale.getpreferredencoding()
+ if sys.version_info < (3, 11)
+ else locale.getencoding() # type: ignore
)
# In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of
diff --git a/tox.ini b/tox.ini
index e0c5dbee..dbc0f1f9 100644
--- a/tox.ini
+++ b/tox.ini
@@ -28,7 +28,14 @@ setenv =
pypy3{,9,10,11}: COVERAGE_TEST_CORES=pytrace
# If we ever need a stronger way to suppress warnings:
#PYTHONWARNINGS=ignore:removed in Python 3.14; use ast.Constant:DeprecationWarning
- PYTHONWARNDEFAULTENCODING=1
+ # We want to know about missing encoding arguments, but we need to silence
+ # some warnings that aren't ours. We can't silence them in 3.9 because
+ # EncodingWarning doesn't exist yet, and it's hard to suppress them in some
+ # environments and not others. So by default, don't warn, and will enable
+ # the warning in a handful of environments to catch the problems.
+ PYTHONWARNDEFAULTENCODING=
+ py3{10,11,12,13,14}: PYTHONWARNDEFAULTENCODING=1
+ py3{10,11,12,13,14}: PYTHONWARNINGS=ignore::EncodingWarning:pip._internal.utils.subprocess
# Disable CPython's color output
PYTHON_COLORS=0 |
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
henryiii
commented
May 16, 2025
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Member
|
Thanks! I have one more warning to suppress that I'll do on master after merging this. |
Member
|
One more tweak is in commit 6999a4e. |
Member
|
This is now released as part of coverage 7.8.1. |
Contributor
Author
|
Thanks! I can verify build's test suite now passes without warnings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes #1966 by adding
encoding="utf-8"everywhere. See https://peps.python.org/pep-0597/.