deprecated.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Deprecation messages and bits of code used elsewhere in the codebase that
  2. is planned to be removed in the next pytest release.
  3. Keeping it in a central location makes it easy to track what is deprecated and should
  4. be removed when the time comes.
  5. All constants defined in this module should be either instances of
  6. :class:`PytestWarning`, or :class:`UnformattedWarning`
  7. in case of warnings which need to format their messages.
  8. """
  9. from __future__ import annotations
  10. from warnings import warn
  11. from _pytest.warning_types import PytestDeprecationWarning
  12. from _pytest.warning_types import PytestRemovedIn9Warning
  13. from _pytest.warning_types import PytestRemovedIn10Warning
  14. from _pytest.warning_types import UnformattedWarning
  15. # set of plugins which have been integrated into the core; we use this list to ignore
  16. # them during registration to avoid conflicts
  17. DEPRECATED_EXTERNAL_PLUGINS = {
  18. "pytest_catchlog",
  19. "pytest_capturelog",
  20. "pytest_faulthandler",
  21. "pytest_subtests",
  22. }
  23. # This could have been removed pytest 8, but it's harmless and common, so no rush to remove.
  24. YIELD_FIXTURE = PytestDeprecationWarning(
  25. "@pytest.yield_fixture is deprecated.\n"
  26. "Use @pytest.fixture instead; they are the same."
  27. )
  28. # This deprecation is never really meant to be removed.
  29. PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
  30. HOOK_LEGACY_PATH_ARG = UnformattedWarning(
  31. PytestRemovedIn9Warning,
  32. "The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n"
  33. "see https://docs.pytest.org/en/latest/deprecations.html"
  34. "#py-path-local-arguments-for-hooks-replaced-with-pathlib-path",
  35. )
  36. NODE_CTOR_FSPATH_ARG = UnformattedWarning(
  37. PytestRemovedIn9Warning,
  38. "The (fspath: py.path.local) argument to {node_type_name} is deprecated. "
  39. "Please use the (path: pathlib.Path) argument instead.\n"
  40. "See https://docs.pytest.org/en/latest/deprecations.html"
  41. "#fspath-argument-for-node-constructors-replaced-with-pathlib-path",
  42. )
  43. HOOK_LEGACY_MARKING = UnformattedWarning(
  44. PytestDeprecationWarning,
  45. "The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n"
  46. "Please use the pytest.hook{type}({hook_opts}) decorator instead\n"
  47. " to configure the hooks.\n"
  48. " See https://docs.pytest.org/en/latest/deprecations.html"
  49. "#configuring-hook-specs-impls-using-markers",
  50. )
  51. MARKED_FIXTURE = PytestRemovedIn9Warning(
  52. "Marks applied to fixtures have no effect\n"
  53. "See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function"
  54. )
  55. MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES = PytestRemovedIn10Warning(
  56. "monkeypatch.syspath_prepend() called with pkg_resources legacy namespace packages detected.\n"
  57. "Legacy namespace packages (using pkg_resources.declare_namespace) are deprecated.\n"
  58. "Please use native namespace packages (PEP 420) instead.\n"
  59. "See https://docs.pytest.org/en/stable/deprecations.html#monkeypatch-fixup-namespace-packages"
  60. )
  61. # You want to make some `__init__` or function "private".
  62. #
  63. # def my_private_function(some, args):
  64. # ...
  65. #
  66. # Do this:
  67. #
  68. # def my_private_function(some, args, *, _ispytest: bool = False):
  69. # check_ispytest(_ispytest)
  70. # ...
  71. #
  72. # Change all internal/allowed calls to
  73. #
  74. # my_private_function(some, args, _ispytest=True)
  75. #
  76. # All other calls will get the default _ispytest=False and trigger
  77. # the warning (possibly error in the future).
  78. def check_ispytest(ispytest: bool) -> None:
  79. if not ispytest:
  80. warn(PRIVATE, stacklevel=3)