This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: @staticmethod seems to work with setUpClass, but docs say it shouldn't
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Peter de Blanc, brett.cannon, ezio.melotti, michael.foord, rbcollins, xtreak
Priority: normal Keywords:

Created on 2019-04-09 05:40 by Peter de Blanc, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test_bar.py Peter de Blanc, 2019-04-09 05:40 Test case to be run with python interpreter or pytest
Messages (3)
msg339700 - (view) Author: Peter de Blanc (Peter de Blanc) Date: 2019-04-09 05:40
According to unittest docs: https://docs.python.org/3.7/library/unittest.html#module-unittest

`setUpClass is called with the class as the only argument and must be decorated as a classmethod()`

and:

`tearDownClass is called with the class as the only argument and must be decorated as a classmethod()`

However, I was able to create a passing test case where `setUpClass` and `tearDownClass` are decorated with `@staticmethod` instead of `@classmethod`:

I tested this with Python versions 3.6.4 and 3.7.1.

Please update the documentation to indicate that `@staticmethod` is allowed here, or else indicate why it's bad.
msg339705 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-04-09 06:14
The use case of setUpClass and tearDownClass is to run once during class setup and teardown where a class variable might be mutated. It's not a strict requirement [0] but rather maybe a common use case. There is at least one test case where setUpClass is used with @staticmethod at [1]

[0] https://github.com/python/cpython/blob/5909ad1217aad200c69ffa794fcab285bacb609e/Lib/unittest/suite.py#L159
[1] https://github.com/python/cpython/blob/5909ad1217aad200c69ffa794fcab285bacb609e/Lib/test/test_dbm_gnu.py#L12
msg339810 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-04-09 21:34
It's more of a "can't be an instance method" than a strict "only classmethod".

Can I ask why you want it to be a strictmethod instead of a classmethod? I personally don't want to change the docs as I think that encourages a potentially bad situation where someone meant to have a classmethod and didn't realize the difference between that and staticmethod. But if there's a legitimate need for staticmethod then I'm open to reconsidering how the docs are written.
History
Date User Action Args
2022-04-11 14:59:13adminsetgithub: 80750
2019-04-09 21:34:14brett.cannonsetnosy: + brett.cannon
messages: + msg339810
2019-04-09 06:14:13xtreaksetnosy: + xtreak
messages: + msg339705
2019-04-09 05:40:35Peter de Blanccreate