Skip to content

gh-128770: raise warnings as errors in test suite - except for test_socket which still logs warnings, and internal test warnings that are now logged #128973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
a6bb15e
Reapply "gh-128770: raise warnings as errors in test suite - except f…
graingert Jan 18, 2025
baab6a3
convert internal test warnings into logger calls
graingert Jan 18, 2025
c933487
Apply suggestions from code review
graingert Jan 18, 2025
9e532fa
test that support logs instead of warns
graingert Jan 18, 2025
c06c3db
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
ba36dc0
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
7123093
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
33c84e6
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
104f427
Update test_hashlib.py
graingert Jan 18, 2025
1b44669
Merge branch 'main' into warnings-as-error-2
graingert Jan 19, 2025
36bba4e
Merge branch 'main' into warnings-as-error-2
graingert Jan 20, 2025
b5fc0df
sort logging import
graingert Jan 20, 2025
6bdaad4
Update Lib/test/test_support.py
graingert Jan 20, 2025
3473aab
Update Lib/test/test_support.py
graingert Jan 20, 2025
b49e3f9
Update Lib/test/test_support.py
graingert Jan 20, 2025
b92cbec
Update Lib/test/test_support.py
graingert Jan 20, 2025
fbb99a3
Apply suggestions from code review
graingert Jan 20, 2025
f6909e8
move logging into alphabetical position
graingert Jan 21, 2025
3b895f2
do the hanging indent
graingert Jan 21, 2025
8e17c8a
use indexing rather than unpacking one item
graingert Jan 21, 2025
7ecc3b0
Merge branch 'main' into warnings-as-error-2
graingert Jan 21, 2025
9af76bd
Merge branch 'main' into warnings-as-error-2
graingert Jan 22, 2025
8250358
Merge branch 'main' into warnings-as-error-2
graingert Jan 22, 2025
b1334ea
Merge branch 'main' into warnings-as-error-2
graingert Jan 25, 2025
94fd5b7
Merge branch 'main' into warnings-as-error-2
graingert Jan 25, 2025
1af4d6f
prevent SyntaxError/SyntaxWarning in test_fstring
graingert Jan 25, 2025
122f70d
Merge branch 'main' into warnings-as-error-2
graingert Feb 13, 2025
b44e834
Merge branch 'main' into warnings-as-error-2
graingert Mar 19, 2025
2f29d80
fix SyntaxWarning in test_join_windows.py
graingert Mar 19, 2025
19dc865
fix 'sys.path_hooks is empty' warning in test_permission_error_cwd
graingert Mar 19, 2025
12b6c64
fix meta invalid escape in test_invalid_escape_locations_with_offset
graingert Mar 19, 2025
f8e4f56
fix test_string_literals SyntaxWarning
graingert Mar 19, 2025
b9ecf97
exclude test_grammar.py from test_write_filtered_python_package
graingert Mar 19, 2025
c5e77ba
Merge branch 'main' into warnings-as-error-2
graingert Mar 19, 2025
0305cfe
fix ResourceWarning when writing a unwritable gzipfile
graingert Mar 19, 2025
6dab428
fix ResourceWarnings in test_asyncio.test_events
graingert Mar 19, 2025
6e83d44
prevent another ResourceWarning in GzipFile due to an unclosed myfileobj
graingert Mar 19, 2025
aa35271
extract UNDERSCORE_LITERALS from test_grammar
graingert Mar 19, 2025
6c918b3
pre-commit
graingert Mar 19, 2025
19e6a20
Merge branch 'main' into warnings-as-error-2
graingert Mar 19, 2025
5eae8f4
Merge branch 'main' into warnings-as-error-2
graingert Mar 20, 2025
37900e1
Merge branch 'main' into warnings-as-error-2
graingert Mar 21, 2025
eb01d6b
Merge branch 'main' into warnings-as-error-2
graingert Mar 23, 2025
3d68f6e
mimimize LogCaptureHandler, and reduce pytest citation
graingert Mar 23, 2025
9b438a5
Merge branch 'main' into warnings-as-error-2
graingert Mar 24, 2025
774dd98
Discard changes to Lib/test/test_zipfile/test_core.py
graingert Mar 24, 2025
d1ae6a6
Merge branch 'main' into warnings-as-error-2
graingert Mar 24, 2025
9f92698
Merge branch 'main' into warnings-as-error-2
graingert Mar 25, 2025
78ffa2a
gh-131707: fix unawaited coroutine warning in test_coroutines.Corouti…
graingert Mar 25, 2025
009a6a0
Merge branch 'main' into warnings-as-error-2
graingert Mar 25, 2025
4586652
Merge branch 'main' into warnings-as-error-2
graingert Mar 25, 2025
e0479be
Merge branch 'main' into warnings-as-error-2
graingert Mar 26, 2025
a656a68
Merge branch 'main' into warnings-as-error-2
graingert Mar 26, 2025
f27d4a7
Merge branch 'main' into warnings-as-error-2
graingert Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ):
if not sys.stdout.write_through:
python_opts.append('-u')

# Add warnings filter 'default'
# Add warnings filter 'error'
if 'default' not in sys.warnoptions:
python_opts.extend(('-W', 'default'))
python_opts.extend(('-W', 'error'))

# Error on bytes/str comparison
if sys.flags.bytes_warning < 2:
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import contextlib
import functools
import inspect
import logging
import _opcode
import os
import re
Expand Down Expand Up @@ -404,7 +405,7 @@ def skip_if_buildbot(reason=None):
try:
isbuildbot = getpass.getuser().lower() == 'buildbot'
except (KeyError, OSError) as err:
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
isbuildbot = False
return unittest.skipIf(isbuildbot, reason)

Expand Down Expand Up @@ -1089,8 +1090,7 @@ def start(self):
try:
f = open(self.procfile, 'r')
except OSError as e:
warnings.warn('/proc not available for stats: {}'.format(e),
RuntimeWarning)
logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e)
sys.stderr.flush()
return

Expand Down
80 changes: 80 additions & 0 deletions Lib/test/support/numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# These are shared with test_tokenize and other test modules.
#
# Note: since several test cases filter out floats by looking for "e" and ".",
# don't add hexadecimal literals that contain "e" or "E".
VALID_UNDERSCORE_LITERALS = [
'0_0_0',
'4_2',
'1_0000_0000',
'0b1001_0100',
'0xffff_ffff',
'0o5_7_7',
'1_00_00.5',
'1_00_00.5e5',
'1_00_00e5_1',
'1e1_0',
'.1_4',
'.1_4e1',
'0b_0',
'0x_f',
'0o_5',
'1_00_00j',
'1_00_00.5j',
'1_00_00e5_1j',
'.1_4j',
'(1_2.5+3_3j)',
'(.5_6j)',
]
INVALID_UNDERSCORE_LITERALS = [
# Trailing underscores:
'0_',
'42_',
'1.4j_',
'0x_',
'0b1_',
'0xf_',
'0o5_',
'0 if 1_Else 1',
# Underscores in the base selector:
'0_b0',
'0_xf',
'0_o5',
# Old-style octal, still disallowed:
'0_7',
'09_99',
# Multiple consecutive underscores:
'4_______2',
'0.1__4',
'0.1__4j',
'0b1001__0100',
'0xffff__ffff',
'0x___',
'0o5__77',
'1e1__0',
'1e1__0j',
# Underscore right before a dot:
'1_.4',
'1_.4j',
# Underscore right after a dot:
'1._4',
'1._4j',
'._5',
'._5j',
# Underscore right after a sign:
'1.0e+_1',
'1.0e+_1j',
# Underscore right before j:
'1.4_j',
'1.4e5_j',
# Underscore right before e:
'1_e1',
'1.4_e1',
'1.4_e1j',
# Underscore right after e:
'1e_1',
'1.4e_1',
'1.4e_1j',
# Complex cases with parens:
'(1+1.5_j_)',
'(1+1.5_j)',
]
32 changes: 24 additions & 8 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
import contextlib
import errno
import logging
import os
import re
import stat
Expand Down Expand Up @@ -405,8 +406,12 @@ def _waitfor(func, pathname, waitall=False):
# Increase the timeout and try again
time.sleep(timeout)
timeout *= 2
warnings.warn('tests may fail, delete still pending for ' + pathname,
RuntimeWarning, stacklevel=4)
logging.getLogger(__name__).warning(
'tests may fail, delete still pending for %s',
pathname,
stack_info=True,
stacklevel=4,
)

def _unlink(filename):
_waitfor(os.unlink, filename)
Expand Down Expand Up @@ -521,9 +526,14 @@ def temp_dir(path=None, quiet=False):
except OSError as exc:
if not quiet:
raise
warnings.warn(f'tests may fail, unable to create '
f'temporary directory {path!r}: {exc}',
RuntimeWarning, stacklevel=3)
logging.getLogger(__name__).warning(
"tests may fail, unable to create temporary directory %r: %s",
path,
exc,
exc_info=exc,
stack_info=True,
stacklevel=3,
)
if dir_created:
pid = os.getpid()
try:
Expand Down Expand Up @@ -554,9 +564,15 @@ def change_cwd(path, quiet=False):
except OSError as exc:
if not quiet:
raise
warnings.warn(f'tests may fail, unable to change the current working '
f'directory to {path!r}: {exc}',
RuntimeWarning, stacklevel=3)
logging.getLogger(__name__).warning(
'tests may fail, unable to change the current working directory '
'to %r: %s',
path,
exc,
exc_info=exc,
stack_info=True,
stacklevel=3,
)
try:
yield os.getcwd()
finally:
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import sys
from test import support
from test.support.testcase import ComplexesAreIdenticalMixin
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

from random import random
from math import isnan, copysign
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
with the corresponding argument.
"""

import logging
import math
import os, sys
import operator
Expand Down Expand Up @@ -5955,8 +5956,9 @@ def tearDownModule():
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
P.setcontext(ORIGINAL_CONTEXT[P].copy())
if not C:
warnings.warn('C tests skipped: no module named _decimal.',
UserWarning)
logging.getLogger(__name__).warning(
'C tests skipped: no module named _decimal.'
)
if not orig_sys_decimal is sys.modules['decimal']:
raise TestFailed("Internal error: unbalanced number of changes to "
"sys.modules['decimal'].")
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from test import support
from test.support.testcase import FloatsAreIdenticalMixin
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)
from math import isinf, isnan, copysign, ldexp
import math

Expand Down
86 changes: 4 additions & 82 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,10 @@
import typing
from test.typinganndata import ann_module2
import test

# These are shared with test_tokenize and other test modules.
#
# Note: since several test cases filter out floats by looking for "e" and ".",
# don't add hexadecimal literals that contain "e" or "E".
VALID_UNDERSCORE_LITERALS = [
'0_0_0',
'4_2',
'1_0000_0000',
'0b1001_0100',
'0xffff_ffff',
'0o5_7_7',
'1_00_00.5',
'1_00_00.5e5',
'1_00_00e5_1',
'1e1_0',
'.1_4',
'.1_4e1',
'0b_0',
'0x_f',
'0o_5',
'1_00_00j',
'1_00_00.5j',
'1_00_00e5_1j',
'.1_4j',
'(1_2.5+3_3j)',
'(.5_6j)',
]
INVALID_UNDERSCORE_LITERALS = [
# Trailing underscores:
'0_',
'42_',
'1.4j_',
'0x_',
'0b1_',
'0xf_',
'0o5_',
'0 if 1_Else 1',
# Underscores in the base selector:
'0_b0',
'0_xf',
'0_o5',
# Old-style octal, still disallowed:
'0_7',
'09_99',
# Multiple consecutive underscores:
'4_______2',
'0.1__4',
'0.1__4j',
'0b1001__0100',
'0xffff__ffff',
'0x___',
'0o5__77',
'1e1__0',
'1e1__0j',
# Underscore right before a dot:
'1_.4',
'1_.4j',
# Underscore right after a dot:
'1._4',
'1._4j',
'._5',
'._5j',
# Underscore right after a sign:
'1.0e+_1',
'1.0e+_1j',
# Underscore right before j:
'1.4_j',
'1.4e5_j',
# Underscore right before e:
'1_e1',
'1.4_e1',
'1.4_e1j',
# Underscore right after e:
'1e_1',
'1.4e_1',
'1.4e_1j',
# Complex cases with parens:
'(1+1.5_j_)',
'(1+1.5_j)',
]

from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

class TokenTests(unittest.TestCase):

Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import importlib
import io
import itertools
import logging
import os
import sys
import sysconfig
Expand Down Expand Up @@ -113,7 +114,11 @@ def _conditional_import_module(self, module_name):
return importlib.import_module(module_name)
except ModuleNotFoundError as error:
if self._warn_on_extension_import and module_name in builtin_hashes:
warnings.warn(f'Did a C extension fail to compile? {error}')
logging.getLogger(__name__).warning(
'Did a C extension fail to compile? %s',
error,
exc_info=error,
)
return None

def __init__(self, *args, **kwargs):
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import unittest
from unittest import mock
from test import support
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

try:
import _pylong
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_interpreters/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple
import contextlib
import json
import logging
import os
import os.path
#import select
Expand Down Expand Up @@ -66,8 +67,8 @@ def pack_exception(exc=None):
def unpack_exception(packed):
try:
data = json.loads(packed)
except json.decoder.JSONDecodeError:
warnings.warn('incomplete exception data', RuntimeWarning)
except json.decoder.JSONDecodeError as e:
logging.getLogger(__name__).warning('incomplete exception data', exc_info=e)
print(packed if isinstance(packed, str) else packed.decode('utf-8'))
return None
exc = types.SimpleNamespace(**data)
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_pty.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ def test_openpty(self):
new_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
self.assertEqual(new_dim, target_dim,
"pty.STDIN_FILENO window size unchanged")
except OSError:
warnings.warn("Failed to set pty.STDIN_FILENO window size.")
except OSError as e:
logging.getLogger(__name__).warning(
"Failed to set pty.STDIN_FILENO window size.", exc_info=e,
)
pass

try:
Expand Down
Loading
Loading