deprecated.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 UnformattedWarning
  14. # set of plugins which have been integrated into the core; we use this list to ignore
  15. # them during registration to avoid conflicts
  16. DEPRECATED_EXTERNAL_PLUGINS = {
  17. "pytest_catchlog",
  18. "pytest_capturelog",
  19. "pytest_faulthandler",
  20. }
  21. # This can be* removed pytest 8, but it's harmless and common, so no rush to remove.
  22. # * If you're in the future: "could have been".
  23. YIELD_FIXTURE = PytestDeprecationWarning(
  24. "@pytest.yield_fixture is deprecated.\n"
  25. "Use @pytest.fixture instead; they are the same."
  26. )
  27. # This deprecation is never really meant to be removed.
  28. PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
  29. HOOK_LEGACY_PATH_ARG = UnformattedWarning(
  30. PytestRemovedIn9Warning,
  31. "The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n"
  32. "see https://docs.pytest.org/en/latest/deprecations.html"
  33. "#py-path-local-arguments-for-hooks-replaced-with-pathlib-path",
  34. )
  35. NODE_CTOR_FSPATH_ARG = UnformattedWarning(
  36. PytestRemovedIn9Warning,
  37. "The (fspath: py.path.local) argument to {node_type_name} is deprecated. "
  38. "Please use the (path: pathlib.Path) argument instead.\n"
  39. "See https://docs.pytest.org/en/latest/deprecations.html"
  40. "#fspath-argument-for-node-constructors-replaced-with-pathlib-path",
  41. )
  42. HOOK_LEGACY_MARKING = UnformattedWarning(
  43. PytestDeprecationWarning,
  44. "The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n"
  45. "Please use the pytest.hook{type}({hook_opts}) decorator instead\n"
  46. " to configure the hooks.\n"
  47. " See https://docs.pytest.org/en/latest/deprecations.html"
  48. "#configuring-hook-specs-impls-using-markers",
  49. )
  50. MARKED_FIXTURE = PytestRemovedIn9Warning(
  51. "Marks applied to fixtures have no effect\n"
  52. "See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function"
  53. )
  54. # You want to make some `__init__` or function "private".
  55. #
  56. # def my_private_function(some, args):
  57. # ...
  58. #
  59. # Do this:
  60. #
  61. # def my_private_function(some, args, *, _ispytest: bool = False):
  62. # check_ispytest(_ispytest)
  63. # ...
  64. #
  65. # Change all internal/allowed calls to
  66. #
  67. # my_private_function(some, args, _ispytest=True)
  68. #
  69. # All other calls will get the default _ispytest=False and trigger
  70. # the warning (possibly error in the future).
  71. def check_ispytest(ispytest: bool) -> None:
  72. if not ispytest:
  73. warn(PRIVATE, stacklevel=3)