From 32edc4655cc3cff79a935fbd4e9f474fa3001ffd Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 15 Aug 2020 12:38:57 +0300 Subject: [PATCH] main: inline Session._matchnodes() into Session.matchnodes() Similar to the previous commit, this makes things more straightforward. --- src/_pytest/main.py | 85 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 7281ff9d1..06619023e 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -709,52 +709,51 @@ class Session(nodes.FSCollector): ) -> Sequence[Union[nodes.Item, nodes.Collector]]: self.trace("matchnodes", matching, names) self.trace.root.indent += 1 - nodes = self._matchnodes(matching, names) - num = len(nodes) - self.trace("matchnodes finished -> ", num, "nodes") - self.trace.root.indent -= 1 - if num == 0: - raise NoMatch(matching, names[:1]) - return nodes - def _matchnodes( - self, matching: Sequence[Union[nodes.Item, nodes.Collector]], names: List[str], - ) -> Sequence[Union[nodes.Item, nodes.Collector]]: if not matching or not names: - return matching - name = names[0] - assert name - nextnames = names[1:] - resultnodes = [] # type: List[Union[nodes.Item, nodes.Collector]] - for node in matching: - if isinstance(node, nodes.Item): - if not names: - resultnodes.append(node) - continue - assert isinstance(node, nodes.Collector) - key = (type(node), node.nodeid) - if key in self._collection_node_cache3: - rep = self._collection_node_cache3[key] - else: - rep = collect_one_node(node) - self._collection_node_cache3[key] = rep - if rep.passed: - has_matched = False - for x in rep.result: - # TODO: Remove parametrized workaround once collection structure contains parametrization. - if x.name == name or x.name.split("[")[0] == name: + result = matching + else: + name = names[0] + assert name + nextnames = names[1:] + resultnodes = [] # type: List[Union[nodes.Item, nodes.Collector]] + for node in matching: + if isinstance(node, nodes.Item): + if not names: + resultnodes.append(node) + continue + assert isinstance(node, nodes.Collector) + key = (type(node), node.nodeid) + if key in self._collection_node_cache3: + rep = self._collection_node_cache3[key] + else: + rep = collect_one_node(node) + self._collection_node_cache3[key] = rep + if rep.passed: + has_matched = False + for x in rep.result: + # TODO: Remove parametrized workaround once collection structure contains parametrization. + if x.name == name or x.name.split("[")[0] == name: + resultnodes.extend(self.matchnodes([x], nextnames)) + has_matched = True + # XXX Accept IDs that don't have "()" for class instances. + if not has_matched and len(rep.result) == 1 and x.name == "()": + nextnames.insert(0, name) resultnodes.extend(self.matchnodes([x], nextnames)) - has_matched = True - # XXX Accept IDs that don't have "()" for class instances. - if not has_matched and len(rep.result) == 1 and x.name == "()": - nextnames.insert(0, name) - resultnodes.extend(self.matchnodes([x], nextnames)) - else: - # Report collection failures here to avoid failing to run some test - # specified in the command line because the module could not be - # imported (#134). - node.ihook.pytest_collectreport(report=rep) - return resultnodes + else: + # Report collection failures here to avoid failing to run some test + # specified in the command line because the module could not be + # imported (#134). + node.ihook.pytest_collectreport(report=rep) + result = resultnodes + + self.trace("matchnodes finished -> ", len(result), "nodes") + self.trace.root.indent -= 1 + + if not result: + raise NoMatch(matching, names[:1]) + else: + return result def genitems( self, node: Union[nodes.Item, nodes.Collector]