diff options
author | Ran Benita <ran@unusedvar.com> | 2020-08-26 18:05:08 +0300 |
---|---|---|
committer | Ran Benita <ran@unusedvar.com> | 2020-08-27 10:18:37 +0300 |
commit | daca174c987389e9cc1a95ce9497dffc6ce51020 (patch) | |
tree | 7ec4868dfadb03b9a18488e484a9633b43483f50 /src | |
parent | 2fcf763d7ee0c3f8e91682f2cfa5cd572f177b3b (diff) | |
download | pytest-daca174c987389e9cc1a95ce9497dffc6ce51020.tar.gz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/_pytest/python.py | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/src/_pytest/python.py b/src/_pytest/python.py index be21b61d6..eeccb4755 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -421,6 +421,7 @@ class PyCollector(PyobjMixin, 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. @@ -430,12 +431,15 @@ class PyCollector(PyobjMixin, 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() @@ -444,17 +448,6 @@ class PyCollector(PyobjMixin, nodes.Collector): 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 |