Skip to content

Improve tracebacks for ImportErrors in conftest #4124

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

Here's an conftest import error on master:

λ pytest 
Traceback (most recent call last):
  File "c:\pytest\src\_pytest\config\__init__.py", line 381, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('c:\\pytest\\.tmp\\conftest-err\\tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "c:\pytest\src\_pytest\config\__init__.py", line 412, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('c:\\pytest\\.tmp\\conftest-err\\tests\\conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "c:\pytest\src\_pytest\config\__init__.py", line 418, in _importconftest
    mod = conftestpath.pyimport()
  File "c:\pytest\.env36\lib\site-packages\py\_path\local.py", line 668, in pyimport
    __import__(modname)
  File "c:\pytest\src\_pytest\assertion\rewrite.py", line 290, in load_module
    six.exec_(co, mod.__dict__)
  File "c:\pytest\.tmp\conftest-err\tests\conftest.py", line 10, in <module>
    some_import()
  File "c:\pytest\.tmp\conftest-err\tests\conftest.py", line 6, in some_import
    other_import()
  File "c:\pytest\.tmp\conftest-err\tests\conftest.py", line 2, in other_import
    import invalid_internal
ModuleNotFoundError: No module named 'invalid_internal'
ERROR: could not load c:\pytest\.tmp\conftest-err\tests\conftest.py

and here's the same error on this branch:

λ pytest .tmp\conftest-err\
ImportError while loading conftest 'c:\pytest\.tmp\conftest-err\tests\conftest.py'.
tests\conftest.py:10: in <module>
    some_import()
tests\conftest.py:6: in some_import
    other_import()
tests\conftest.py:2: in other_import
    import invalid_internal
E   ModuleNotFoundError: No module named 'invalid_internal'

Fix #3332

cc @The-Compiler

Thanks @feuillemorte for the initial take on this in #3345 👍

# points to dynamically generated code
# see https://bitbucket.org/pytest-dev/py/issues/71
raw_filename = entry.frame.code.raw.co_filename
is_generated = "<" in raw_filename and ">" in raw_filename
Copy link
Contributor

Choose a reason for hiding this comment

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

<> would be a valid filename, no? Similar to foo><bar - so maybe use startswith/endswith at least?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that it is just moved code though.

Copy link
Member Author

@nicoddemus nicoddemus Oct 12, 2018

Choose a reason for hiding this comment

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

Makes sense, but I'm not sure I would like to touch that now, there might be some corner case which is covered by that, plus <> are not valid characters for filenames AFAIK so I think we're safe. 😁

@coveralls
Copy link

coveralls commented Oct 12, 2018

Coverage Status

Coverage increased (+0.04%) to 93.793% when pulling ef97121 on nicoddemus:traceback-import-error-3332 into 933de16 on pytest-dev:features.

@nicoddemus nicoddemus merged commit ed42ada into pytest-dev:features Oct 13, 2018
@nicoddemus nicoddemus deleted the traceback-import-error-3332 branch October 13, 2018 12:25
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.

3 participants