Skip to content

Commit b1fc698

Browse files
authored
Turn EncodingWarning into errors and cleanup pytest.ini (#4255)
2 parents cd24907 + 0faba50 commit b1fc698

File tree

7 files changed

+52
-18
lines changed

7 files changed

+52
-18
lines changed

newsfragments/4255.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Treat ``EncodingWarning``s as errors in tests. -- by :user:`Avasam`

pytest.ini

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,31 @@ filterwarnings=
1010
# Fail on warnings
1111
error
1212

13+
# Workarounds for pypa/setuptools#3810
14+
# Can't use EncodingWarning as it doesn't exist on Python 3.9.
15+
# These warnings only appear on Python 3.10+
16+
1317
## upstream
1418

1519
# Ensure ResourceWarnings are emitted
1620
default::ResourceWarning
1721

22+
# python/mypy#17057
23+
ignore:'encoding' argument not specified::mypy
24+
ignore:'encoding' argument not specified::configparser
25+
# ^-- ConfigParser is called by mypy,
26+
# but ignoring the warning in `mypy` is not enough
27+
# to make it work on PyPy
28+
1829
# realpython/pytest-mypy#152
1930
ignore:'encoding' argument not specified::pytest_mypy
2031

21-
# python/cpython#100750
22-
ignore:'encoding' argument not specified::platform
32+
# TODO: Set encoding when openning/writing tmpdir files with pytest's LocalPath.open
33+
# see pypa/setuptools#4326
34+
ignore:'encoding' argument not specified::_pytest
2335

24-
# pypa/build#615
25-
ignore:'encoding' argument not specified::build.env
26-
27-
# dateutil/dateutil#1284
28-
ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:dateutil.tz.tz
36+
# Already fixed in pypa/distutils, but present in stdlib
37+
ignore:'encoding' argument not specified::distutils
2938

3039
## end upstream
3140

@@ -69,11 +78,6 @@ filterwarnings=
6978
# https://github.com/pypa/setuptools/issues/3655
7079
ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning
7180

72-
# Workarounds for pypa/setuptools#3810
73-
# Can't use EncodingWarning as it doesn't exist on Python 3.9
74-
default:'encoding' argument not specified
75-
default:UTF-8 Mode affects locale.getpreferredencoding().
76-
7781
# Avoid errors when testing pkg_resources.declare_namespace
7882
ignore:.*pkg_resources\.declare_namespace.*:DeprecationWarning
7983

setuptools/command/editable_wheel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ def _encode_pth(content: str) -> bytes:
565565
This function tries to simulate this behaviour without having to create an
566566
actual file, in a way that supports a range of active Python versions.
567567
(There seems to be some variety in the way different version of Python handle
568-
``encoding=None``, not all of them use ``locale.getpreferredencoding(False)``).
568+
``encoding=None``, not all of them use ``locale.getpreferredencoding(False)``
569+
or ``locale.getencoding()``).
569570
"""
570571
with io.BytesIO() as buffer:
571572
wrapper = io.TextIOWrapper(buffer, encoding=py39.LOCALE_ENCODING)

setuptools/tests/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import locale
2+
import sys
23

34
import pytest
45

56

67
__all__ = ['fail_on_ascii']
78

8-
9-
is_ascii = locale.getpreferredencoding() == 'ANSI_X3.4-1968'
9+
locale_encoding = (
10+
locale.getencoding()
11+
if sys.version_info >= (3, 11)
12+
else locale.getpreferredencoding(False)
13+
)
14+
is_ascii = locale_encoding == 'ANSI_X3.4-1968'
1015
fail_on_ascii = pytest.mark.xfail(is_ascii, reason="Test fails in this locale")

setuptools/tests/test_build_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def run():
160160
# to obtain a distribution object first, and then run the distutils
161161
# commands later, because these files will be removed in the meantime.
162162
163-
with open('world.py', 'w') as f:
163+
with open('world.py', 'w', encoding="utf-8") as f:
164164
f.write('x = 42')
165165
166166
try:

setuptools/tests/test_build_py.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import stat
33
import shutil
4+
import warnings
45
from pathlib import Path
56
from unittest.mock import Mock
67

@@ -162,11 +163,23 @@ def test_excluded_subpackages(tmpdir_cwd):
162163
dist.parse_config_files()
163164

164165
build_py = dist.get_command_obj("build_py")
166+
165167
msg = r"Python recognizes 'mypkg\.tests' as an importable package"
166168
with pytest.warns(SetuptoolsDeprecationWarning, match=msg):
167169
# TODO: To fix #3260 we need some transition period to deprecate the
168170
# existing behavior of `include_package_data`. After the transition, we
169171
# should remove the warning and fix the behaviour.
172+
173+
if os.getenv("SETUPTOOLS_USE_DISTUTILS") == "stdlib":
174+
# pytest.warns reset the warning filter temporarily
175+
# https://github.com/pytest-dev/pytest/issues/4011#issuecomment-423494810
176+
warnings.filterwarnings(
177+
"ignore",
178+
"'encoding' argument not specified",
179+
module="distutils.text_file",
180+
# This warning is already fixed in pypa/distutils but not in stdlib
181+
)
182+
170183
build_py.finalize_options()
171184
build_py.run()
172185

setuptools/tests/test_windows_wrappers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ def test_basic(self, tmpdir):
110110
'arg5 a\\\\b',
111111
]
112112
proc = subprocess.Popen(
113-
cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True
113+
cmd,
114+
stdout=subprocess.PIPE,
115+
stdin=subprocess.PIPE,
116+
text=True,
117+
encoding="utf-8",
114118
)
115119
stdout, stderr = proc.communicate('hello\nworld\n')
116120
actual = stdout.replace('\r\n', '\n')
@@ -143,7 +147,11 @@ def test_symlink(self, tmpdir):
143147
'arg5 a\\\\b',
144148
]
145149
proc = subprocess.Popen(
146-
cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True
150+
cmd,
151+
stdout=subprocess.PIPE,
152+
stdin=subprocess.PIPE,
153+
text=True,
154+
encoding="utf-8",
147155
)
148156
stdout, stderr = proc.communicate('hello\nworld\n')
149157
actual = stdout.replace('\r\n', '\n')
@@ -191,6 +199,7 @@ def test_with_options(self, tmpdir):
191199
stdin=subprocess.PIPE,
192200
stderr=subprocess.STDOUT,
193201
text=True,
202+
encoding="utf-8",
194203
)
195204
stdout, stderr = proc.communicate()
196205
actual = stdout.replace('\r\n', '\n')
@@ -240,6 +249,7 @@ def test_basic(self, tmpdir):
240249
stdin=subprocess.PIPE,
241250
stderr=subprocess.STDOUT,
242251
text=True,
252+
encoding="utf-8",
243253
)
244254
stdout, stderr = proc.communicate()
245255
assert not stdout

0 commit comments

Comments
 (0)