Improve error message for module-level __getattr__ issues#14170
Open
veeceey wants to merge 3 commits intopytest-dev:mainfrom
Open
Improve error message for module-level __getattr__ issues#14170veeceey wants to merge 3 commits intopytest-dev:mainfrom
veeceey wants to merge 3 commits intopytest-dev:mainfrom
Conversation
When a module defines a custom __getattr__ that returns None instead of raising AttributeError, pytest now provides a helpful error message explaining the likely cause and suggesting the fix. The previous error was: "got None instead of Mark" The new error includes: "this is likely caused by a module-level __getattr__ that does not raise AttributeError for missing attributes" Fixes pytest-dev#8265
for more information, see https://pre-commit.ci
RonnyPfannschmidt
requested changes
Feb 8, 2026
Member
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
This is the wrong place for the check there should be a warning triggered in the helper to get that list
…marks Per reviewer feedback, move the check for module-level __getattr__ returning None to get_unpacked_marks() (the helper that fetches the mark list) instead of normalize_mark_list(). Also change from raising a TypeError to emitting a PytestCollectionWarning, so the module can still be collected normally. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
Thanks for the review @RonnyPfannschmidt! You're right — the check was in the wrong place. I've moved it from Changes in the latest push:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #8265
This PR improves the error message when a module-level
__getattr__returnsNoneinstead of raisingAttributeError.Problem
When a module defines a custom
__getattr__that returnsNoneinstead of raisingAttributeError, pytest gives a cryptic error message:This is confusing for users who don't understand what a "Mark" is or why pytest is looking for one.
Solution
Added a special check in
get_unpacked_marks()to detect when thepytestmarkattribute resolves toNoneand emit aPytestCollectionWarningthat explains:__getattr__)AttributeError)Changes
get_unpacked_marks()in/src/_pytest/mark/structures.pyto detectNoneand emit a warningTest plan
testing/test_mark.pyto verify new warning message