{"record":{"id":"d080482d14e51f65","repo":"pytest-dev/pytest","slug":"class-fixtures-not-supported-maybe-in-the-future","errorCode":null,"errorMessage":"class fixtures not supported (maybe in the future)","messagePattern":"class fixtures not supported \\(maybe in the future\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":1427,"sourceCode":"\n\n@final\n@dataclasses.dataclass(frozen=True)\nclass FixtureFunctionMarker:\n    scope: ScopeName | Callable[[str, Config], ScopeName]\n    params: tuple[object, ...] | None\n    autouse: bool = False\n    ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None\n    name: str | None = None\n\n    _ispytest: dataclasses.InitVar[bool] = False\n\n    def __post_init__(self, _ispytest: bool) -> None:\n        check_ispytest(_ispytest)\n\n    def __call__(self, function: FixtureFunction) -> FixtureFunctionDefinition:\n        if inspect.isclass(function):\n            raise ValueError(\"class fixtures not supported (maybe in the future)\")\n\n        if isinstance(function, FixtureFunctionDefinition):\n            raise ValueError(\n                f\"@pytest.fixture is being applied more than once to the same function {function.__name__!r}\"\n            )\n\n        if hasattr(function, \"pytestmark\"):\n            fail(\n                \"Marks cannot be applied to fixtures.\\n\"\n                \"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function\"\n            )\n\n        fixture_definition = FixtureFunctionDefinition(\n            function=function, fixture_function_marker=self, _ispytest=True\n        )\n\n        name = self.name or function.__name__\n        if name == \"request\":","sourceCodeStart":1409,"sourceCodeEnd":1445,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L1409-L1445","documentation":"FixtureFunctionDefinition.__call__ (fixtures.py:1428) rejects applying @pytest.fixture to a class. pytest fixtures must be functions; class-based fixtures are explicitly unsupported and the message notes 'maybe in the future'. This is a hard ValueError raised at decoration time, before any collection.","triggerScenarios":"Writing @pytest.fixture above a 'class Foo:' definition; attempting to use a class as a fixture (a pattern familiar from unittest setUpClass or dependency-injection frameworks).","commonSituations":"Migrating from a DI framework that uses classes; misunderstanding pytest's fixture model; accidentally stacking @pytest.fixture on a class helper instead of the function inside it.","solutions":["Convert the class into a fixture function returning an instance, or a factory fixture returning the class.","If you need parametrized setup, use a factory fixture: def make_thing(): return Thing.","Move the decorator to a method/function inside the class if a function was intended."],"exampleFix":"// before\n@pytest.fixture\nclass DbHelper:\n    ...\n// after\n@pytest.fixture\ndef db_helper():\n    return DbHelper()","handlingStrategy":"type-guard","validationCode":"import inspect, functools\ndef fixture_or_factory(obj):\n    assert not inspect.isclass(obj), '@pytest.fixture cannot decorate a class; pass a function'\n    return obj\n# usage: @functools.wraps ... ensure target is a function before decorating","typeGuard":"import inspect\ndef is_fixture_target_ok(fn) -> bool:\n    return inspect.isfunction(fn) or inspect.ismethod(fn)","tryCatchPattern":null,"preventionTips":["Only decorate functions/methods with @pytest.fixture.","For class-based setup, use a factory fixture returning an instance.","Code-review decorator placement."],"tags":["pytest","fixtures","decorator","class"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}