Skip to content

Refactor Session._initialparts to have a more explicit type #6547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Dict
from typing import FrozenSet
from typing import List
from typing import Tuple

import attr
import py
Expand Down Expand Up @@ -485,13 +486,13 @@ def _perform_collect(self, args, genitems):
self.trace("perform_collect", self, args)
self.trace.root.indent += 1
self._notfound = []
initialpaths = []
self._initialparts = []
initialpaths = [] # type: List[py.path.local]
self._initial_parts = [] # type: List[Tuple[py.path.local, List[str]]]
self.items = items = []
for arg in args:
parts = self._parsearg(arg)
self._initialparts.append(parts)
initialpaths.append(parts[0])
fspath, parts = self._parsearg(arg)
self._initial_parts.append((fspath, parts))
initialpaths.append(fspath)
self._initialpaths = frozenset(initialpaths)
rep = collect_one_node(self)
self.ihook.pytest_collectreport(report=rep)
Expand All @@ -511,13 +512,13 @@ def _perform_collect(self, args, genitems):
return items

def collect(self):
for initialpart in self._initialparts:
self.trace("processing argument", initialpart)
for fspath, parts in self._initial_parts:
self.trace("processing argument", (fspath, parts))
self.trace.root.indent += 1
try:
yield from self._collect(initialpart)
yield from self._collect(fspath, parts)
except NoMatch:
report_arg = "::".join(map(str, initialpart))
report_arg = "::".join((str(fspath), *parts))
# we are inside a make_report hook so
# we cannot directly pass through the exception
self._notfound.append((report_arg, sys.exc_info()[1]))
Expand All @@ -526,12 +527,9 @@ def collect(self):
self._collection_node_cache.clear()
self._collection_pkg_roots.clear()

def _collect(self, arg):
def _collect(self, argpath, names):
from _pytest.python import Package

names = arg[:]
argpath = names.pop(0)

# Start with a Session root, and delve to argpath item (dir or file)
# and stack all Packages found on the way.
# No point in finding packages when collecting doctests
Expand All @@ -555,7 +553,7 @@ def _collect(self, arg):
# If it's a directory argument, recurse and look for any Subpackages.
# Let the Package collector deal with subnodes, don't collect here.
if argpath.check(dir=1):
assert not names, "invalid arg {!r}".format(arg)
assert not names, "invalid arg {!r}".format((argpath, names))

seen_dirs = set()
for path in argpath.visit(
Expand Down Expand Up @@ -665,19 +663,19 @@ def _tryconvertpyarg(self, x):

def _parsearg(self, arg):
""" return (fspath, names) tuple after checking the file exists. """
parts = str(arg).split("::")
strpath, *parts = str(arg).split("::")
if self.config.option.pyargs:
parts[0] = self._tryconvertpyarg(parts[0])
relpath = parts[0].replace("/", os.sep)
path = self.config.invocation_dir.join(relpath, abs=True)
if not path.check():
strpath = self._tryconvertpyarg(strpath)
relpath = strpath.replace("/", os.sep)
fspath = self.config.invocation_dir.join(relpath, abs=True)
if not fspath.check():
if self.config.option.pyargs:
raise UsageError(
"file or package not found: " + arg + " (missing __init__.py?)"
)
raise UsageError("file not found: " + arg)
parts[0] = path.realpath()
return parts
fspath = fspath.realpath()
return (fspath, parts)

def matchnodes(self, matching, names):
self.trace("matchnodes", matching, names)
Expand Down
14 changes: 7 additions & 7 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def pytest_collect_file(path, parent):


class TestSession:
def test_parsearg(self, testdir):
def test_parsearg(self, testdir) -> None:
p = testdir.makepyfile("def test_func(): pass")
subdir = testdir.mkdir("sub")
subdir.ensure("__init__.py")
Expand All @@ -448,14 +448,14 @@ def test_parsearg(self, testdir):
config = testdir.parseconfig(p.basename)
rcol = Session.from_config(config)
assert rcol.fspath == subdir
parts = rcol._parsearg(p.basename)
fspath, parts = rcol._parsearg(p.basename)

assert parts[0] == target
assert fspath == target
assert len(parts) == 0
fspath, parts = rcol._parsearg(p.basename + "::test_func")
assert fspath == target
assert parts[0] == "test_func"
assert len(parts) == 1
parts = rcol._parsearg(p.basename + "::test_func")
assert parts[0] == target
assert parts[1] == "test_func"
assert len(parts) == 2

def test_collect_topdir(self, testdir):
p = testdir.makepyfile("def test_func(): pass")
Expand Down