structures.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. # mypy: allow-untyped-defs
  2. from __future__ import annotations
  3. import collections.abc
  4. from collections.abc import Callable
  5. from collections.abc import Collection
  6. from collections.abc import Iterable
  7. from collections.abc import Iterator
  8. from collections.abc import Mapping
  9. from collections.abc import MutableMapping
  10. from collections.abc import Sequence
  11. import dataclasses
  12. import enum
  13. import inspect
  14. from typing import Any
  15. from typing import final
  16. from typing import NamedTuple
  17. from typing import overload
  18. from typing import TYPE_CHECKING
  19. from typing import TypeVar
  20. import warnings
  21. from .._code import getfslineno
  22. from ..compat import NOTSET
  23. from ..compat import NotSetType
  24. from _pytest.config import Config
  25. from _pytest.deprecated import check_ispytest
  26. from _pytest.deprecated import MARKED_FIXTURE
  27. from _pytest.outcomes import fail
  28. from _pytest.raises import AbstractRaises
  29. from _pytest.scope import _ScopeName
  30. from _pytest.warning_types import PytestUnknownMarkWarning
  31. if TYPE_CHECKING:
  32. from ..nodes import Node
  33. EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
  34. # Singleton type for HIDDEN_PARAM, as described in:
  35. # https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions
  36. class _HiddenParam(enum.Enum):
  37. token = 0
  38. #: Can be used as a parameter set id to hide it from the test name.
  39. HIDDEN_PARAM = _HiddenParam.token
  40. def istestfunc(func) -> bool:
  41. return callable(func) and getattr(func, "__name__", "<lambda>") != "<lambda>"
  42. def get_empty_parameterset_mark(
  43. config: Config, argnames: Sequence[str], func
  44. ) -> MarkDecorator:
  45. from ..nodes import Collector
  46. argslisting = ", ".join(argnames)
  47. _fs, lineno = getfslineno(func)
  48. reason = f"got empty parameter set for ({argslisting})"
  49. requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
  50. if requested_mark in ("", None, "skip"):
  51. mark = MARK_GEN.skip(reason=reason)
  52. elif requested_mark == "xfail":
  53. mark = MARK_GEN.xfail(reason=reason, run=False)
  54. elif requested_mark == "fail_at_collect":
  55. raise Collector.CollectError(
  56. f"Empty parameter set in '{func.__name__}' at line {lineno + 1}"
  57. )
  58. else:
  59. raise LookupError(requested_mark)
  60. return mark
  61. class ParameterSet(NamedTuple):
  62. """A set of values for a set of parameters along with associated marks and
  63. an optional ID for the set.
  64. Examples::
  65. pytest.param(1, 2, 3)
  66. # ParameterSet(values=(1, 2, 3), marks=(), id=None)
  67. pytest.param("hello", id="greeting")
  68. # ParameterSet(values=("hello",), marks=(), id="greeting")
  69. # Parameter set with marks
  70. pytest.param(42, marks=pytest.mark.xfail)
  71. # ParameterSet(values=(42,), marks=(MarkDecorator(...),), id=None)
  72. # From parametrize mark (parameter names + list of parameter sets)
  73. pytest.mark.parametrize(
  74. ("a", "b", "expected"),
  75. [
  76. (1, 2, 3),
  77. pytest.param(40, 2, 42, id="everything"),
  78. ],
  79. )
  80. # ParameterSet(values=(1, 2, 3), marks=(), id=None)
  81. # ParameterSet(values=(40, 2, 42), marks=(), id="everything")
  82. """
  83. values: Sequence[object | NotSetType]
  84. marks: Collection[MarkDecorator | Mark]
  85. id: str | _HiddenParam | None
  86. @classmethod
  87. def param(
  88. cls,
  89. *values: object,
  90. marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),
  91. id: str | _HiddenParam | None = None,
  92. ) -> ParameterSet:
  93. if isinstance(marks, MarkDecorator):
  94. marks = (marks,)
  95. else:
  96. assert isinstance(marks, collections.abc.Collection)
  97. if any(i.name == "usefixtures" for i in marks):
  98. raise ValueError(
  99. "pytest.param cannot add pytest.mark.usefixtures; see "
  100. "https://docs.pytest.org/en/stable/reference/reference.html#pytest-param"
  101. )
  102. if id is not None:
  103. if not isinstance(id, str) and id is not HIDDEN_PARAM:
  104. raise TypeError(
  105. "Expected id to be a string or a `pytest.HIDDEN_PARAM` sentinel, "
  106. f"got {type(id)}: {id!r}",
  107. )
  108. return cls(values, marks, id)
  109. @classmethod
  110. def extract_from(
  111. cls,
  112. parameterset: ParameterSet | Sequence[object] | object,
  113. force_tuple: bool = False,
  114. ) -> ParameterSet:
  115. """Extract from an object or objects.
  116. :param parameterset:
  117. A legacy style parameterset that may or may not be a tuple,
  118. and may or may not be wrapped into a mess of mark objects.
  119. :param force_tuple:
  120. Enforce tuple wrapping so single argument tuple values
  121. don't get decomposed and break tests.
  122. """
  123. if isinstance(parameterset, cls):
  124. return parameterset
  125. if force_tuple:
  126. return cls.param(parameterset)
  127. else:
  128. # TODO: Refactor to fix this type-ignore. Currently the following
  129. # passes type-checking but crashes:
  130. #
  131. # @pytest.mark.parametrize(('x', 'y'), [1, 2])
  132. # def test_foo(x, y): pass
  133. return cls(parameterset, marks=[], id=None) # type: ignore[arg-type]
  134. @staticmethod
  135. def _parse_parametrize_args(
  136. argnames: str | Sequence[str],
  137. argvalues: Iterable[ParameterSet | Sequence[object] | object],
  138. *args,
  139. **kwargs,
  140. ) -> tuple[Sequence[str], bool]:
  141. if isinstance(argnames, str):
  142. argnames = [x.strip() for x in argnames.split(",") if x.strip()]
  143. force_tuple = len(argnames) == 1
  144. else:
  145. force_tuple = False
  146. return argnames, force_tuple
  147. @staticmethod
  148. def _parse_parametrize_parameters(
  149. argvalues: Iterable[ParameterSet | Sequence[object] | object],
  150. force_tuple: bool,
  151. ) -> list[ParameterSet]:
  152. return [
  153. ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
  154. ]
  155. @classmethod
  156. def _for_parametrize(
  157. cls,
  158. argnames: str | Sequence[str],
  159. argvalues: Iterable[ParameterSet | Sequence[object] | object],
  160. func,
  161. config: Config,
  162. nodeid: str,
  163. ) -> tuple[Sequence[str], list[ParameterSet]]:
  164. argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
  165. parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
  166. del argvalues
  167. if parameters:
  168. # Check all parameter sets have the correct number of values.
  169. for param in parameters:
  170. if len(param.values) != len(argnames):
  171. msg = (
  172. '{nodeid}: in "parametrize" the number of names ({names_len}):\n'
  173. " {names}\n"
  174. "must be equal to the number of values ({values_len}):\n"
  175. " {values}"
  176. )
  177. fail(
  178. msg.format(
  179. nodeid=nodeid,
  180. values=param.values,
  181. names=argnames,
  182. names_len=len(argnames),
  183. values_len=len(param.values),
  184. ),
  185. pytrace=False,
  186. )
  187. else:
  188. # Empty parameter set (likely computed at runtime): create a single
  189. # parameter set with NOTSET values, with the "empty parameter set" mark applied to it.
  190. mark = get_empty_parameterset_mark(config, argnames, func)
  191. parameters.append(
  192. ParameterSet(
  193. values=(NOTSET,) * len(argnames), marks=[mark], id="NOTSET"
  194. )
  195. )
  196. return argnames, parameters
  197. @final
  198. @dataclasses.dataclass(frozen=True)
  199. class Mark:
  200. """A pytest mark."""
  201. #: Name of the mark.
  202. name: str
  203. #: Positional arguments of the mark decorator.
  204. args: tuple[Any, ...]
  205. #: Keyword arguments of the mark decorator.
  206. kwargs: Mapping[str, Any]
  207. #: Source Mark for ids with parametrize Marks.
  208. _param_ids_from: Mark | None = dataclasses.field(default=None, repr=False)
  209. #: Resolved/generated ids with parametrize Marks.
  210. _param_ids_generated: Sequence[str] | None = dataclasses.field(
  211. default=None, repr=False
  212. )
  213. def __init__(
  214. self,
  215. name: str,
  216. args: tuple[Any, ...],
  217. kwargs: Mapping[str, Any],
  218. param_ids_from: Mark | None = None,
  219. param_ids_generated: Sequence[str] | None = None,
  220. *,
  221. _ispytest: bool = False,
  222. ) -> None:
  223. """:meta private:"""
  224. check_ispytest(_ispytest)
  225. # Weirdness to bypass frozen=True.
  226. object.__setattr__(self, "name", name)
  227. object.__setattr__(self, "args", args)
  228. object.__setattr__(self, "kwargs", kwargs)
  229. object.__setattr__(self, "_param_ids_from", param_ids_from)
  230. object.__setattr__(self, "_param_ids_generated", param_ids_generated)
  231. def _has_param_ids(self) -> bool:
  232. return "ids" in self.kwargs or len(self.args) >= 4
  233. def combined_with(self, other: Mark) -> Mark:
  234. """Return a new Mark which is a combination of this
  235. Mark and another Mark.
  236. Combines by appending args and merging kwargs.
  237. :param Mark other: The mark to combine with.
  238. :rtype: Mark
  239. """
  240. assert self.name == other.name
  241. # Remember source of ids with parametrize Marks.
  242. param_ids_from: Mark | None = None
  243. if self.name == "parametrize":
  244. if other._has_param_ids():
  245. param_ids_from = other
  246. elif self._has_param_ids():
  247. param_ids_from = self
  248. return Mark(
  249. self.name,
  250. self.args + other.args,
  251. dict(self.kwargs, **other.kwargs),
  252. param_ids_from=param_ids_from,
  253. _ispytest=True,
  254. )
  255. # A generic parameter designating an object to which a Mark may
  256. # be applied -- a test function (callable) or class.
  257. # Note: a lambda is not allowed, but this can't be represented.
  258. Markable = TypeVar("Markable", bound=Callable[..., object] | type)
  259. @dataclasses.dataclass
  260. class MarkDecorator:
  261. """A decorator for applying a mark on test functions and classes.
  262. ``MarkDecorators`` are created with ``pytest.mark``::
  263. mark1 = pytest.mark.NAME # Simple MarkDecorator
  264. mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecorator
  265. and can then be applied as decorators to test functions::
  266. @mark2
  267. def test_function():
  268. pass
  269. When a ``MarkDecorator`` is called, it does the following:
  270. 1. If called with a single class as its only positional argument and no
  271. additional keyword arguments, it attaches the mark to the class so it
  272. gets applied automatically to all test cases found in that class.
  273. 2. If called with a single function as its only positional argument and
  274. no additional keyword arguments, it attaches the mark to the function,
  275. containing all the arguments already stored internally in the
  276. ``MarkDecorator``.
  277. 3. When called in any other case, it returns a new ``MarkDecorator``
  278. instance with the original ``MarkDecorator``'s content updated with
  279. the arguments passed to this call.
  280. Note: The rules above prevent a ``MarkDecorator`` from storing only a
  281. single function or class reference as its positional argument with no
  282. additional keyword or positional arguments. You can work around this by
  283. using `with_args()`.
  284. """
  285. mark: Mark
  286. def __init__(self, mark: Mark, *, _ispytest: bool = False) -> None:
  287. """:meta private:"""
  288. check_ispytest(_ispytest)
  289. self.mark = mark
  290. @property
  291. def name(self) -> str:
  292. """Alias for mark.name."""
  293. return self.mark.name
  294. @property
  295. def args(self) -> tuple[Any, ...]:
  296. """Alias for mark.args."""
  297. return self.mark.args
  298. @property
  299. def kwargs(self) -> Mapping[str, Any]:
  300. """Alias for mark.kwargs."""
  301. return self.mark.kwargs
  302. @property
  303. def markname(self) -> str:
  304. """:meta private:"""
  305. return self.name # for backward-compat (2.4.1 had this attr)
  306. def with_args(self, *args: object, **kwargs: object) -> MarkDecorator:
  307. """Return a MarkDecorator with extra arguments added.
  308. Unlike calling the MarkDecorator, with_args() can be used even
  309. if the sole argument is a callable/class.
  310. """
  311. mark = Mark(self.name, args, kwargs, _ispytest=True)
  312. return MarkDecorator(self.mark.combined_with(mark), _ispytest=True)
  313. # Type ignored because the overloads overlap with an incompatible
  314. # return type. Not much we can do about that. Thankfully mypy picks
  315. # the first match so it works out even if we break the rules.
  316. @overload
  317. def __call__(self, arg: Markable) -> Markable: # type: ignore[overload-overlap]
  318. pass
  319. @overload
  320. def __call__(self, *args: object, **kwargs: object) -> MarkDecorator:
  321. pass
  322. def __call__(self, *args: object, **kwargs: object):
  323. """Call the MarkDecorator."""
  324. if args and not kwargs:
  325. func = args[0]
  326. is_class = inspect.isclass(func)
  327. # For staticmethods/classmethods, the marks are eventually fetched from the
  328. # function object, not the descriptor, so unwrap.
  329. unwrapped_func = func
  330. if isinstance(func, staticmethod | classmethod):
  331. unwrapped_func = func.__func__
  332. if len(args) == 1 and (istestfunc(unwrapped_func) or is_class):
  333. store_mark(unwrapped_func, self.mark, stacklevel=3)
  334. return func
  335. return self.with_args(*args, **kwargs)
  336. def get_unpacked_marks(
  337. obj: object | type,
  338. *,
  339. consider_mro: bool = True,
  340. ) -> list[Mark]:
  341. """Obtain the unpacked marks that are stored on an object.
  342. If obj is a class and consider_mro is true, return marks applied to
  343. this class and all of its super-classes in MRO order. If consider_mro
  344. is false, only return marks applied directly to this class.
  345. """
  346. if isinstance(obj, type):
  347. if not consider_mro:
  348. mark_lists = [obj.__dict__.get("pytestmark", [])]
  349. else:
  350. mark_lists = [
  351. x.__dict__.get("pytestmark", []) for x in reversed(obj.__mro__)
  352. ]
  353. mark_list = []
  354. for item in mark_lists:
  355. if isinstance(item, list):
  356. mark_list.extend(item)
  357. else:
  358. mark_list.append(item)
  359. else:
  360. mark_attribute = getattr(obj, "pytestmark", [])
  361. if isinstance(mark_attribute, list):
  362. mark_list = mark_attribute
  363. else:
  364. mark_list = [mark_attribute]
  365. return list(normalize_mark_list(mark_list))
  366. def normalize_mark_list(
  367. mark_list: Iterable[Mark | MarkDecorator],
  368. ) -> Iterable[Mark]:
  369. """
  370. Normalize an iterable of Mark or MarkDecorator objects into a list of marks
  371. by retrieving the `mark` attribute on MarkDecorator instances.
  372. :param mark_list: marks to normalize
  373. :returns: A new list of the extracted Mark objects
  374. """
  375. for mark in mark_list:
  376. mark_obj = getattr(mark, "mark", mark)
  377. if not isinstance(mark_obj, Mark):
  378. raise TypeError(f"got {mark_obj!r} instead of Mark")
  379. yield mark_obj
  380. def store_mark(obj, mark: Mark, *, stacklevel: int = 2) -> None:
  381. """Store a Mark on an object.
  382. This is used to implement the Mark declarations/decorators correctly.
  383. """
  384. assert isinstance(mark, Mark), mark
  385. from ..fixtures import getfixturemarker
  386. if getfixturemarker(obj) is not None:
  387. warnings.warn(MARKED_FIXTURE, stacklevel=stacklevel)
  388. # Always reassign name to avoid updating pytestmark in a reference that
  389. # was only borrowed.
  390. obj.pytestmark = [*get_unpacked_marks(obj, consider_mro=False), mark]
  391. # Typing for builtin pytest marks. This is cheating; it gives builtin marks
  392. # special privilege, and breaks modularity. But practicality beats purity...
  393. if TYPE_CHECKING:
  394. class _SkipMarkDecorator(MarkDecorator):
  395. @overload # type: ignore[override,no-overload-impl]
  396. def __call__(self, arg: Markable) -> Markable: ...
  397. @overload
  398. def __call__(self, reason: str = ...) -> MarkDecorator: ...
  399. class _SkipifMarkDecorator(MarkDecorator):
  400. def __call__( # type: ignore[override]
  401. self,
  402. condition: str | bool = ...,
  403. *conditions: str | bool,
  404. reason: str = ...,
  405. ) -> MarkDecorator: ...
  406. class _XfailMarkDecorator(MarkDecorator):
  407. @overload # type: ignore[override,no-overload-impl]
  408. def __call__(self, arg: Markable) -> Markable: ...
  409. @overload
  410. def __call__(
  411. self,
  412. condition: str | bool = False,
  413. *conditions: str | bool,
  414. reason: str = ...,
  415. run: bool = ...,
  416. raises: None
  417. | type[BaseException]
  418. | tuple[type[BaseException], ...]
  419. | AbstractRaises[BaseException] = ...,
  420. strict: bool = ...,
  421. ) -> MarkDecorator: ...
  422. class _ParametrizeMarkDecorator(MarkDecorator):
  423. def __call__( # type: ignore[override]
  424. self,
  425. argnames: str | Sequence[str],
  426. argvalues: Iterable[ParameterSet | Sequence[object] | object],
  427. *,
  428. indirect: bool | Sequence[str] = ...,
  429. ids: Iterable[None | str | float | int | bool]
  430. | Callable[[Any], object | None]
  431. | None = ...,
  432. scope: _ScopeName | None = ...,
  433. ) -> MarkDecorator: ...
  434. class _UsefixturesMarkDecorator(MarkDecorator):
  435. def __call__(self, *fixtures: str) -> MarkDecorator: # type: ignore[override]
  436. ...
  437. class _FilterwarningsMarkDecorator(MarkDecorator):
  438. def __call__(self, *filters: str) -> MarkDecorator: # type: ignore[override]
  439. ...
  440. @final
  441. class MarkGenerator:
  442. """Factory for :class:`MarkDecorator` objects - exposed as
  443. a ``pytest.mark`` singleton instance.
  444. Example::
  445. import pytest
  446. @pytest.mark.slowtest
  447. def test_function():
  448. pass
  449. applies a 'slowtest' :class:`Mark` on ``test_function``.
  450. """
  451. # See TYPE_CHECKING above.
  452. if TYPE_CHECKING:
  453. skip: _SkipMarkDecorator
  454. skipif: _SkipifMarkDecorator
  455. xfail: _XfailMarkDecorator
  456. parametrize: _ParametrizeMarkDecorator
  457. usefixtures: _UsefixturesMarkDecorator
  458. filterwarnings: _FilterwarningsMarkDecorator
  459. def __init__(self, *, _ispytest: bool = False) -> None:
  460. check_ispytest(_ispytest)
  461. self._config: Config | None = None
  462. self._markers: set[str] = set()
  463. def __getattr__(self, name: str) -> MarkDecorator:
  464. """Generate a new :class:`MarkDecorator` with the given name."""
  465. if name[0] == "_":
  466. raise AttributeError("Marker name must NOT start with underscore")
  467. if self._config is not None:
  468. # We store a set of markers as a performance optimisation - if a mark
  469. # name is in the set we definitely know it, but a mark may be known and
  470. # not in the set. We therefore start by updating the set!
  471. if name not in self._markers:
  472. for line in self._config.getini("markers"):
  473. # example lines: "skipif(condition): skip the given test if..."
  474. # or "hypothesis: tests which use Hypothesis", so to get the
  475. # marker name we split on both `:` and `(`.
  476. marker = line.split(":")[0].split("(")[0].strip()
  477. self._markers.add(marker)
  478. # If the name is not in the set of known marks after updating,
  479. # then it really is time to issue a warning or an error.
  480. if name not in self._markers:
  481. # Raise a specific error for common misspellings of "parametrize".
  482. if name in ["parameterize", "parametrise", "parameterise"]:
  483. __tracebackhide__ = True
  484. fail(f"Unknown '{name}' mark, did you mean 'parametrize'?")
  485. strict_markers = self._config.getini("strict_markers")
  486. if strict_markers is None:
  487. strict_markers = self._config.getini("strict")
  488. if strict_markers:
  489. fail(
  490. f"{name!r} not found in `markers` configuration option",
  491. pytrace=False,
  492. )
  493. warnings.warn(
  494. f"Unknown pytest.mark.{name} - is this a typo? You can register "
  495. "custom marks to avoid this warning - for details, see "
  496. "https://docs.pytest.org/en/stable/how-to/mark.html",
  497. PytestUnknownMarkWarning,
  498. 2,
  499. )
  500. return MarkDecorator(Mark(name, (), {}, _ispytest=True), _ispytest=True)
  501. MARK_GEN = MarkGenerator(_ispytest=True)
  502. @final
  503. class NodeKeywords(MutableMapping[str, Any]):
  504. __slots__ = ("_markers", "node", "parent")
  505. def __init__(self, node: Node) -> None:
  506. self.node = node
  507. self.parent = node.parent
  508. self._markers = {node.name: True}
  509. def __getitem__(self, key: str) -> Any:
  510. try:
  511. return self._markers[key]
  512. except KeyError:
  513. if self.parent is None:
  514. raise
  515. return self.parent.keywords[key]
  516. def __setitem__(self, key: str, value: Any) -> None:
  517. self._markers[key] = value
  518. # Note: we could've avoided explicitly implementing some of the methods
  519. # below and use the collections.abc fallback, but that would be slow.
  520. def __contains__(self, key: object) -> bool:
  521. return key in self._markers or (
  522. self.parent is not None and key in self.parent.keywords
  523. )
  524. def update( # type: ignore[override]
  525. self,
  526. other: Mapping[str, Any] | Iterable[tuple[str, Any]] = (),
  527. **kwds: Any,
  528. ) -> None:
  529. self._markers.update(other)
  530. self._markers.update(kwds)
  531. def __delitem__(self, key: str) -> None:
  532. raise ValueError("cannot delete key in keywords dict")
  533. def __iter__(self) -> Iterator[str]:
  534. # Doesn't need to be fast.
  535. yield from self._markers
  536. if self.parent is not None:
  537. for keyword in self.parent.keywords:
  538. # self._marks and self.parent.keywords can have duplicates.
  539. if keyword not in self._markers:
  540. yield keyword
  541. def __len__(self) -> int:
  542. # Doesn't need to be fast.
  543. return sum(1 for keyword in self)
  544. def __repr__(self) -> str:
  545. return f"<NodeKeywords for node {self.node}>"