Skip to content

Commit 28b8f3c

Browse files
authored
Merge pull request #6461 from blueyed/test-package-upstream
Package: typing, cleanup
2 parents ef283ef + ae5d16b commit 28b8f3c

File tree

3 files changed

+60
-70
lines changed

3 files changed

+60
-70
lines changed

src/_pytest/main.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,6 @@ def pytest_collection_modifyitems(items, config):
348348
items[:] = remaining
349349

350350

351-
class FSHookProxy:
352-
def __init__(self, fspath, pm, remove_mods):
353-
self.fspath = fspath
354-
self.pm = pm
355-
self.remove_mods = remove_mods
356-
357-
def __getattr__(self, name):
358-
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
359-
self.__dict__[name] = x
360-
return x
361-
362-
363351
class NoMatch(Exception):
364352
""" raised if matching cannot locate a matching names. """
365353

@@ -401,7 +389,6 @@ def __init__(self, config: Config) -> None:
401389
self.shouldstop = False
402390
self.shouldfail = False
403391
self.trace = config.trace.root.get("collection")
404-
self._norecursepatterns = config.getini("norecursedirs")
405392
self.startdir = config.invocation_dir
406393
self._initialpaths = frozenset() # type: FrozenSet[py.path.local]
407394

@@ -449,19 +436,8 @@ def pytest_runtest_logreport(self, report):
449436
def isinitpath(self, path):
450437
return path in self._initialpaths
451438

452-
def gethookproxy(self, fspath):
453-
# check if we have the common case of running
454-
# hooks with all conftest.py files
455-
pm = self.config.pluginmanager
456-
my_conftestmodules = pm._getconftestmodules(fspath)
457-
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
458-
if remove_mods:
459-
# one or more conftests are not in use at this fspath
460-
proxy = FSHookProxy(fspath, pm, remove_mods)
461-
else:
462-
# all plugins are active for this fspath
463-
proxy = self.config.hook
464-
return proxy
439+
def gethookproxy(self, fspath: py.path.local):
440+
return super()._gethookproxy(fspath)
465441

466442
def perform_collect(self, args=None, genitems=True):
467443
hook = self.config.hook
@@ -625,19 +601,6 @@ def _collectfile(self, path, handle_dupes=True):
625601

626602
return ihook.pytest_collect_file(path=path, parent=self)
627603

628-
def _recurse(self, dirpath):
629-
if dirpath.basename == "__pycache__":
630-
return False
631-
ihook = self.gethookproxy(dirpath.dirpath())
632-
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
633-
return False
634-
for pat in self._norecursepatterns:
635-
if dirpath.check(fnmatch=pat):
636-
return False
637-
ihook = self.gethookproxy(dirpath)
638-
ihook.pytest_collect_directory(path=dirpath, parent=self)
639-
return True
640-
641604
@staticmethod
642605
def _visit_filter(f):
643606
return f.check(file=1)

src/_pytest/nodes.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from _pytest.compat import getfslineno
2020
from _pytest.compat import TYPE_CHECKING
2121
from _pytest.config import Config
22+
from _pytest.config import PytestPluginManager
2223
from _pytest.fixtures import FixtureDef
2324
from _pytest.fixtures import FixtureLookupError
2425
from _pytest.fixtures import FixtureLookupErrorRepr
@@ -393,6 +394,20 @@ def _check_initialpaths_for_relpath(session, fspath):
393394
return fspath.relto(initial_path)
394395

395396

397+
class FSHookProxy:
398+
def __init__(
399+
self, fspath: py.path.local, pm: PytestPluginManager, remove_mods
400+
) -> None:
401+
self.fspath = fspath
402+
self.pm = pm
403+
self.remove_mods = remove_mods
404+
405+
def __getattr__(self, name: str):
406+
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
407+
self.__dict__[name] = x
408+
return x
409+
410+
396411
class FSCollector(Collector):
397412
def __init__(
398413
self, fspath: py.path.local, parent=None, config=None, session=None, nodeid=None
@@ -417,6 +432,35 @@ def __init__(
417432

418433
super().__init__(name, parent, config, session, nodeid=nodeid, fspath=fspath)
419434

435+
self._norecursepatterns = self.config.getini("norecursedirs")
436+
437+
def _gethookproxy(self, fspath: py.path.local):
438+
# check if we have the common case of running
439+
# hooks with all conftest.py files
440+
pm = self.config.pluginmanager
441+
my_conftestmodules = pm._getconftestmodules(fspath)
442+
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
443+
if remove_mods:
444+
# one or more conftests are not in use at this fspath
445+
proxy = FSHookProxy(fspath, pm, remove_mods)
446+
else:
447+
# all plugins are active for this fspath
448+
proxy = self.config.hook
449+
return proxy
450+
451+
def _recurse(self, dirpath: py.path.local) -> bool:
452+
if dirpath.basename == "__pycache__":
453+
return False
454+
ihook = self._gethookproxy(dirpath.dirpath())
455+
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
456+
return False
457+
for pat in self._norecursepatterns:
458+
if dirpath.check(fnmatch=pat):
459+
return False
460+
ihook = self._gethookproxy(dirpath)
461+
ihook.pytest_collect_directory(path=dirpath, parent=self)
462+
return True
463+
420464

421465
class File(FSCollector):
422466
""" base class for collecting tests from a file. """

src/_pytest/python.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from _pytest.compat import STRING_TYPES
3636
from _pytest.config import hookimpl
3737
from _pytest.deprecated import FUNCARGNAMES
38-
from _pytest.main import FSHookProxy
3938
from _pytest.mark import MARK_GEN
4039
from _pytest.mark.structures import get_unpacked_marks
4140
from _pytest.mark.structures import normalize_mark_list
@@ -545,15 +544,23 @@ def _importtestmodule(self):
545544

546545

547546
class Package(Module):
548-
def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
547+
def __init__(
548+
self,
549+
fspath: py.path.local,
550+
parent: nodes.Collector,
551+
# NOTE: following args are unused:
552+
config=None,
553+
session=None,
554+
nodeid=None,
555+
) -> None:
556+
# NOTE: could be just the following, but kept as-is for compat.
557+
# nodes.FSCollector.__init__(self, fspath, parent=parent)
549558
session = parent.session
550559
nodes.FSCollector.__init__(
551560
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
552561
)
562+
553563
self.name = fspath.dirname
554-
self.trace = session.trace
555-
self._norecursepatterns = session._norecursepatterns
556-
self.fspath = fspath
557564

558565
def setup(self):
559566
# not using fixtures to call setup_module here because autouse fixtures
@@ -571,32 +578,8 @@ def setup(self):
571578
func = partial(_call_with_optional_argument, teardown_module, self.obj)
572579
self.addfinalizer(func)
573580

574-
def _recurse(self, dirpath):
575-
if dirpath.basename == "__pycache__":
576-
return False
577-
ihook = self.gethookproxy(dirpath.dirpath())
578-
if ihook.pytest_ignore_collect(path=dirpath, config=self.config):
579-
return
580-
for pat in self._norecursepatterns:
581-
if dirpath.check(fnmatch=pat):
582-
return False
583-
ihook = self.gethookproxy(dirpath)
584-
ihook.pytest_collect_directory(path=dirpath, parent=self)
585-
return True
586-
587-
def gethookproxy(self, fspath):
588-
# check if we have the common case of running
589-
# hooks with all conftest.py filesall conftest.py
590-
pm = self.config.pluginmanager
591-
my_conftestmodules = pm._getconftestmodules(fspath)
592-
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
593-
if remove_mods:
594-
# one or more conftests are not in use at this fspath
595-
proxy = FSHookProxy(fspath, pm, remove_mods)
596-
else:
597-
# all plugins are active for this fspath
598-
proxy = self.config.hook
599-
return proxy
581+
def gethookproxy(self, fspath: py.path.local):
582+
return super()._gethookproxy(fspath)
600583

601584
def _collectfile(self, path, handle_dupes=True):
602585
assert (

0 commit comments

Comments
 (0)