python: small optimization in PyCollector.collect()
Inline `_makeitem()` so that `self.ihook` (which is moderately expensive) can be called only once. Note: the removed test "test_makeitem_non_underscore" comes from an old behavior of skipping names that start with `_` which has since been generalized, making the test no longer relevant.
This commit is contained in:
parent
2fcf763d7e
commit
daca174c98
|
@ -421,6 +421,7 @@ class PyCollector(PyobjMixin, nodes.Collector):
|
||||||
dicts.append(basecls.__dict__)
|
dicts.append(basecls.__dict__)
|
||||||
seen = set() # type: Set[str]
|
seen = set() # type: Set[str]
|
||||||
values = [] # type: List[Union[nodes.Item, nodes.Collector]]
|
values = [] # type: List[Union[nodes.Item, nodes.Collector]]
|
||||||
|
ihook = self.ihook
|
||||||
for dic in dicts:
|
for dic in dicts:
|
||||||
# Note: seems like the dict can change during iteration -
|
# Note: seems like the dict can change during iteration -
|
||||||
# be careful not to remove the list() without consideration.
|
# be careful not to remove the list() without consideration.
|
||||||
|
@ -430,12 +431,15 @@ class PyCollector(PyobjMixin, nodes.Collector):
|
||||||
if name in seen:
|
if name in seen:
|
||||||
continue
|
continue
|
||||||
seen.add(name)
|
seen.add(name)
|
||||||
res = self._makeitem(name, obj)
|
res = ihook.pytest_pycollect_makeitem(
|
||||||
|
collector=self, name=name, obj=obj
|
||||||
|
)
|
||||||
if res is None:
|
if res is None:
|
||||||
continue
|
continue
|
||||||
if not isinstance(res, list):
|
elif isinstance(res, list):
|
||||||
res = [res]
|
values.extend(res)
|
||||||
values.extend(res)
|
else:
|
||||||
|
values.append(res)
|
||||||
|
|
||||||
def sort_key(item):
|
def sort_key(item):
|
||||||
fspath, lineno, _ = item.reportinfo()
|
fspath, lineno, _ = item.reportinfo()
|
||||||
|
@ -444,17 +448,6 @@ class PyCollector(PyobjMixin, nodes.Collector):
|
||||||
values.sort(key=sort_key)
|
values.sort(key=sort_key)
|
||||||
return values
|
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"]:
|
def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
|
||||||
modulecol = self.getparent(Module)
|
modulecol = self.getparent(Module)
|
||||||
assert modulecol is not None
|
assert modulecol is not None
|
||||||
|
|
|
@ -843,15 +843,6 @@ class TestConftestCustomization:
|
||||||
result = testdir.runpytest("--collect-only")
|
result = testdir.runpytest("--collect-only")
|
||||||
result.stdout.fnmatch_lines(["*MyFunction*some*"])
|
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):
|
def test_issue2369_collect_module_fileext(self, testdir):
|
||||||
"""Ensure we can collect files with weird file extensions as Python
|
"""Ensure we can collect files with weird file extensions as Python
|
||||||
modules (#2369)"""
|
modules (#2369)"""
|
||||||
|
|
Loading…
Reference in New Issue