Skip to content

Commit

Permalink
Turn EncodingWarning into errors and cleanup pytest.ini (#4255)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Apr 25, 2024
2 parents cd24907 + 0faba50 commit b1fc698
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions newsfragments/4255.misc.rst
@@ -0,0 +1 @@
Treat ``EncodingWarning``s as errors in tests. -- by :user:`Avasam`
28 changes: 16 additions & 12 deletions pytest.ini
Expand Up @@ -10,22 +10,31 @@ filterwarnings=
# Fail on warnings
error

# Workarounds for pypa/setuptools#3810
# Can't use EncodingWarning as it doesn't exist on Python 3.9.
# These warnings only appear on Python 3.10+

## upstream

# Ensure ResourceWarnings are emitted
default::ResourceWarning

# python/mypy#17057
ignore:'encoding' argument not specified::mypy
ignore:'encoding' argument not specified::configparser
# ^-- ConfigParser is called by mypy,
# but ignoring the warning in `mypy` is not enough
# to make it work on PyPy

# realpython/pytest-mypy#152
ignore:'encoding' argument not specified::pytest_mypy

# python/cpython#100750
ignore:'encoding' argument not specified::platform
# TODO: Set encoding when openning/writing tmpdir files with pytest's LocalPath.open
# see pypa/setuptools#4326
ignore:'encoding' argument not specified::_pytest

# pypa/build#615
ignore:'encoding' argument not specified::build.env

# dateutil/dateutil#1284
ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:dateutil.tz.tz
# Already fixed in pypa/distutils, but present in stdlib
ignore:'encoding' argument not specified::distutils

## end upstream

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

# Workarounds for pypa/setuptools#3810
# Can't use EncodingWarning as it doesn't exist on Python 3.9
default:'encoding' argument not specified
default:UTF-8 Mode affects locale.getpreferredencoding().

# Avoid errors when testing pkg_resources.declare_namespace
ignore:.*pkg_resources\.declare_namespace.*:DeprecationWarning

Expand Down
3 changes: 2 additions & 1 deletion setuptools/command/editable_wheel.py
Expand Up @@ -565,7 +565,8 @@ def _encode_pth(content: str) -> bytes:
This function tries to simulate this behaviour without having to create an
actual file, in a way that supports a range of active Python versions.
(There seems to be some variety in the way different version of Python handle
``encoding=None``, not all of them use ``locale.getpreferredencoding(False)``).
``encoding=None``, not all of them use ``locale.getpreferredencoding(False)``
or ``locale.getencoding()``).
"""
with io.BytesIO() as buffer:
wrapper = io.TextIOWrapper(buffer, encoding=py39.LOCALE_ENCODING)
Expand Down
9 changes: 7 additions & 2 deletions setuptools/tests/__init__.py
@@ -1,10 +1,15 @@
import locale
import sys

import pytest


__all__ = ['fail_on_ascii']


is_ascii = locale.getpreferredencoding() == 'ANSI_X3.4-1968'
locale_encoding = (
locale.getencoding()
if sys.version_info >= (3, 11)
else locale.getpreferredencoding(False)
)
is_ascii = locale_encoding == 'ANSI_X3.4-1968'
fail_on_ascii = pytest.mark.xfail(is_ascii, reason="Test fails in this locale")
2 changes: 1 addition & 1 deletion setuptools/tests/test_build_meta.py
Expand Up @@ -160,7 +160,7 @@ def run():
# to obtain a distribution object first, and then run the distutils
# commands later, because these files will be removed in the meantime.
with open('world.py', 'w') as f:
with open('world.py', 'w', encoding="utf-8") as f:
f.write('x = 42')
try:
Expand Down
13 changes: 13 additions & 0 deletions setuptools/tests/test_build_py.py
@@ -1,6 +1,7 @@
import os
import stat
import shutil
import warnings
from pathlib import Path
from unittest.mock import Mock

Expand Down Expand Up @@ -162,11 +163,23 @@ def test_excluded_subpackages(tmpdir_cwd):
dist.parse_config_files()

build_py = dist.get_command_obj("build_py")

msg = r"Python recognizes 'mypkg\.tests' as an importable package"
with pytest.warns(SetuptoolsDeprecationWarning, match=msg):
# TODO: To fix #3260 we need some transition period to deprecate the
# existing behavior of `include_package_data`. After the transition, we
# should remove the warning and fix the behaviour.

if os.getenv("SETUPTOOLS_USE_DISTUTILS") == "stdlib":
# pytest.warns reset the warning filter temporarily
# https://github.com/pytest-dev/pytest/issues/4011#issuecomment-423494810
warnings.filterwarnings(
"ignore",
"'encoding' argument not specified",
module="distutils.text_file",
# This warning is already fixed in pypa/distutils but not in stdlib
)

build_py.finalize_options()
build_py.run()

Expand Down
14 changes: 12 additions & 2 deletions setuptools/tests/test_windows_wrappers.py
Expand Up @@ -110,7 +110,11 @@ def test_basic(self, tmpdir):
'arg5 a\\\\b',
]
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True
cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
text=True,
encoding="utf-8",
)
stdout, stderr = proc.communicate('hello\nworld\n')
actual = stdout.replace('\r\n', '\n')
Expand Down Expand Up @@ -143,7 +147,11 @@ def test_symlink(self, tmpdir):
'arg5 a\\\\b',
]
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, text=True
cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
text=True,
encoding="utf-8",
)
stdout, stderr = proc.communicate('hello\nworld\n')
actual = stdout.replace('\r\n', '\n')
Expand Down Expand Up @@ -191,6 +199,7 @@ def test_with_options(self, tmpdir):
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
)
stdout, stderr = proc.communicate()
actual = stdout.replace('\r\n', '\n')
Expand Down Expand Up @@ -240,6 +249,7 @@ def test_basic(self, tmpdir):
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
)
stdout, stderr = proc.communicate()
assert not stdout
Expand Down

0 comments on commit b1fc698

Please sign in to comment.