-
-
Notifications
You must be signed in to change notification settings - Fork 3k
correct-class-fixtures (#10819) (#14011) #14071
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
base: main
Are you sure you want to change the base?
correct-class-fixtures (#10819) (#14011) #14071
Conversation
yastcher
commented
Dec 28, 2025
- closes Inherit fixture from baseclass with class scope fails #14011
- closes Fixture with scope=class and autouse=True is not executed from mother's test class #10819
6c5967f to
9772d0c
Compare
9772d0c to
acd357c
Compare
acd357c to
ff1759c
Compare
| pass | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def fix(self) -> typing.Generator[None]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reminds me that we need to ensure that class scope fixtures do not actually ever get a instance - class scope is above instances and should get a class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed: added _ClassScopeInstance proxy that redirects attribute writes to the class, so class-scoped fixtures never work with actual test instances
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented _ClassScopeInstance proxy that redirects attribute writes to the class, so class-scoped fixtures work correctly without actual test instances.
However, I noticed your comment in #10819 about deprecating non-classmethod class-scoped fixtures. If that's the preferred direction, this fix might be unnecessary — we could add a deprecation warning instead and guide users toward @ classmethod fixtures.
Should I proceed with the proxy approach, or switch to deprecation warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically speaking its a bug to even try to pass a instance to a class scope fixture as no instance is available in that context
that a instance is actually avaliable is a bug an a artifact of how setupstate and scope initially developed
before there where fixtures we had the pytest_funcarg__ARGNAME hooks and no scopes at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. So the fix is correct — class-scoped fixtures should never receive an actual instance. The _ClassScopeInstance proxy ensures they work with the class directly, which is the intended behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should error in future and warn right now
Instance methods are always wrong
Class scope requires classmethods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. Will remove the proxy and add deprecation warning instead.
Proposed implementation:
In src/_pytest/deprecated.py:
CLASS_FIXTURE_INSTANCE_METHOD = PytestRemovedIn10Warning(
"Class-scoped fixture defined as instance method is deprecated.\n"
"Use @classmethod decorator instead.\n"
"See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method"
)Then warn in resolve_fixture_function when class-scoped fixture is an instance method.
Does this look right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the deprecation message should be very aggressive about the fact that any changes to the instance would in fact not transfer to subsequent instances
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CLASS_FIXTURE_INSTANCE_METHOD = PytestRemovedIn10Warning(
"Class-scoped fixture defined as instance method is deprecated.\n"
"Instance attributes set in this fixture will NOT be visible to test methods,\n"
"as each test gets a new instance while the fixture runs only once per class.\n"
"Use @classmethod decorator and set attributes on cls instead.\n"
"See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method"
)?
86b996f to
a79434e
Compare
a79434e to
bea8737
Compare