Skip to content

gh-162535: Fix setUpClass signatures in test_cmd, test_dtrace and test_dbm_gnu - #162536

Open
tanloong wants to merge 3 commits into
python:mainfrom
tanloong:setupclass
Open

tanloong wants to merge 3 commits into
python:mainfrom
tanloong:setupclass

Conversation

@tanloong

@tanloong tanloong commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adapt test_cmd.CmdTestReadline.setUpClass() following the documented form [0]. unnittest accesses [1] it as a plain function instead of a class method, then calls [2] it without arguments. As a zero-parameter plain function it happens to satisfy that call. The decorator and cls parameter make it more correct.

Adapt test_dbm_gnu.TestGdbm.setUpClass(), which happens to work similarly, from @staticmethod to @classmethod.

Rename self to cls in test_dtrace.TraceTests.setUpClass(self). The class is bound as the first argument anyway. This is a matter of convention and there is no behavior change.

I grepped Lib/test/ and confirm that there is no other setUpClass() nor tearDownClass() that misses @classmethod or use self instead of cls. Didn't include a newsentry as this is a test change.

[0]:

.. method:: setUpClass()
A class method called before tests in an individual class are run.
``setUpClass`` is called with the class as the only argument
and must be decorated as a :deco:`classmethod`::
@classmethod
def setUpClass(cls):
...

[1]:
setUpClass = getattr(currentClass, 'setUpClass', None)

[2]:
setUpClass()

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is just churn, right? There is no functional difference in these cases.

@StanFromIreland StanFromIreland added the pending The issue will be closed if no feedback is provided label Nov 24, 2026
@tanloong

tanloong commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

There was similar change in the past: #133840

@serhiy-storchaka

Copy link
Copy Markdown
Member

It isn't purely cosmetic. unittest calls setUpClass as cls.setUpClass(), so a staticmethod or a bare function happens to work. But code that wraps setUpClass expects a classmethod and breaks on these. For example, the class decorator @support.exclusive() proposed in #156902 uses setUpClass.__func__, which a staticmethod or plain function doesn't have. These two are the only such cases in Lib/test.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

@picnixz picnixz removed the pending The issue will be closed if no feedback is provided label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants