{"id":"f7b5755152b533cb","repo":"pytest-dev/pytest","slug":"pytest-fixture-is-being-applied-more-than-once-to","errorCode":null,"errorMessage":"@pytest.fixture is being applied more than once to the same function {function.__name__!r}","messagePattern":"@pytest\\.fixture is being applied more than once to the same function (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/fixtures.py","lineNumber":1430,"sourceCode":"@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\":\n            location = getlocation(function)\n            fail(\n                f\"'request' is a reserved word for fixtures, use another name:\\n  {location}\",","sourceCodeStart":1412,"sourceCodeEnd":1448,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/fixtures.py#L1412-L1448","documentation":"Raised by `FixtureFunctionMarker.__call__` when the function it receives is already a `FixtureFunctionDefinition`, i.e. `@pytest.fixture` has been stacked twice on the same function. Applying the decorator a second time wraps the already-produced definition object, which pytest rejects because the scope/params/name would be ambiguous.","triggerScenarios":"Stacking decorators: `@pytest.fixture(scope=\"session\")\\n@pytest.fixture\\ndef x(): ...`, or a helper that reapplies `@pytest.fixture` to an already-decorated fixture.","commonSituations":"Copy-paste of decorators. A custom decorator that internally calls `pytest.fixture` applied to a function already decorated. Migrating old code that used `@pytest.yield_fixture` plus `@pytest.fixture`.","solutions":["Use exactly one `@pytest.fixture(...)` decorator per function with all options in a single call.","If a helper adds fixture behavior, have it return a plain function and decorate once at the definition site.","Remove the redundant decorator."],"exampleFix":"// before\n@pytest.fixture(scope=\"session\")\n@pytest.fixture\ndef x():\n    yield 1\n// after\n@pytest.fixture(scope=\"session\")\ndef x():\n    yield 1","handlingStrategy":"validation","validationCode":"def ensure_single_fixture_decorator(fn):\n    if isinstance(fn, FixtureFunctionDefinition):\n        raise ValueError(f\"{fn.__name__!r} already decorated with @pytest.fixture\")\n    return fn","typeGuard":"def is_already_fixture(fn) -> bool:\n    from _pytest.fixtures import FixtureFunctionDefinition\n    return isinstance(fn, FixtureFunctionDefinition)","tryCatchPattern":null,"preventionTips":["Apply @pytest.fixture exactly once per function.","Put all fixture options in a single decorator call.","Avoid helper decorators that re-apply pytest.fixture."],"tags":["pytest","fixtures","decorator","valueerror"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}