Improve assert mod not in mods error message#11795
Merged
bluetech merged 1 commit intopytest-dev:mainfrom Jan 14, 2024
Merged
Conversation
nicoddemus
approved these changes
Jan 10, 2024
Member
nicoddemus
left a comment
There was a problem hiding this comment.
Indeed having a better error message is something we should have for all internal assert calls, thanks!
bluetech
reviewed
Jan 10, 2024
Member
bluetech
left a comment
There was a problem hiding this comment.
Thanks, this would have been useful with the issue.
src/_pytest/config/__init__.py
Outdated
| for path, mods in self._dirpath2confmods.items(): | ||
| if dirpath in path.parents or path == dirpath: | ||
| assert mod not in mods | ||
| error_message = f"{str(conftestpath)=} has already been loaded as the module {mod=}" |
Member
There was a problem hiding this comment.
I do think it will be useful to include the __file__ as a debugging aid - it may no longer be at fault but it's useful to see where mod came from.
[ran: tweaked message, made the formatting lazy]
be9d154 to
1c9d683
Compare
bluetech
approved these changes
Jan 14, 2024
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.
Related to #9765, add a better error message than
assert mod not in mods. This would have been super useful to figure out from the error message the difference fromconftestpathandmod.__file__. Hopefully the #11708 will be merged and this error is less likely to occur, but at least if a variation of the issue occurs again there will be a better error message.Here is the error on my reproducer https://github.com/lesteve/pytest-issue-9765-reproducer:
For now I keep an
AssertionErrorbut let me know if you think another kind of error is more appropriate.It does not seem that easy to add a test for this ...
Let me know if you think this is worth adding a changelog for this or not!
Depending on how #11708 goes, this may need to be tweaked since a discrepancy between
mod.__file__andstr(contestpath)is likely not how this problem will show up. It could even lead to red-herring, since it will be potentially fine thatmod.__file__andstr(conftestpath)are not the consistent and people may be misled by reading comments like #9765 (comment) ...