Improve error message for module-level __getattr__ issues#14170
Improve error message for module-level __getattr__ issues#14170veeceey 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
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>
|
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:
|
|
Hi @RonnyPfannschmidt -- just following up on this. I've addressed your feedback by moving the check from Would you be able to take another look when you get a chance? Happy to make further adjustments if anything else needs tweaking. Thanks! |
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