-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Remove extraneous traceback and simplify string repr of ConftestImportFailure #7222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove extraneous traceback and simplify string repr of ConftestImportFailure #7222
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -511,9 +516,10 @@ def _importconftest(self, conftestpath): | |||
# Using Path().resolve() is better than py.path.realpath because | |||
# it resolves to the correct path/drive in case-insensitive file systems (#5792) | |||
key = Path(str(conftestpath)).resolve() | |||
try: | |||
return self._conftestpath2mod[key] | |||
except KeyError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iirc we could still keep this structure but instead do
with contextlib.suppress(KeyError):
return ...
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion, it makes the code easier to follow, thanks.
I've also refactored the check for non-top-level pytest_plugins
declaration, I believe it makes the code even easier to follow then.
This removes the KeyError from the traceback chain when an conftest fails to import: return self._conftestpath2mod[key] E KeyError: WindowsPath('D:/projects/pytest/.tmp/root/foo/conftest.py') During handling of the above exception, another exception occurred: ... raise RuntimeError("some error") E RuntimeError: some error During handling of the above exception, another exception occurred: ... E _pytest.config.ConftestImportFailure: (...) By slightly changing the code, we can remove the first chain, which is often very confusing to users and doesn't help with anything. Fix pytest-dev#7223
The default message is often hard to read: E _pytest.config.ConftestImportFailure: (local('D:\\projects\\pytest\\.tmp\\root\\foo\\conftest.py'), (<class 'RuntimeError'>, RuntimeError('some error',), <traceback object at 0x000001CCC3E39348>)) Using a shorter message is better: E _pytest.config.ConftestImportFailure: RuntimeError: some error (from D:\projects\pytest\.tmp\root\foo\conftest.py) And we don't really lose any information due to exception chaining.
Decided to move the 'if' logic together with the error message, as this leaves the _importconftest method cleaner.
00447cc
to
c26f389
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes the
KeyError
traceback from the chain when an conftest fails to import:By slightly changing the code, we can remove the first chain, which is often confusing to users and doesn't help with anything.
Also while at it, simplified the string representation of
ConftestImportFailure
from:To:
Fix #7223