Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/_pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
from _pytest.assertion import util
from _pytest.compat import TYPE_CHECKING
from _pytest.config import hookimpl
from _pytest.config.argparsing import Parser

if TYPE_CHECKING:
from _pytest.main import Session


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("debugconfig")
group.addoption(
"--assert",
Expand Down Expand Up @@ -162,7 +163,7 @@ def call_assertion_pass_hook(lineno, orig, expl):
util._reprcompare, util._assertion_pass = saved_assert_hooks


def pytest_sessionfinish(session):
def pytest_sessionfinish(session: "Session") -> None:
assertstate = getattr(session.config, "_assertstate", None)
if assertstate:
if assertstate.hook is not None:
Expand Down
16 changes: 11 additions & 5 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import List
from typing import Optional
from typing import Set
from typing import Union

import attr
import py
Expand All @@ -24,6 +25,8 @@
from _pytest import nodes
from _pytest._io import TerminalWriter
from _pytest.config import Config
from _pytest.config import ExitCode
from _pytest.config.argparsing import Parser
from _pytest.main import Session
from _pytest.python import Module

Expand Down Expand Up @@ -316,11 +319,12 @@ def pytest_collection_modifyitems(self, session, config, items):
else:
self._report_status += "not deselecting items."

def pytest_sessionfinish(self, session):
def pytest_sessionfinish(self, session: Session) -> None:
config = self.config
if config.getoption("cacheshow") or hasattr(config, "slaveinput"):
return

assert config.cache is not None
saved_lastfailed = config.cache.get("cache/lastfailed", {})
if saved_lastfailed != self.lastfailed:
config.cache.set("cache/lastfailed", self.lastfailed)
Expand Down Expand Up @@ -358,15 +362,15 @@ def pytest_collection_modifyitems(
def _get_increasing_order(self, items):
return sorted(items, key=lambda item: item.fspath.mtime(), reverse=True)

def pytest_sessionfinish(self, session):
def pytest_sessionfinish(self, session: Session) -> None:
config = self.config
if config.getoption("cacheshow") or hasattr(config, "slaveinput"):
return

config.cache.set("cache/nodeids", self.cached_nodeids)


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("general")
group.addoption(
"--lf",
Expand Down Expand Up @@ -424,16 +428,18 @@ def pytest_addoption(parser):
)


def pytest_cmdline_main(config):
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
if config.option.cacheshow:
from _pytest.main import wrap_session

return wrap_session(config, cacheshow)
return None


@pytest.hookimpl(tryfirst=True)
def pytest_configure(config: Config) -> None:
config.cache = Cache.for_config(config)
# Type ignored: pending mechanism to store typed objects scoped to config.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats in several plugins. I have a simple plan for how to do it, but for now I added type-ignores.

config.cache = Cache.for_config(config) # type: ignore # noqa: F821
config.pluginmanager.register(LFPlugin(config), "lfplugin")
config.pluginmanager.register(NFPlugin(config), "nfplugin")

Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from _pytest.compat import CaptureIO
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.fixtures import FixtureRequest

if TYPE_CHECKING:
Expand All @@ -29,7 +30,7 @@
patchsysdict = {0: "stdin", 1: "stdout", 2: "stderr"}


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("general")
group._addoption(
"--capture",
Expand Down
6 changes: 4 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def hasplugin(self, name):
"""Return True if the plugin with the given name is registered."""
return bool(self.get_plugin(name))

def pytest_configure(self, config):
def pytest_configure(self, config: "Config") -> None:
# XXX now that the pluginmanager exposes hookimpl(tryfirst...)
# we should remove tryfirst/trylast as markers
config.addinivalue_line(
Expand Down Expand Up @@ -831,7 +831,9 @@ def _ensure_unconfigure(self):
def get_terminal_writer(self):
return self.pluginmanager.get_plugin("terminalreporter")._tw

def pytest_cmdline_parse(self, pluginmanager, args):
def pytest_cmdline_parse(
self, pluginmanager: PytestPluginManager, args: List[str]
) -> object:
try:
self.parse(args)
except UsageError:
Expand Down
11 changes: 7 additions & 4 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import sys

from _pytest import outcomes
from _pytest.config import Config
from _pytest.config import hookimpl
from _pytest.config import PytestPluginManager
from _pytest.config.argparsing import Parser
from _pytest.config.exceptions import UsageError


Expand All @@ -19,7 +22,7 @@ def _validate_usepdb_cls(value):
return (modname, classname)


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("general")
group._addoption(
"--pdb",
Expand All @@ -43,7 +46,7 @@ def pytest_addoption(parser):
)


def pytest_configure(config):
def pytest_configure(config: Config) -> None:
import pdb

if config.getvalue("trace"):
Expand Down Expand Up @@ -73,8 +76,8 @@ def fin():
class pytestPDB:
""" Pseudo PDB that defers to the real pdb. """

_pluginmanager = None
_config = None
_pluginmanager = None # type: PytestPluginManager
_config = None # type: Config
_saved = [] # type: list
_recursive_debug = 0
_wrapped_pdb_cls = None
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from _pytest._io import TerminalWriter
from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.config.argparsing import Parser
from _pytest.fixtures import FixtureRequest
from _pytest.outcomes import OutcomeException
from _pytest.python_api import approx
Expand Down Expand Up @@ -52,7 +53,7 @@
CHECKER_CLASS = None # type: Optional[Type[doctest.OutputChecker]]


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
parser.addini(
"doctest_optionflags",
"option flags for doctests",
Expand Down Expand Up @@ -102,7 +103,7 @@ def pytest_addoption(parser):
)


def pytest_unconfigure():
def pytest_unconfigure() -> None:
global RUNNER_CLASS

RUNNER_CLASS = None
Expand Down
20 changes: 12 additions & 8 deletions src/_pytest/faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import sys

import pytest
from _pytest.config import Config
from _pytest.config.argparsing import Parser


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
help = (
"Dump the traceback of all threads if a test takes "
"more than TIMEOUT seconds to finish.\n"
Expand All @@ -14,7 +16,7 @@ def pytest_addoption(parser):
parser.addini("faulthandler_timeout", help, default=0.0)


def pytest_configure(config):
def pytest_configure(config: Config) -> None:
import faulthandler

if not faulthandler.is_enabled():
Expand Down Expand Up @@ -42,23 +44,25 @@ class FaultHandlerHooks:
"""Implements hooks that will actually install fault handler before tests execute,
as well as correctly handle pdb and internal errors."""

def pytest_configure(self, config):
def pytest_configure(self, config: Config) -> None:
import faulthandler

stderr_fd_copy = os.dup(self._get_stderr_fileno())
config.fault_handler_stderr = os.fdopen(stderr_fd_copy, "w")
faulthandler.enable(file=config.fault_handler_stderr)
fault_handler_stderr = os.fdopen(stderr_fd_copy, "w")
# Type ignored: pending mechanism to store typed objects scoped to config.
config.fault_handler_stderr = fault_handler_stderr # type: ignore # noqa: F821
faulthandler.enable(file=fault_handler_stderr)

def pytest_unconfigure(self, config):
def pytest_unconfigure(self, config: Config) -> None:
import faulthandler

faulthandler.disable()
# close our dup file installed during pytest_configure
# re-enable the faulthandler, attaching it to the default sys.stderr
# so we can see crashes after pytest has finished, usually during
# garbage collection during interpreter shutdown
config.fault_handler_stderr.close()
del config.fault_handler_stderr
config.fault_handler_stderr.close() # type: ignore[attr-defined] # noqa: F821
del config.fault_handler_stderr # type: ignore[attr-defined] # noqa: F821
faulthandler.enable(file=self._get_stderr_fileno())

@staticmethod
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from _pytest.compat import NOTSET
from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.config.argparsing import Parser
from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS
from _pytest.deprecated import FUNCARGNAMES
from _pytest.mark import ParameterSet
Expand All @@ -47,7 +48,7 @@ class PseudoFixtureDef:
scope = attr.ib()


def pytest_sessionstart(session: "Session"):
def pytest_sessionstart(session: "Session") -> None:
import _pytest.python
import _pytest.nodes

Expand Down Expand Up @@ -1200,7 +1201,7 @@ def test_foo(pytestconfig):
return request.config


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
parser.addini(
"usefixtures",
type="args",
Expand Down
14 changes: 10 additions & 4 deletions src/_pytest/helpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
import os
import sys
from argparse import Action
from typing import Optional
from typing import Union

import py

import pytest
from _pytest.config import Config
from _pytest.config import ExitCode
from _pytest.config import PrintHelp
from _pytest.config.argparsing import Parser


class HelpAction(Action):
Expand Down Expand Up @@ -36,7 +41,7 @@ def __call__(self, parser, namespace, values, option_string=None):
raise PrintHelp


def pytest_addoption(parser):
def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("debugconfig")
group.addoption(
"--version",
Expand Down Expand Up @@ -106,7 +111,7 @@ def pytest_cmdline_parse():
undo_tracing = config.pluginmanager.enable_tracing()
sys.stderr.write("writing pytestdebug information to %s\n" % path)

def unset_tracing():
def unset_tracing() -> None:
debugfile.close()
sys.stderr.write("wrote pytestdebug information to %s\n" % debugfile.name)
config.trace.root.setwriter(None)
Expand All @@ -127,7 +132,7 @@ def showversion(config):
sys.stderr.write(line + "\n")


def pytest_cmdline_main(config):
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
if config.option.version:
showversion(config)
return 0
Expand All @@ -136,9 +141,10 @@ def pytest_cmdline_main(config):
showhelp(config)
config._ensure_unconfigure()
return 0
return None


def showhelp(config):
def showhelp(config: Config) -> None:
import textwrap

reporter = config.pluginmanager.get_plugin("terminalreporter")
Expand Down
Loading