Deprecate self in module, package and session scoped fixtures - #14911
Closed
nuemaan wants to merge 1 commit into
Closed
Deprecate self in module, package and session scoped fixtures#14911nuemaan wants to merge 1 commit into
nuemaan wants to merge 1 commit into
Conversation
A class-scoped fixture defined as an instance method already warns, because attributes set on self never reach the tests. Each test runs on a fresh instance while the fixture runs once per class. Module, package and session scope have the same problem, and they were still silent. Extend the deprecation to cover them. The message differs from the class-scoped one. For those wider scopes the class body is not a useful place to hang state either, so it points at @staticmethod rather than @classmethod. Four fixtures in our own test suite matched the pattern. All of them took self without using it, so they became staticmethods. TestImportPath.path1 called self.setuptestfs, which does not use self either, so that helper became a staticmethod too. Closes pytest-dev#14765
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14765.
#14071 deprecated instance-method fixtures at
classscope, since attributes set onselfnever reach the tests. Each test runs on a fresh instance while the fixture runs once per class.module,packageandsessionscope have exactly the same problem and were still silent, so this extends the deprecation to cover them.Confirmed the breakage is real before writing the warning. With
scope="module"orscope="session", a fixture that setsself.attrleaves the test seeing nothing:The message differs from the class-scoped one.
@classmethodis what the class-scoped warning recommends, but for these wider scopes the class body is not a useful place to hang state either, so the new message points at@staticmethod. Worth noting that@classmethoddoes happen to work at these scopes, so if you would rather the message mention both, that is a one-line change.Function scope is deliberately untouched. The fixture and the test share an instance there, so
selfbehaves as people expect.Four fixtures in our own test suite matched the pattern:
testing/test_legacypath.pyTestFixtureRequestSessionScoped.session_requesttesting/test_conftest.pyTestConftestValueAccessGlobal.basedirtesting/test_pathlib.pyTestImportPath.path1testing/python/fixtures.pyTestRequestSessionScoped.session_requestEvery one of them took
selfwithout using it, which is some evidence the deprecation catches accidental instance methods rather than deliberate ones. They are now staticmethods.TestImportPath.path1calledself.setuptestfs, which does not useselfeither, so that helper became a staticmethod as well.Verification
testing/deprecated_test.py, 23 passed, including 7 newtesting/suite with-n 8, 3761 passed against 3754 on unmodified main. Same 1 failure and same 5 errors appear on both, all from optional deps missing in my environment,attrsamong themruff check,ruff format --check, andmypyon the changed files, all cleansrc/files and re-ran the new tests to confirm they fail without the change, rather than passing regardless