Issue #373 duplicate

Assertion rewrite fails for PEP 420 style packages (without __init__.py) (Python 3.3)

Andreas Pelme avatarAndreas Pelme created an issue

To reproduce (with Python 3.3):

[pytest3.3] ~ $ mkdir tmp
[pytest3.3] ~ $ cd tmp
[pytest3.3] ~/tmp $ echo 'import foo.bar' > test_a.py
[pytest3.3] ~/tmp $ mkdir foo
[pytest3.3] ~/tmp $ touch foo/bar.py
[pytest3.3] ~/tmp $ py.test
==== test session starts ====
platform darwin -- Python 3.3.0 -- pytest-2.4.3.dev2
collected 0 items / 1 errors

==== ERRORS ====
____ ERROR collecting test_a.py ____
test_a.py:1: in <module>
>   import foo.bar
<frozen importlib._bootstrap>:1558: in _find_and_load
>   ???
<frozen importlib._bootstrap>:1516: in _find_and_load_unlocked
>   ???
<frozen importlib._bootstrap>:1470: in _find_module
>   ???
../code/pytest/_pytest/assertion/rewrite.py:60: in find_module
>           pth = path[0]
E           TypeError: '_NamespacePath' object does not support indexing
==== 1 error in 0.04 seconds ====

I tried to make a proper test for this in testing/test_assertrewrite.py like this:

def test_package_without__init__py(self, testdir):
    pkg = testdir.mkdir('a_package_without_init_py')
    mod = pkg.join('module.py').ensure()
    testdir.makepyfile("import a_package_without_init_py.module")
    assert testdir.runpytest().ret == 0

However, I cannot quite get that test to reproduce the failure above, instead this error appears:

==================================== ERRORS ====================================
______________ ERROR collecting test_package_without__init__py.py ______________
test_package_without__init__py.py:1: in <module>
>   import a_package_without_init_py.module
<frozen importlib._bootstrap>:1558: in _find_and_load
>   ???
<frozen importlib._bootstrap>:1516: in _find_and_load_unlocked
>   ???
<frozen importlib._bootstrap>:1470: in _find_module
>   ???
/Users/andreas/code/pytest/_pytest/assertion/rewrite.py:63: in find_module
>               fd, fn, desc = imp.find_module(lastname, path)
/Users/andreas/.virtualenvs/pytest3.3/lib/python3.3/imp.py:192: in find_module
>                              "not {}".format(type(name)))
E           RuntimeError: 'list' must be None or a list, not <class 'str'>
=========================== 1 error in 0.06 seconds ============================

If I set a breakpoint right before runpytest in that test, open a second shell, cwd to the testdir and invokes py.test by hand from there, the TypeError appears instead.

(Changing pth = path[0] to pth = list(path)[0] fixes the issue, but I am not sure that's the proper fix)

I have confirmed this bug in two environments:

  • Ubuntu 12.04 / Python 3.3.3 (py.test 2.4.2)
  • Mac OS / Python 3.3.0 (py.test tip (3169:e8e4c19c61c7))

Comments (1)

  1. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.