Skip to content

gh-135120: Add test.support.subTests() #135121

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jun 4, 2025

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This looks very useful :)

Comment on lines 80 to 100
def _parameterize(*parameters):
"""Simplistic decorator to parametrize a test

Runs the decorated test multiple times in subTest, with a value from
'parameters' passed as an extra positional argument.
Calls doCleanups() after each run.

Not for general use. Intended to avoid indenting for easier backports.

See https://discuss.python.org/t/91827 for discussing generalizations.
"""
def _parametrize_decorator(func):
def _parameterized(self, *args, **kwargs):
for parameter in parameters:
with self.subTest(parameter):
func(self, *args, parameter, **kwargs)
self.doCleanups()
return _parameterized
return _parametrize_decorator


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For test_ntpath and test_posixpath, could you leave _parameterize in for now, making it an adapter for support.subTests?

This was added recently, in a security change that was backported everywhere and received limited review. I think it would be good to try keeping the tests stable (textually) for a while (a few months?), to make backports easier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@functools.wraps(func)
def wrapper(self, /, *args, **kwargs):
for values in arg_values:
if len(arg_names) == 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change the logic to if the original arg_names was a string without a comma.

That is, I'd expect subTests(['number'], ([1], [2]) to set number to 1 and 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"""Run multiple subtests with different parameters.
"""
if isinstance(arg_names, str):
arg_names = arg_names.split(',')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also strip() the names? We should allow putting space after a comma.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I initially used an idiom from namedtuple, but then decided that comma-only is enough for initial implementation in test.support.

"""
if isinstance(arg_names, str):
arg_names = arg_names.split(',')
def decorator(func):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current PR, if you use a generator for arg_values, a second run of the test will silently do nothing ☠️

Suggested change
def decorator(func):
arg_values = tuple(arg_values)
def decorator(func):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@serhiy-storchaka serhiy-storchaka force-pushed the test-support-subtests branch from 16eeaf5 to 8b3ede1 Compare June 5, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants