Skip to content

fix #13564 - warn when fixtures get wrapped with a decorator#13745

Open
RonnyPfannschmidt wants to merge 4 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-13564-warn-on-decorated-fixturedef
Open

fix #13564 - warn when fixtures get wrapped with a decorator#13745
RonnyPfannschmidt wants to merge 4 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-13564-warn-on-decorated-fixturedef

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt force-pushed the ronny/fix-13564-warn-on-decorated-fixturedef branch from c879256 to 9e7e132 Compare September 24, 2025 07:12
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 24, 2025
@RonnyPfannschmidt RonnyPfannschmidt force-pushed the ronny/fix-13564-warn-on-decorated-fixturedef branch 2 times, most recently from b79b0ed to 59a31ca Compare September 24, 2025 07:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a warning mechanism to detect when pytest fixtures are wrapped with custom decorators, which prevents pytest from discovering them during collection. The warning helps developers understand why their fixtures might not be working as expected.

Key Changes:

  • Added a warning system that detects fixtures wrapped in decorators using functools.wraps
  • Implemented safe wrapper chain traversal with loop detection and special object handling
  • Added comprehensive test coverage for the new warning functionality

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/_pytest/fixtures.py Implements the core detection logic for wrapped fixtures and issues warnings when found
testing/python/fixtures.py Adds test case to verify the warning is correctly issued for decorated fixtures
changelog/13564.improvement.rst Documents the new warning feature for wrapped fixtures

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/_pytest/fixtures.py Outdated
Comment thread src/_pytest/fixtures.py Outdated

@Zac-HD Zac-HD left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread src/_pytest/fixtures.py Outdated
RonnyPfannschmidt and others added 4 commits June 14, 2026 11:01
Refactor the wrapped fixture detection logic to safely handle:
- Mock objects (which have _mock_name attribute)
- Proxy objects with problematic __class__ properties
- Wrapper loops (like in mock.call)
- Objects that raise exceptions during isinstance checks

The new _find_wrapped_fixture_def() method walks the wrapper chain
more safely and avoids infinite loops and errors from special objects.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Prevent OOM crash when encountering objects with self-referential
__wrapped__ attributes. The loop detection logic now checks object
IDs at the start of each iteration and includes a maximum depth
limit as an additional safeguard.

The original code had a timing issue where it checked id(wrapped)
against seen after fetching __wrapped__ but before updating current,
which failed to detect self-referential objects properly.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Clarify mock object skip comment to explain why (avoid false positives)
- Use unambiguous pytest#214 reference instead of bare "issue pytest-dev#214"
- Simplify loop: use safe_getattr directly for __wrapped__ traversal,
  the None check at loop start already handles termination
- Remove narrating comments, add type annotation to seen set
- Inline _issue_fixture_wrapped_warning into _check_for_wrapped_fixture
  and drop unused nodeid parameter from both helpers
- Guard warn_explicit_for with isinstance check for FunctionType

Co-authored-by: Cursor AI <ai@cursor.sh>
Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
@RonnyPfannschmidt RonnyPfannschmidt force-pushed the ronny/fix-13564-warn-on-decorated-fixturedef branch from 2510a6a to 3f27e81 Compare June 14, 2026 09:01
@RonnyPfannschmidt RonnyPfannschmidt marked this pull request as ready for review June 14, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixture definition check in parsefactories is too strict

3 participants