deprecated.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 warnings import warn
  10. from _pytest.warning_types import PytestDeprecationWarning
  11. from _pytest.warning_types import PytestRemovedIn7Warning
  12. from _pytest.warning_types import PytestRemovedIn8Warning
  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. FILLFUNCARGS = UnformattedWarning(
  22. PytestRemovedIn7Warning,
  23. "{name} is deprecated, use "
  24. "function._request._fillfixtures() instead if you cannot avoid reaching into internals.",
  25. )
  26. PYTEST_COLLECT_MODULE = UnformattedWarning(
  27. PytestRemovedIn7Warning,
  28. "pytest.collect.{name} was moved to pytest.{name}\n"
  29. "Please update to the new name.",
  30. )
  31. # This can be* removed pytest 8, but it's harmless and common, so no rush to remove.
  32. # * If you're in the future: "could have been".
  33. YIELD_FIXTURE = PytestDeprecationWarning(
  34. "@pytest.yield_fixture is deprecated.\n"
  35. "Use @pytest.fixture instead; they are the same."
  36. )
  37. MINUS_K_DASH = PytestRemovedIn7Warning(
  38. "The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."
  39. )
  40. MINUS_K_COLON = PytestRemovedIn7Warning(
  41. "The `-k 'expr:'` syntax to -k is deprecated.\n"
  42. "Please open an issue if you use this and want a replacement."
  43. )
  44. WARNING_CAPTURED_HOOK = PytestRemovedIn7Warning(
  45. "The pytest_warning_captured is deprecated and will be removed in a future release.\n"
  46. "Please use pytest_warning_recorded instead."
  47. )
  48. WARNING_CMDLINE_PREPARSE_HOOK = PytestRemovedIn8Warning(
  49. "The pytest_cmdline_preparse hook is deprecated and will be removed in a future release. \n"
  50. "Please use pytest_load_initial_conftests hook instead."
  51. )
  52. FSCOLLECTOR_GETHOOKPROXY_ISINITPATH = PytestRemovedIn8Warning(
  53. "The gethookproxy() and isinitpath() methods of FSCollector and Package are deprecated; "
  54. "use self.session.gethookproxy() and self.session.isinitpath() instead. "
  55. )
  56. STRICT_OPTION = PytestRemovedIn8Warning(
  57. "The --strict option is deprecated, use --strict-markers instead."
  58. )
  59. # This deprecation is never really meant to be removed.
  60. PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
  61. UNITTEST_SKIP_DURING_COLLECTION = PytestRemovedIn8Warning(
  62. "Raising unittest.SkipTest to skip tests during collection is deprecated. "
  63. "Use pytest.skip() instead."
  64. )
  65. ARGUMENT_PERCENT_DEFAULT = PytestRemovedIn8Warning(
  66. 'pytest now uses argparse. "%default" should be changed to "%(default)s"',
  67. )
  68. ARGUMENT_TYPE_STR_CHOICE = UnformattedWarning(
  69. PytestRemovedIn8Warning,
  70. "`type` argument to addoption() is the string {typ!r}."
  71. " For choices this is optional and can be omitted, "
  72. " but when supplied should be a type (for example `str` or `int`)."
  73. " (options: {names})",
  74. )
  75. ARGUMENT_TYPE_STR = UnformattedWarning(
  76. PytestRemovedIn8Warning,
  77. "`type` argument to addoption() is the string {typ!r}, "
  78. " but when supplied should be a type (for example `str` or `int`)."
  79. " (options: {names})",
  80. )
  81. HOOK_LEGACY_PATH_ARG = UnformattedWarning(
  82. PytestRemovedIn8Warning,
  83. "The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n"
  84. "see https://docs.pytest.org/en/latest/deprecations.html"
  85. "#py-path-local-arguments-for-hooks-replaced-with-pathlib-path",
  86. )
  87. NODE_CTOR_FSPATH_ARG = UnformattedWarning(
  88. PytestRemovedIn8Warning,
  89. "The (fspath: py.path.local) argument to {node_type_name} is deprecated. "
  90. "Please use the (path: pathlib.Path) argument instead.\n"
  91. "See https://docs.pytest.org/en/latest/deprecations.html"
  92. "#fspath-argument-for-node-constructors-replaced-with-pathlib-path",
  93. )
  94. WARNS_NONE_ARG = PytestRemovedIn8Warning(
  95. "Passing None has been deprecated.\n"
  96. "See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
  97. "#additional-use-cases-of-warnings-in-tests"
  98. " for alternatives in common use cases."
  99. )
  100. KEYWORD_MSG_ARG = UnformattedWarning(
  101. PytestRemovedIn8Warning,
  102. "pytest.{func}(msg=...) is now deprecated, use pytest.{func}(reason=...) instead",
  103. )
  104. INSTANCE_COLLECTOR = PytestRemovedIn8Warning(
  105. "The pytest.Instance collector type is deprecated and is no longer used. "
  106. "See https://docs.pytest.org/en/latest/deprecations.html#the-pytest-instance-collector",
  107. )
  108. # You want to make some `__init__` or function "private".
  109. #
  110. # def my_private_function(some, args):
  111. # ...
  112. #
  113. # Do this:
  114. #
  115. # def my_private_function(some, args, *, _ispytest: bool = False):
  116. # check_ispytest(_ispytest)
  117. # ...
  118. #
  119. # Change all internal/allowed calls to
  120. #
  121. # my_private_function(some, args, _ispytest=True)
  122. #
  123. # All other calls will get the default _ispytest=False and trigger
  124. # the warning (possibly error in the future).
  125. def check_ispytest(ispytest: bool) -> None:
  126. if not ispytest:
  127. warn(PRIVATE, stacklevel=3)