Skip to content

Commit

Permalink
Merge pull request #7699 from bluetech/optimize-makeitem
Browse files Browse the repository at this point in the history
python: small optimization in PyCollector.collect()
  • Loading branch information
nicoddemus committed Aug 28, 2020
2 parents 21aa6c4 + daca174 commit 877c621
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
23 changes: 8 additions & 15 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
dicts.append(basecls.__dict__)
seen = set() # type: Set[str]
values = [] # type: List[Union[nodes.Item, nodes.Collector]]
ihook = self.ihook
for dic in dicts:
# Note: seems like the dict can change during iteration -
# be careful not to remove the list() without consideration.
Expand All @@ -430,12 +431,15 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
if name in seen:
continue
seen.add(name)
res = self._makeitem(name, obj)
res = ihook.pytest_pycollect_makeitem(
collector=self, name=name, obj=obj
)
if res is None:
continue
if not isinstance(res, list):
res = [res]
values.extend(res)
elif isinstance(res, list):
values.extend(res)
else:
values.append(res)

def sort_key(item):
fspath, lineno, _ = item.reportinfo()
Expand All @@ -444,17 +448,6 @@ def sort_key(item):
values.sort(key=sort_key)
return values

def _makeitem(
self, name: str, obj: object
) -> Union[
None, nodes.Item, nodes.Collector, List[Union[nodes.Item, nodes.Collector]]
]:
# assert self.ihook.fspath == self.fspath, self
item = self.ihook.pytest_pycollect_makeitem(
collector=self, name=name, obj=obj
) # type: Union[None, nodes.Item, nodes.Collector, List[Union[nodes.Item, nodes.Collector]]]
return item

def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
modulecol = self.getparent(Module)
assert modulecol is not None
Expand Down
9 changes: 0 additions & 9 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,15 +843,6 @@ def pytest_pycollect_makeitem(collector, name, obj):
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*MyFunction*some*"])

def test_makeitem_non_underscore(self, testdir, monkeypatch):
modcol = testdir.getmodulecol("def _hello(): pass")
values = []
monkeypatch.setattr(
pytest.Module, "_makeitem", lambda self, name, obj: values.append(name)
)
values = modcol.collect()
assert "_hello" not in values

def test_issue2369_collect_module_fileext(self, testdir):
"""Ensure we can collect files with weird file extensions as Python
modules (#2369)"""
Expand Down

0 comments on commit 877c621

Please sign in to comment.