Skip to content

typing: Testdir.__init__ #6580

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 4 commits into from
Jan 27, 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
7 changes: 7 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
from typing import Type


_PluggyPlugin = object
"""A type to represent plugin objects.
Plugins can be any namespace, so we can't narrow it down much, but we use an
alias to make the intent clear.
Ideally this type would be provided by pluggy itself."""


hookimpl = HookimplMarker("pytest")
hookspec = HookspecMarker("pytest")

Expand Down
19 changes: 14 additions & 5 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
from _pytest.capture import MultiCapture
from _pytest.capture import SysCapture
from _pytest.compat import TYPE_CHECKING
from _pytest.config import _PluggyPlugin
from _pytest.fixtures import FixtureRequest
from _pytest.main import ExitCode
from _pytest.main import Session
from _pytest.monkeypatch import MonkeyPatch
from _pytest.nodes import Collector
from _pytest.nodes import Item
from _pytest.pathlib import Path
from _pytest.python import Module
from _pytest.reports import TestReport
from _pytest.tmpdir import TempdirFactory

if TYPE_CHECKING:
from typing import Type
Expand Down Expand Up @@ -534,13 +539,15 @@ class Testdir:
class TimeoutExpired(Exception):
pass

def __init__(self, request, tmpdir_factory):
def __init__(self, request: FixtureRequest, tmpdir_factory: TempdirFactory) -> None:
self.request = request
self._mod_collections = WeakKeyDictionary()
self._mod_collections = (
WeakKeyDictionary()
) # type: WeakKeyDictionary[Module, List[Union[Item, Collector]]]
name = request.function.__name__
self.tmpdir = tmpdir_factory.mktemp(name, numbered=True)
self.test_tmproot = tmpdir_factory.mktemp("tmp-" + name, numbered=True)
self.plugins = []
self.plugins = [] # type: List[Union[str, _PluggyPlugin]]
self._cwd_snapshot = CwdSnapshot()
self._sys_path_snapshot = SysPathsSnapshot()
self._sys_modules_snapshot = self.__take_sys_modules_snapshot()
Expand Down Expand Up @@ -1064,7 +1071,9 @@ def getmodulecol(self, source, configargs=(), withinit=False):
self.config = config = self.parseconfigure(path, *configargs)
return self.getnode(config, path)

def collect_by_name(self, modcol, name):
def collect_by_name(
self, modcol: Module, name: str
) -> Optional[Union[Item, Collector]]:
"""Return the collection node for name from the module collection.

This will search a module collection node for a collection node
Expand All @@ -1073,13 +1082,13 @@ def collect_by_name(self, modcol, name):
:param modcol: a module collection node; see :py:meth:`getmodulecol`

:param name: the name of the node to return

"""
if modcol not in self._mod_collections:
self._mod_collections[modcol] = list(modcol.collect())
for colitem in self._mod_collections[modcol]:
if colitem.name == name:
return colitem
return None

def popen(
self,
Expand Down
12 changes: 8 additions & 4 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from _pytest.main import _in_venv
from _pytest.main import ExitCode
from _pytest.main import Session
from _pytest.pytester import Testdir


class TestCollector:
Expand All @@ -18,7 +19,7 @@ def test_collect_versus_item(self):
assert not issubclass(Collector, Item)
assert not issubclass(Item, Collector)

def test_check_equality(self, testdir):
def test_check_equality(self, testdir: Testdir) -> None:
modcol = testdir.getmodulecol(
"""
def test_pass(): pass
Expand All @@ -40,12 +41,15 @@ def test_fail(): assert 0
assert fn1 != fn3

for fn in fn1, fn2, fn3:
assert fn != 3
assert isinstance(fn, pytest.Function)
assert fn != 3 # type: ignore[comparison-overlap] # noqa: F821
assert fn != modcol
assert fn != [1, 2, 3]
assert [1, 2, 3] != fn
assert fn != [1, 2, 3] # type: ignore[comparison-overlap] # noqa: F821
assert [1, 2, 3] != fn # type: ignore[comparison-overlap] # noqa: F821
assert modcol != fn

assert testdir.collect_by_name(modcol, "doesnotexist") is None

def test_getparent(self, testdir):
modcol = testdir.getmodulecol(
"""
Expand Down