Skip to content

Commit cb66aa3

Browse files
committed
main: inline _collect() into collect()
This removes an unhelpful level of indirection and enables some upcoming upcoming simplifications.
1 parent 78a4a12 commit cb66aa3

File tree

1 file changed

+90
-91
lines changed

1 file changed

+90
-91
lines changed

src/_pytest/main.py

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -623,104 +623,103 @@ def perform_collect( # noqa: F811
623623
return items
624624

625625
def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
626-
for fspath, parts in self._initial_parts:
627-
self.trace("processing argument", (fspath, parts))
628-
self.trace.root.indent += 1
629-
yield from self._collect(fspath, parts)
630-
self.trace.root.indent -= 1
631-
self._collection_node_cache1.clear()
632-
self._collection_node_cache2.clear()
633-
self._collection_matchnodes_cache.clear()
634-
self._collection_pkg_roots.clear()
635-
636-
def _collect(
637-
self, argpath: py.path.local, names: Sequence[str]
638-
) -> Iterator[Union[nodes.Item, nodes.Collector]]:
639626
from _pytest.python import Package
640627

641-
# Start with a Session root, and delve to argpath item (dir or file)
642-
# and stack all Packages found on the way.
643-
# No point in finding packages when collecting doctests.
644-
if not self.config.getoption("doctestmodules", False):
645-
pm = self.config.pluginmanager
646-
for parent in reversed(argpath.parts()):
647-
if pm._confcutdir and pm._confcutdir.relto(parent):
648-
break
649-
650-
if parent.isdir():
651-
pkginit = parent.join("__init__.py")
652-
if pkginit.isfile():
653-
if pkginit not in self._collection_node_cache1:
654-
col = self._collectfile(pkginit, handle_dupes=False)
655-
if col:
656-
if isinstance(col[0], Package):
657-
self._collection_pkg_roots[str(parent)] = col[0]
658-
self._collection_node_cache1[col[0].fspath] = [col[0]]
659-
660-
# If it's a directory argument, recurse and look for any Subpackages.
661-
# Let the Package collector deal with subnodes, don't collect here.
662-
if argpath.check(dir=1):
663-
assert not names, "invalid arg {!r}".format((argpath, names))
664-
665-
seen_dirs = set() # type: Set[py.path.local]
666-
for direntry in visit(str(argpath), self._recurse):
667-
if not direntry.is_file():
668-
continue
669-
670-
path = py.path.local(direntry.path)
671-
dirpath = path.dirpath()
628+
for argpath, names in self._initial_parts:
629+
self.trace("processing argument", (argpath, names))
630+
self.trace.root.indent += 1
672631

673-
if dirpath not in seen_dirs:
674-
# Collect packages first.
675-
seen_dirs.add(dirpath)
676-
pkginit = dirpath.join("__init__.py")
677-
if pkginit.exists():
678-
for x in self._collectfile(pkginit):
632+
# Start with a Session root, and delve to argpath item (dir or file)
633+
# and stack all Packages found on the way.
634+
# No point in finding packages when collecting doctests.
635+
if not self.config.getoption("doctestmodules", False):
636+
pm = self.config.pluginmanager
637+
for parent in reversed(argpath.parts()):
638+
if pm._confcutdir and pm._confcutdir.relto(parent):
639+
break
640+
641+
if parent.isdir():
642+
pkginit = parent.join("__init__.py")
643+
if pkginit.isfile():
644+
if pkginit not in self._collection_node_cache1:
645+
col = self._collectfile(pkginit, handle_dupes=False)
646+
if col:
647+
if isinstance(col[0], Package):
648+
self._collection_pkg_roots[str(parent)] = col[0]
649+
self._collection_node_cache1[col[0].fspath] = [
650+
col[0]
651+
]
652+
653+
# If it's a directory argument, recurse and look for any Subpackages.
654+
# Let the Package collector deal with subnodes, don't collect here.
655+
if argpath.check(dir=1):
656+
assert not names, "invalid arg {!r}".format((argpath, names))
657+
658+
seen_dirs = set() # type: Set[py.path.local]
659+
for direntry in visit(str(argpath), self._recurse):
660+
if not direntry.is_file():
661+
continue
662+
663+
path = py.path.local(direntry.path)
664+
dirpath = path.dirpath()
665+
666+
if dirpath not in seen_dirs:
667+
# Collect packages first.
668+
seen_dirs.add(dirpath)
669+
pkginit = dirpath.join("__init__.py")
670+
if pkginit.exists():
671+
for x in self._collectfile(pkginit):
672+
yield x
673+
if isinstance(x, Package):
674+
self._collection_pkg_roots[str(dirpath)] = x
675+
if str(dirpath) in self._collection_pkg_roots:
676+
# Do not collect packages here.
677+
continue
678+
679+
for x in self._collectfile(path):
680+
key = (type(x), x.fspath)
681+
if key in self._collection_node_cache2:
682+
yield self._collection_node_cache2[key]
683+
else:
684+
self._collection_node_cache2[key] = x
679685
yield x
680-
if isinstance(x, Package):
681-
self._collection_pkg_roots[str(dirpath)] = x
682-
if str(dirpath) in self._collection_pkg_roots:
683-
# Do not collect packages here.
686+
else:
687+
assert argpath.check(file=1)
688+
689+
if argpath in self._collection_node_cache1:
690+
col = self._collection_node_cache1[argpath]
691+
else:
692+
collect_root = self._collection_pkg_roots.get(argpath.dirname, self)
693+
col = collect_root._collectfile(argpath, handle_dupes=False)
694+
if col:
695+
self._collection_node_cache1[argpath] = col
696+
m = self.matchnodes(col, names)
697+
if not m:
698+
report_arg = "::".join((str(argpath), *names))
699+
self._notfound.append((report_arg, col))
684700
continue
685701

686-
for x in self._collectfile(path):
687-
key = (type(x), x.fspath)
688-
if key in self._collection_node_cache2:
689-
yield self._collection_node_cache2[key]
690-
else:
691-
self._collection_node_cache2[key] = x
692-
yield x
693-
else:
694-
assert argpath.check(file=1)
702+
# If __init__.py was the only file requested, then the matched node will be
703+
# the corresponding Package, and the first yielded item will be the __init__
704+
# Module itself, so just use that. If this special case isn't taken, then all
705+
# the files in the package will be yielded.
706+
if argpath.basename == "__init__.py":
707+
assert isinstance(m[0], nodes.Collector)
708+
try:
709+
yield next(iter(m[0].collect()))
710+
except StopIteration:
711+
# The package collects nothing with only an __init__.py
712+
# file in it, which gets ignored by the default
713+
# "python_files" option.
714+
pass
715+
continue
716+
yield from m
695717

696-
if argpath in self._collection_node_cache1:
697-
col = self._collection_node_cache1[argpath]
698-
else:
699-
collect_root = self._collection_pkg_roots.get(argpath.dirname, self)
700-
col = collect_root._collectfile(argpath, handle_dupes=False)
701-
if col:
702-
self._collection_node_cache1[argpath] = col
703-
m = self.matchnodes(col, names)
704-
if not m:
705-
report_arg = "::".join((str(argpath), *names))
706-
self._notfound.append((report_arg, col))
707-
return
708-
709-
# If __init__.py was the only file requested, then the matched node will be
710-
# the corresponding Package, and the first yielded item will be the __init__
711-
# Module itself, so just use that. If this special case isn't taken, then all
712-
# the files in the package will be yielded.
713-
if argpath.basename == "__init__.py":
714-
assert isinstance(m[0], nodes.Collector)
715-
try:
716-
yield next(iter(m[0].collect()))
717-
except StopIteration:
718-
# The package collects nothing with only an __init__.py
719-
# file in it, which gets ignored by the default
720-
# "python_files" option.
721-
pass
722-
return
723-
yield from m
718+
self.trace.root.indent -= 1
719+
self._collection_node_cache1.clear()
720+
self._collection_node_cache2.clear()
721+
self._collection_matchnodes_cache.clear()
722+
self._collection_pkg_roots.clear()
724723

725724
def matchnodes(
726725
self,

0 commit comments

Comments
 (0)