Skip to content

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes #11281

This PR changes fixture discovery to preserve definition order instead of using alphabetical sorting from dir().

Problem

Previously, parsefactories() used dir() to iterate over object attributes, which sorts names alphabetically. This caused unexpected behavior when fixtures with the same name were defined at different scopes (e.g., module vs class level), because the order of discovery depended on alphabetical sorting rather than source code order.

Solution

Changed parsefactories() to use __dict__ iteration (similar to how PyCollector.collect() works), which preserves insertion/definition order in Python 3.7+. This ensures fixtures are processed in the order they appear in source code, making behavior predictable and intuitive.

Changes

  • Modified FixtureManager.parsefactories() to iterate __dict__ instead of dir()
  • Added logic to handle MRO for class fixtures (similar to PyCollector)
  • Added tests to verify definition order is respected
  • Added changelog entry

Test plan

🤖 Generated with Claude Code

Fixtures are now discovered in their definition order (as they appear in
source code) rather than alphabetical order. This change resolves issues
where fixtures with the same name at different scopes would be processed
in unexpected order due to dir() sorting.

The fix changes parsefactories() to use __dict__ iteration (which
preserves insertion order in Python 3.7+) instead of dir() which sorts.
This makes fixture behavior predictable based on source code order.

Fixes pytest-dev#11281
Related to pytest-dev#12952
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Feb 8, 2026
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.

Autouse fixtures with the same name in class and module scopes are executed in the wrong order

1 participant