[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,7 +1,10 @@
# mypy: allow-untyped-defs
from __future__ import annotations
import collections.abc
import dataclasses
import inspect
import warnings
from typing import Any
from typing import Callable
from typing import Collection
@ -21,37 +24,37 @@ from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
import warnings
from .._code import getfslineno
from ..compat import ascii_escaped
from ..compat import NOTSET
from ..compat import NotSetType
from _pytest.config import Config
from _pytest.deprecated import check_ispytest
from _pytest.deprecated import MARKED_FIXTURE
from _pytest.outcomes import fail
from _pytest.warning_types import PytestUnknownMarkWarning
from .._code import getfslineno
from ..compat import ascii_escaped
from ..compat import NOTSET
from ..compat import NotSetType
if TYPE_CHECKING:
from ..nodes import Node
EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
EMPTY_PARAMETERSET_OPTION = 'empty_parameter_set_mark'
def istestfunc(func) -> bool:
return callable(func) and getattr(func, "__name__", "<lambda>") != "<lambda>"
return callable(func) and getattr(func, '__name__', '<lambda>') != '<lambda>'
def get_empty_parameterset_mark(
config: Config, argnames: Sequence[str], func
) -> "MarkDecorator":
config: Config, argnames: Sequence[str], func,
) -> MarkDecorator:
from ..nodes import Collector
fs, lineno = getfslineno(func)
reason = "got empty parameter set %r, function %s at %s:%d" % (
reason = 'got empty parameter set %r, function %s at %s:%d' % (
argnames,
func.__name__,
fs,
@ -59,15 +62,15 @@ def get_empty_parameterset_mark(
)
requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
if requested_mark in ("", None, "skip"):
if requested_mark in ('', None, 'skip'):
mark = MARK_GEN.skip(reason=reason)
elif requested_mark == "xfail":
elif requested_mark == 'xfail':
mark = MARK_GEN.xfail(reason=reason, run=False)
elif requested_mark == "fail_at_collect":
elif requested_mark == 'fail_at_collect':
f_name = func.__name__
_, lineno = getfslineno(func)
raise Collector.CollectError(
"Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
"Empty parameter set in '%s' at line %d" % (f_name, lineno + 1),
)
else:
raise LookupError(requested_mark)
@ -75,17 +78,17 @@ def get_empty_parameterset_mark(
class ParameterSet(NamedTuple):
values: Sequence[Union[object, NotSetType]]
marks: Collection[Union["MarkDecorator", "Mark"]]
id: Optional[str]
values: Sequence[object | NotSetType]
marks: Collection[MarkDecorator | Mark]
id: str | None
@classmethod
def param(
cls,
*values: object,
marks: Union["MarkDecorator", Collection[Union["MarkDecorator", "Mark"]]] = (),
id: Optional[str] = None,
) -> "ParameterSet":
marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),
id: str | None = None,
) -> ParameterSet:
if isinstance(marks, MarkDecorator):
marks = (marks,)
else:
@ -93,16 +96,16 @@ class ParameterSet(NamedTuple):
if id is not None:
if not isinstance(id, str):
raise TypeError(f"Expected id to be a string, got {type(id)}: {id!r}")
raise TypeError(f'Expected id to be a string, got {type(id)}: {id!r}')
id = ascii_escaped(id)
return cls(values, marks, id)
@classmethod
def extract_from(
cls,
parameterset: Union["ParameterSet", Sequence[object], object],
parameterset: ParameterSet | Sequence[object] | object,
force_tuple: bool = False,
) -> "ParameterSet":
) -> ParameterSet:
"""Extract from an object or objects.
:param parameterset:
@ -127,13 +130,13 @@ class ParameterSet(NamedTuple):
@staticmethod
def _parse_parametrize_args(
argnames: Union[str, Sequence[str]],
argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
argnames: str | Sequence[str],
argvalues: Iterable[ParameterSet | Sequence[object] | object],
*args,
**kwargs,
) -> Tuple[Sequence[str], bool]:
) -> tuple[Sequence[str], bool]:
if isinstance(argnames, str):
argnames = [x.strip() for x in argnames.split(",") if x.strip()]
argnames = [x.strip() for x in argnames.split(',') if x.strip()]
force_tuple = len(argnames) == 1
else:
force_tuple = False
@ -141,9 +144,9 @@ class ParameterSet(NamedTuple):
@staticmethod
def _parse_parametrize_parameters(
argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
argvalues: Iterable[ParameterSet | Sequence[object] | object],
force_tuple: bool,
) -> List["ParameterSet"]:
) -> list[ParameterSet]:
return [
ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
]
@ -151,12 +154,12 @@ class ParameterSet(NamedTuple):
@classmethod
def _for_parametrize(
cls,
argnames: Union[str, Sequence[str]],
argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
argnames: str | Sequence[str],
argvalues: Iterable[ParameterSet | Sequence[object] | object],
func,
config: Config,
nodeid: str,
) -> Tuple[Sequence[str], List["ParameterSet"]]:
) -> tuple[Sequence[str], list[ParameterSet]]:
argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
del argvalues
@ -167,9 +170,9 @@ class ParameterSet(NamedTuple):
if len(param.values) != len(argnames):
msg = (
'{nodeid}: in "parametrize" the number of names ({names_len}):\n'
" {names}\n"
"must be equal to the number of values ({values_len}):\n"
" {values}"
' {names}\n'
'must be equal to the number of values ({values_len}):\n'
' {values}'
)
fail(
msg.format(
@ -186,7 +189,7 @@ class ParameterSet(NamedTuple):
# parameter set with NOTSET values, with the "empty parameter set" mark applied to it.
mark = get_empty_parameterset_mark(config, argnames, func)
parameters.append(
ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None)
ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None),
)
return argnames, parameters
@ -199,40 +202,40 @@ class Mark:
#: Name of the mark.
name: str
#: Positional arguments of the mark decorator.
args: Tuple[Any, ...]
args: tuple[Any, ...]
#: Keyword arguments of the mark decorator.
kwargs: Mapping[str, Any]
#: Source Mark for ids with parametrize Marks.
_param_ids_from: Optional["Mark"] = dataclasses.field(default=None, repr=False)
_param_ids_from: Mark | None = dataclasses.field(default=None, repr=False)
#: Resolved/generated ids with parametrize Marks.
_param_ids_generated: Optional[Sequence[str]] = dataclasses.field(
default=None, repr=False
_param_ids_generated: Sequence[str] | None = dataclasses.field(
default=None, repr=False,
)
def __init__(
self,
name: str,
args: Tuple[Any, ...],
args: tuple[Any, ...],
kwargs: Mapping[str, Any],
param_ids_from: Optional["Mark"] = None,
param_ids_generated: Optional[Sequence[str]] = None,
param_ids_from: Mark | None = None,
param_ids_generated: Sequence[str] | None = None,
*,
_ispytest: bool = False,
) -> None:
""":meta private:"""
check_ispytest(_ispytest)
# Weirdness to bypass frozen=True.
object.__setattr__(self, "name", name)
object.__setattr__(self, "args", args)
object.__setattr__(self, "kwargs", kwargs)
object.__setattr__(self, "_param_ids_from", param_ids_from)
object.__setattr__(self, "_param_ids_generated", param_ids_generated)
object.__setattr__(self, 'name', name)
object.__setattr__(self, 'args', args)
object.__setattr__(self, 'kwargs', kwargs)
object.__setattr__(self, '_param_ids_from', param_ids_from)
object.__setattr__(self, '_param_ids_generated', param_ids_generated)
def _has_param_ids(self) -> bool:
return "ids" in self.kwargs or len(self.args) >= 4
return 'ids' in self.kwargs or len(self.args) >= 4
def combined_with(self, other: "Mark") -> "Mark":
def combined_with(self, other: Mark) -> Mark:
"""Return a new Mark which is a combination of this
Mark and another Mark.
@ -244,8 +247,8 @@ class Mark:
assert self.name == other.name
# Remember source of ids with parametrize Marks.
param_ids_from: Optional[Mark] = None
if self.name == "parametrize":
param_ids_from: Mark | None = None
if self.name == 'parametrize':
if other._has_param_ids():
param_ids_from = other
elif self._has_param_ids():
@ -263,7 +266,7 @@ class Mark:
# A generic parameter designating an object to which a Mark may
# be applied -- a test function (callable) or class.
# Note: a lambda is not allowed, but this can't be represented.
Markable = TypeVar("Markable", bound=Union[Callable[..., object], type])
Markable = TypeVar('Markable', bound=Union[Callable[..., object], type])
@dataclasses.dataclass
@ -315,7 +318,7 @@ class MarkDecorator:
return self.mark.name
@property
def args(self) -> Tuple[Any, ...]:
def args(self) -> tuple[Any, ...]:
"""Alias for mark.args."""
return self.mark.args
@ -329,7 +332,7 @@ class MarkDecorator:
""":meta private:"""
return self.name # for backward-compat (2.4.1 had this attr)
def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
def with_args(self, *args: object, **kwargs: object) -> MarkDecorator:
"""Return a MarkDecorator with extra arguments added.
Unlike calling the MarkDecorator, with_args() can be used even
@ -346,7 +349,7 @@ class MarkDecorator:
pass
@overload
def __call__(self, *args: object, **kwargs: object) -> "MarkDecorator":
def __call__(self, *args: object, **kwargs: object) -> MarkDecorator:
pass
def __call__(self, *args: object, **kwargs: object):
@ -361,10 +364,10 @@ class MarkDecorator:
def get_unpacked_marks(
obj: Union[object, type],
obj: object | type,
*,
consider_mro: bool = True,
) -> List[Mark]:
) -> list[Mark]:
"""Obtain the unpacked marks that are stored on an object.
If obj is a class and consider_mro is true, return marks applied to
@ -373,10 +376,10 @@ def get_unpacked_marks(
"""
if isinstance(obj, type):
if not consider_mro:
mark_lists = [obj.__dict__.get("pytestmark", [])]
mark_lists = [obj.__dict__.get('pytestmark', [])]
else:
mark_lists = [
x.__dict__.get("pytestmark", []) for x in reversed(obj.__mro__)
x.__dict__.get('pytestmark', []) for x in reversed(obj.__mro__)
]
mark_list = []
for item in mark_lists:
@ -385,7 +388,7 @@ def get_unpacked_marks(
else:
mark_list.append(item)
else:
mark_attribute = getattr(obj, "pytestmark", [])
mark_attribute = getattr(obj, 'pytestmark', [])
if isinstance(mark_attribute, list):
mark_list = mark_attribute
else:
@ -394,7 +397,7 @@ def get_unpacked_marks(
def normalize_mark_list(
mark_list: Iterable[Union[Mark, MarkDecorator]],
mark_list: Iterable[Mark | MarkDecorator],
) -> Iterable[Mark]:
"""
Normalize an iterable of Mark or MarkDecorator objects into a list of marks
@ -404,9 +407,9 @@ def normalize_mark_list(
:returns: A new list of the extracted Mark objects
"""
for mark in mark_list:
mark_obj = getattr(mark, "mark", mark)
mark_obj = getattr(mark, 'mark', mark)
if not isinstance(mark_obj, Mark):
raise TypeError(f"got {mark_obj!r} instead of Mark")
raise TypeError(f'got {mark_obj!r} instead of Mark')
yield mark_obj
@ -438,14 +441,14 @@ if TYPE_CHECKING:
...
@overload
def __call__(self, reason: str = ...) -> "MarkDecorator":
def __call__(self, reason: str = ...) -> MarkDecorator:
...
class _SkipifMarkDecorator(MarkDecorator):
def __call__( # type: ignore[override]
self,
condition: Union[str, bool] = ...,
*conditions: Union[str, bool],
condition: str | bool = ...,
*conditions: str | bool,
reason: str = ...,
) -> MarkDecorator:
...
@ -458,13 +461,13 @@ if TYPE_CHECKING:
@overload
def __call__(
self,
condition: Union[str, bool] = False,
*conditions: Union[str, bool],
condition: str | bool = False,
*conditions: str | bool,
reason: str = ...,
run: bool = ...,
raises: Union[
None, Type[BaseException], Tuple[Type[BaseException], ...]
] = ...,
raises: (
None | type[BaseException] | tuple[type[BaseException], ...]
) = ...,
strict: bool = ...,
) -> MarkDecorator:
...
@ -472,17 +475,15 @@ if TYPE_CHECKING:
class _ParametrizeMarkDecorator(MarkDecorator):
def __call__( # type: ignore[override]
self,
argnames: Union[str, Sequence[str]],
argvalues: Iterable[Union[ParameterSet, Sequence[object], object]],
argnames: str | Sequence[str],
argvalues: Iterable[ParameterSet | Sequence[object] | object],
*,
indirect: Union[bool, Sequence[str]] = ...,
ids: Optional[
Union[
Iterable[Union[None, str, float, int, bool]],
Callable[[Any], Optional[object]],
]
] = ...,
scope: Optional[_ScopeName] = ...,
indirect: bool | Sequence[str] = ...,
ids: None | (
Iterable[None | str | float | int | bool] |
Callable[[Any], object | None]
) = ...,
scope: _ScopeName | None = ...,
) -> MarkDecorator:
...
@ -523,24 +524,24 @@ class MarkGenerator:
def __init__(self, *, _ispytest: bool = False) -> None:
check_ispytest(_ispytest)
self._config: Optional[Config] = None
self._markers: Set[str] = set()
self._config: Config | None = None
self._markers: set[str] = set()
def __getattr__(self, name: str) -> MarkDecorator:
"""Generate a new :class:`MarkDecorator` with the given name."""
if name[0] == "_":
raise AttributeError("Marker name must NOT start with underscore")
if name[0] == '_':
raise AttributeError('Marker name must NOT start with underscore')
if self._config is not None:
# We store a set of markers as a performance optimisation - if a mark
# name is in the set we definitely know it, but a mark may be known and
# not in the set. We therefore start by updating the set!
if name not in self._markers:
for line in self._config.getini("markers"):
for line in self._config.getini('markers'):
# example lines: "skipif(condition): skip the given test if..."
# or "hypothesis: tests which use Hypothesis", so to get the
# marker name we split on both `:` and `(`.
marker = line.split(":")[0].split("(")[0].strip()
marker = line.split(':')[0].split('(')[0].strip()
self._markers.add(marker)
# If the name is not in the set of known marks after updating,
@ -548,19 +549,19 @@ class MarkGenerator:
if name not in self._markers:
if self._config.option.strict_markers or self._config.option.strict:
fail(
f"{name!r} not found in `markers` configuration option",
f'{name!r} not found in `markers` configuration option',
pytrace=False,
)
# Raise a specific error for common misspellings of "parametrize".
if name in ["parameterize", "parametrise", "parameterise"]:
if name in ['parameterize', 'parametrise', 'parameterise']:
__tracebackhide__ = True
fail(f"Unknown '{name}' mark, did you mean 'parametrize'?")
warnings.warn(
"Unknown pytest.mark.%s - is this a typo? You can register "
"custom marks to avoid this warning - for details, see "
"https://docs.pytest.org/en/stable/how-to/mark.html" % name,
'Unknown pytest.mark.%s - is this a typo? You can register '
'custom marks to avoid this warning - for details, see '
'https://docs.pytest.org/en/stable/how-to/mark.html' % name,
PytestUnknownMarkWarning,
2,
)
@ -573,9 +574,9 @@ MARK_GEN = MarkGenerator(_ispytest=True)
@final
class NodeKeywords(MutableMapping[str, Any]):
__slots__ = ("node", "parent", "_markers")
__slots__ = ('node', 'parent', '_markers')
def __init__(self, node: "Node") -> None:
def __init__(self, node: Node) -> None:
self.node = node
self.parent = node.parent
self._markers = {node.name: True}
@ -596,21 +597,21 @@ class NodeKeywords(MutableMapping[str, Any]):
def __contains__(self, key: object) -> bool:
return (
key in self._markers
or self.parent is not None
and key in self.parent.keywords
key in self._markers or
self.parent is not None and
key in self.parent.keywords
)
def update( # type: ignore[override]
self,
other: Union[Mapping[str, Any], Iterable[Tuple[str, Any]]] = (),
other: Mapping[str, Any] | Iterable[tuple[str, Any]] = (),
**kwds: Any,
) -> None:
self._markers.update(other)
self._markers.update(kwds)
def __delitem__(self, key: str) -> None:
raise ValueError("cannot delete key in keywords dict")
raise ValueError('cannot delete key in keywords dict')
def __iter__(self) -> Iterator[str]:
# Doesn't need to be fast.
@ -626,4 +627,4 @@ class NodeKeywords(MutableMapping[str, Any]):
return sum(1 for keyword in self)
def __repr__(self) -> str:
return f"<NodeKeywords for node {self.node}>"
return f'<NodeKeywords for node {self.node}>'