Skip to content

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

Merged

Conversation

nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented May 16, 2020

This removes the KeyError traceback from the 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: (local('D:\\projects\\pytest\\.tmp\\root\\foo\\conftest.py'), (<class 'RuntimeError'>, RuntimeError('some error',), <traceback object at 0x000001CCC3E39348>))

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:

E   _pytest.config.ConftestImportFailure: (local('D:\\projects\\pytest\\.tmp\\root\\foo\\conftest.py'), (<class 'RuntimeError'>, RuntimeError('some error',), <traceback object at 0x000001CCC3E39348>))

To:

E   _pytest.config.ConftestImportFailure: RuntimeError: some error (from D:\projects\pytest\.tmp\root\foo\conftest.py)

Fix #7223

Copy link
Member

@asottile asottile left a 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:
Copy link
Member

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 ...
...

Copy link
Member Author

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.
@nicoddemus nicoddemus force-pushed the remove-key-error-conftest-exception branch from 00447cc to c26f389 Compare May 17, 2020 14:26
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus nicoddemus merged commit abbd97f into pytest-dev:master May 17, 2020
@nicoddemus nicoddemus deleted the remove-key-error-conftest-exception branch May 17, 2020 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImportMismatchError from conftest.py confusion
3 participants