Skip to content
Open
38 changes: 10 additions & 28 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import enum
from functools import lru_cache
import glob
import importlib
import importlib.metadata
import inspect
import os
Expand Down Expand Up @@ -55,6 +56,7 @@
from _pytest._code.code import TracebackStyle
from _pytest._io import TerminalWriter
from _pytest.compat import assert_never
from _pytest.compat import NOTSET
from _pytest.config.argparsing import Argument
from _pytest.config.argparsing import FILE_OR_DIR
from _pytest.config.argparsing import Parser
Expand Down Expand Up @@ -873,7 +875,7 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No
return

try:
__import__(importspec)
mod = importlib.import_module(importspec)
except ImportError as e:
raise ImportError(
f'Error importing plugin "{modname}": {e.args[0]}'
Expand All @@ -882,7 +884,6 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No
except Skipped as e:
self.skipped_plugins.append((modname, e.msg or ""))
else:
mod = sys.modules[importspec]
self.register(mod, modname)


Expand All @@ -907,14 +908,6 @@ def _get_plugin_specs_as_list(
)


class Notset:
def __repr__(self):
return "<NOTSET>"


notset = Notset()


def _iter_rewritable_modules(package_files: Iterable[str]) -> Iterator[str]:
"""Given an iterable of file names in a source distribution, return the "names" that should
be marked for assertion rewrite.
Expand Down Expand Up @@ -1090,7 +1083,6 @@ def __init__(
self.trace = self.pluginmanager.trace.root.get("config")
self.hook: pluggy.HookRelay = PathAwareHookProxy(self.pluginmanager.hook) # type: ignore[assignment]
self._inicache: dict[str, Any] = {}
self._opt2dest: dict[str, str] = {}
self._cleanup_stack = contextlib.ExitStack()
self.pluginmanager.register(self, "pytestconfig")
self._configured = False
Expand Down Expand Up @@ -1215,12 +1207,8 @@ def fromdictargs(cls, option_dict: Mapping[str, Any], args: list[str]) -> Config
return config

def _processopt(self, opt: Argument) -> None:
for name in opt._short_opts + opt._long_opts:
self._opt2dest[name] = opt.dest

if hasattr(opt, "default"):
if not hasattr(self.option, opt.dest):
setattr(self.option, opt.dest, opt.default)
if not hasattr(self.option, opt.dest):
setattr(self.option, opt.dest, opt.default)

@hookimpl(trylast=True)
def pytest_load_initial_conftests(self, early_config: Config) -> None:
Expand Down Expand Up @@ -1376,17 +1364,11 @@ def pytest_collection(self) -> Generator[None, object, object]:
def _checkversion(self) -> None:
import pytest

minver_ini_value = self.inicfg.get("minversion", None)
minver = minver_ini_value.value if minver_ini_value is not None else None
minver = self.getini("minversion")
if minver:
# Imported lazily to improve start-up time.
from packaging.version import Version

if not isinstance(minver, str):
raise pytest.UsageError(
f"{self.inipath}: 'minversion' must be a single value"
)

if Version(minver) > Version(pytest.__version__):
raise pytest.UsageError(
f"{self.inipath}: 'minversion' requires pytest-{minver}, actual pytest-{pytest.__version__}'"
Expand Down Expand Up @@ -1840,7 +1822,7 @@ def _getconftest_pathlist(
values.append(relroot)
return values

def getoption(self, name: str, default: Any = notset, skip: bool = False):
def getoption(self, name: str, default: Any = NOTSET, skip: bool = False):
"""Return command line option value.

:param name: Name of the option. You may also specify
Expand All @@ -1850,14 +1832,14 @@ def getoption(self, name: str, default: Any = notset, skip: bool = False):
:param skip: If ``True``, raise :func:`pytest.skip` if option is undeclared or has a ``None`` value.
Note that even if ``True``, if a default was specified it will be returned instead of a skip.
"""
name = self._opt2dest.get(name, name)
name = self._parser._opt2dest.get(name, name)
try:
val = getattr(self.option, name)
if val is None and skip:
raise AttributeError(name)
return val
except AttributeError as e:
if default is not notset:
if default is not NOTSET:
return default
if skip:
import pytest
Expand Down Expand Up @@ -2134,7 +2116,7 @@ def _resolve_warning_category(category: str) -> type[Warning]:
klass = category
else:
module, _, klass = category.rpartition(".")
m = __import__(module, None, None, [klass])
m = importlib.import_module(module)
cat = getattr(m, klass)
if not issubclass(cat, Warning):
raise UsageError(f"{cat} is not a Warning subclass")
Expand Down
Loading
Loading