Skip to content

Commit f02dbaf

Browse files
Merge pull request #4010 from nicoddemus/package-len-error-3749
Fix 'Package has no len()' error during collection
2 parents f6eb39d + 41f6ea1 commit f02dbaf

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

changelog/3749.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the following error during collection of tests inside packages::
2+
3+
TypeError: object of type 'Package' has no len()

src/_pytest/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,14 @@ def _collect(self, arg):
504504
pkginit = parent.join("__init__.py")
505505
if pkginit.isfile():
506506
if pkginit in self._node_cache:
507-
root = self._node_cache[pkginit]
507+
root = self._node_cache[pkginit][0]
508508
else:
509509
col = root._collectfile(pkginit)
510510
if col:
511511
if isinstance(col[0], Package):
512512
root = col[0]
513-
self._node_cache[root.fspath] = root
513+
# always store a list in the cache, matchnodes expects it
514+
self._node_cache[root.fspath] = [root]
514515

515516
# If it's a directory argument, recurse and look for any Subpackages.
516517
# Let the Package collector deal with subnodes, don't collect here.
@@ -530,8 +531,8 @@ def _collect(self, arg):
530531
if (type(x), x.fspath) in self._node_cache:
531532
yield self._node_cache[(type(x), x.fspath)]
532533
else:
533-
yield x
534534
self._node_cache[(type(x), x.fspath)] = x
535+
yield x
535536
else:
536537
assert argpath.check(file=1)
537538

testing/example_scripts/collect/package_init_given_as_arg/pkg/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test():
2+
pass

testing/python/collect.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,13 @@ def test_package_collection_infinite_recursion(testdir):
15911591
result.stdout.fnmatch_lines("*1 passed*")
15921592

15931593

1594+
def test_package_collection_init_given_as_argument(testdir):
1595+
"""Regression test for #3749"""
1596+
p = testdir.copy_example("collect/package_init_given_as_arg")
1597+
result = testdir.runpytest(p / "pkg" / "__init__.py")
1598+
result.stdout.fnmatch_lines("*1 passed*")
1599+
1600+
15941601
def test_package_with_modules(testdir):
15951602
"""
15961603
.

0 commit comments

Comments
 (0)