{"record":{"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":"validation","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/fixtures.py#L1412-L1448","documentation":"fixtures.py:1430 detects that @pytest.fixture is being stacked: the function passed to __call__ is already a FixtureFunctionDefinition (the wrapper pytest produced on the first decoration). pytest does not allow re-decoration; the second application raises ValueError naming the function. This usually comes from a typo or a decorator factory returning another @pytest.fixture.","triggerScenarios":"Writing '@pytest.fixture\\n@pytest.fixture\\ndef foo(): ...'; a custom decorator that itself applies @pytest.fixture being combined with an explicit @pytest.fixture; programmatically wrapping a fixture with the marker twice.","commonSituations":"Copy-paste leaving two decorator lines; refactoring a helper into a fixture while the original @pytest.fixture is still on the function; conflicting plugin/helper decorators that both mark fixtures.","solutions":["Remove the duplicate @pytest.fixture line, keeping exactly one.","If using a custom factory decorator, have it return the raw function and apply @pytest.fixture only once at the call site.","Search the file for repeated decorator usage via grep."],"exampleFix":"// before\n@pytest.fixture\n@pytest.fixture\ndef cache():\n    return {}\n// after\n@pytest.fixture\ndef cache():\n    return {}","handlingStrategy":"validation","validationCode":"# In a pre-commit grep, flag consecutive @pytest.fixture lines.\n# Or via AST: ensure FixtureFunctionDefinition is not re-wrapped.\nimport pathlib\nfor f in pathlib.Path('tests').rglob('*.py'):\n    txt = f.read_text()\n    assert '@pytest.fixture\\n@ pytest.fixture' not in txt.replace(' ',''), f'{f} has stacked fixtures'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply @pytest.fixture exactly once per function.","Avoid custom decorators that internally apply @pytest.fixture.","Use grep/AST linting to detect duplicates."],"tags":["pytest","fixtures","decorator","duplicate"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}