Skip to content

Commit 19a15a9

Browse files
authored
Merge pull request #6197 from asottile/fix_init_py_discovery
Fix incorrect discovery of non-test `__init__.py` files.
2 parents e856638 + 4e0f992 commit 19a15a9

File tree

5 files changed

+29
-41
lines changed

5 files changed

+29
-41
lines changed

changelog/6194.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect discovery of non-test ``__init__.py`` files.

changelog/6197.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert "The first test in a package (``__init__.py``) marked with ``@pytest.mark.skip`` is now correctly skipped.".

src/_pytest/python.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,18 @@ class PyobjMixin(PyobjContext):
251251
@property
252252
def obj(self):
253253
"""Underlying Python object."""
254-
self._mount_obj_if_needed()
255-
return self._obj
256-
257-
@obj.setter
258-
def obj(self, value):
259-
self._obj = value
260-
261-
def _mount_obj_if_needed(self):
262254
obj = getattr(self, "_obj", None)
263255
if obj is None:
264256
self._obj = obj = self._getobj()
265257
# XXX evil hack
266258
# used to avoid Instance collector marker duplication
267259
if self._ALLOW_MARKERS:
268-
self.own_markers.extend(get_unpacked_marks(obj))
260+
self.own_markers.extend(get_unpacked_marks(self.obj))
261+
return obj
262+
263+
@obj.setter
264+
def obj(self, value):
265+
self._obj = value
269266

270267
def _getobj(self):
271268
"""Gets the underlying Python object. May be overwritten by subclasses."""
@@ -432,14 +429,6 @@ def _genfunctions(self, name, funcobj):
432429
class Module(nodes.File, PyCollector):
433430
""" Collector for test classes and functions. """
434431

435-
def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
436-
if fspath.basename == "__init__.py":
437-
self._ALLOW_MARKERS = False
438-
439-
nodes.FSCollector.__init__(
440-
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
441-
)
442-
443432
def _getobj(self):
444433
return self._importtestmodule()
445434

@@ -639,7 +628,6 @@ def isinitpath(self, path):
639628
return path in self.session._initialpaths
640629

641630
def collect(self):
642-
self._mount_obj_if_needed()
643631
this_path = self.fspath.dirpath()
644632
init_module = this_path.join("__init__.py")
645633
if init_module.check(file=1) and path_matches_patterns(

testing/test_collection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,24 @@ def test_collector_respects_tbstyle(testdir):
12571257
"*= 1 error in *",
12581258
]
12591259
)
1260+
1261+
1262+
def test_does_not_eagerly_collect_packages(testdir):
1263+
testdir.makepyfile("def test(): pass")
1264+
pydir = testdir.mkpydir("foopkg")
1265+
pydir.join("__init__.py").write("assert False")
1266+
result = testdir.runpytest()
1267+
assert result.ret == ExitCode.OK
1268+
1269+
1270+
def test_does_not_put_src_on_path(testdir):
1271+
# `src` is not on sys.path so it should not be importable
1272+
testdir.tmpdir.join("src/nope/__init__.py").ensure()
1273+
testdir.makepyfile(
1274+
"import pytest\n"
1275+
"def test():\n"
1276+
" with pytest.raises(ImportError):\n"
1277+
" import nope\n"
1278+
)
1279+
result = testdir.runpytest()
1280+
assert result.ret == ExitCode.OK

testing/test_skipping.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,26 +1162,3 @@ def test_importorskip():
11621162
match="^could not import 'doesnotexist': No module named .*",
11631163
):
11641164
pytest.importorskip("doesnotexist")
1165-
1166-
1167-
def test_skip_package(testdir):
1168-
testdir.makepyfile(
1169-
__init__="""
1170-
import pytest
1171-
pytestmark = pytest.mark.skip
1172-
"""
1173-
)
1174-
1175-
testdir.makepyfile(
1176-
"""
1177-
import pytest
1178-
def test_skip1():
1179-
assert 0
1180-
def test_skip2():
1181-
assert 0
1182-
"""
1183-
)
1184-
1185-
result = testdir.inline_run()
1186-
_, skipped, _ = result.listoutcomes()
1187-
assert len(skipped) == 2

0 commit comments

Comments
 (0)