Skip to content

Commit 06936fb

Browse files
bluetechblueyed
authored andcommitted
python: small optimization in PyCollector.collect()
Inline `_makeitem()` so that `self.ihook` (which is moderately expensive) can be called only once. Note: the renamed test "test_makeitem_non_underscore" (added in 9ac4faf (2009)) was broken since ee2f292 (2010), and was not adjusted for 1d7c718 (2012) then. (Ref: pytest-dev#7671 (comment)) Co-authored-by: Daniel Hahler <[email protected]> (cherry picked from / based on commit daca174)
1 parent d7109c1 commit 06936fb

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/_pytest/python.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def collect(self):
378378
dicts.append(basecls.__dict__)
379379
seen = set()
380380
values = []
381+
ihook = self.ihook
381382
for dic in dicts:
382383
# Note: seems like the dict can change during iteration -
383384
# be careful not to remove the list() without consideration.
@@ -388,12 +389,15 @@ def collect(self):
388389
if name in seen:
389390
continue
390391
seen.add(name)
391-
res = self._makeitem(name, obj)
392+
res = ihook.pytest_pycollect_makeitem(
393+
collector=self, name=name, obj=obj
394+
)
392395
if res is None:
393396
continue
394-
if not isinstance(res, list):
395-
res = [res]
396-
values.extend(res)
397+
elif isinstance(res, list):
398+
values.extend(res)
399+
else:
400+
values.append(res)
397401

398402
def sort_key(item):
399403
fspath, lineno, _ = item.reportinfo()
@@ -402,10 +406,6 @@ def sort_key(item):
402406
values.sort(key=sort_key)
403407
return values
404408

405-
def _makeitem(self, name, obj):
406-
# assert self.ihook.fspath == self.fspath, self
407-
return self.ihook.pytest_pycollect_makeitem(collector=self, name=name, obj=obj)
408-
409409
def _genfunctions(self, name, funcobj):
410410
module = self.getparent(Module).obj
411411
clscol = self.getparent(Class)

testing/python/collect.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
import _pytest._code
66
import pytest
7+
from _pytest.compat import TYPE_CHECKING
78
from _pytest.config import ExitCode
89
from _pytest.nodes import Collector
910

11+
if TYPE_CHECKING:
12+
from _pytest.pytester import Testdir
13+
1014

1115
class TestModule:
1216
def test_failing_import(self, testdir):
@@ -845,14 +849,20 @@ def pytest_pycollect_makeitem(collector, name, obj):
845849
result = testdir.runpytest("--collect-only")
846850
result.stdout.fnmatch_lines(["*MyFunction*some*"])
847851

848-
def test_makeitem_non_underscore(self, testdir, monkeypatch):
849-
modcol = testdir.getmodulecol("def _hello(): pass")
852+
def test_makeitem_non_dunder(self, testdir: "Testdir") -> None:
853+
modcol = testdir.getmodulecol(
854+
"""
855+
def _hello(): pass
856+
def __world(): pass
857+
"""
858+
)
850859
values = []
851-
monkeypatch.setattr(
852-
pytest.Module, "_makeitem", lambda self, name, obj: values.append(name)
860+
testdir.monkeypatch.setattr(
861+
modcol.ihook, "pytest_pycollect_makeitem",
862+
lambda collector, name, obj: values.append(name)
853863
)
854-
values = modcol.collect()
855-
assert "_hello" not in values
864+
modcol.collect()
865+
assert values == ["_hello"]
856866

857867
def test_issue2369_collect_module_fileext(self, testdir):
858868
"""Ensure we can collect files with weird file extensions as Python

0 commit comments

Comments
 (0)