deprecated.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 PytestRemovedIn8Warning
  12. from _pytest.warning_types import UnformattedWarning
  13. # set of plugins which have been integrated into the core; we use this list to ignore
  14. # them during registration to avoid conflicts
  15. DEPRECATED_EXTERNAL_PLUGINS = {
  16. "pytest_catchlog",
  17. "pytest_capturelog",
  18. "pytest_faulthandler",
  19. }
  20. NOSE_SUPPORT = UnformattedWarning(
  21. PytestRemovedIn8Warning,
  22. "Support for nose tests is deprecated and will be removed in a future release.\n"
  23. "{nodeid} is using nose method: `{method}` ({stage})\n"
  24. "See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose",
  25. )
  26. NOSE_SUPPORT_METHOD = UnformattedWarning(
  27. PytestRemovedIn8Warning,
  28. "Support for nose tests is deprecated and will be removed in a future release.\n"
  29. "{nodeid} is using nose-specific method: `{method}(self)`\n"
  30. "To remove this warning, rename it to `{method}_method(self)`\n"
  31. "See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose",
  32. )
  33. # This can be* removed pytest 8, but it's harmless and common, so no rush to remove.
  34. # * If you're in the future: "could have been".
  35. YIELD_FIXTURE = PytestDeprecationWarning(
  36. "@pytest.yield_fixture is deprecated.\n"
  37. "Use @pytest.fixture instead; they are the same."
  38. )
  39. WARNING_CMDLINE_PREPARSE_HOOK = PytestRemovedIn8Warning(
  40. "The pytest_cmdline_preparse hook is deprecated and will be removed in a future release. \n"
  41. "Please use pytest_load_initial_conftests hook instead."
  42. )
  43. FSCOLLECTOR_GETHOOKPROXY_ISINITPATH = PytestRemovedIn8Warning(
  44. "The gethookproxy() and isinitpath() methods of FSCollector and Package are deprecated; "
  45. "use self.session.gethookproxy() and self.session.isinitpath() instead. "
  46. )
  47. STRICT_OPTION = PytestRemovedIn8Warning(
  48. "The --strict option is deprecated, use --strict-markers instead."
  49. )
  50. # This deprecation is never really meant to be removed.
  51. PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
  52. ARGUMENT_PERCENT_DEFAULT = PytestRemovedIn8Warning(
  53. 'pytest now uses argparse. "%default" should be changed to "%(default)s"',
  54. )
  55. ARGUMENT_TYPE_STR_CHOICE = UnformattedWarning(
  56. PytestRemovedIn8Warning,
  57. "`type` argument to addoption() is the string {typ!r}."
  58. " For choices this is optional and can be omitted, "
  59. " but when supplied should be a type (for example `str` or `int`)."
  60. " (options: {names})",
  61. )
  62. ARGUMENT_TYPE_STR = UnformattedWarning(
  63. PytestRemovedIn8Warning,
  64. "`type` argument to addoption() is the string {typ!r}, "
  65. " but when supplied should be a type (for example `str` or `int`)."
  66. " (options: {names})",
  67. )
  68. HOOK_LEGACY_PATH_ARG = UnformattedWarning(
  69. PytestRemovedIn8Warning,
  70. "The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n"
  71. "see https://docs.pytest.org/en/latest/deprecations.html"
  72. "#py-path-local-arguments-for-hooks-replaced-with-pathlib-path",
  73. )
  74. NODE_CTOR_FSPATH_ARG = UnformattedWarning(
  75. PytestRemovedIn8Warning,
  76. "The (fspath: py.path.local) argument to {node_type_name} is deprecated. "
  77. "Please use the (path: pathlib.Path) argument instead.\n"
  78. "See https://docs.pytest.org/en/latest/deprecations.html"
  79. "#fspath-argument-for-node-constructors-replaced-with-pathlib-path",
  80. )
  81. WARNS_NONE_ARG = PytestRemovedIn8Warning(
  82. "Passing None has been deprecated.\n"
  83. "See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
  84. "#additional-use-cases-of-warnings-in-tests"
  85. " for alternatives in common use cases."
  86. )
  87. KEYWORD_MSG_ARG = UnformattedWarning(
  88. PytestRemovedIn8Warning,
  89. "pytest.{func}(msg=...) is now deprecated, use pytest.{func}(reason=...) instead",
  90. )
  91. INSTANCE_COLLECTOR = PytestRemovedIn8Warning(
  92. "The pytest.Instance collector type is deprecated and is no longer used. "
  93. "See https://docs.pytest.org/en/latest/deprecations.html#the-pytest-instance-collector",
  94. )
  95. HOOK_LEGACY_MARKING = UnformattedWarning(
  96. PytestDeprecationWarning,
  97. "The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n"
  98. "Please use the pytest.hook{type}({hook_opts}) decorator instead\n"
  99. " to configure the hooks.\n"
  100. " See https://docs.pytest.org/en/latest/deprecations.html"
  101. "#configuring-hook-specs-impls-using-markers",
  102. )
  103. # You want to make some `__init__` or function "private".
  104. #
  105. # def my_private_function(some, args):
  106. # ...
  107. #
  108. # Do this:
  109. #
  110. # def my_private_function(some, args, *, _ispytest: bool = False):
  111. # check_ispytest(_ispytest)
  112. # ...
  113. #
  114. # Change all internal/allowed calls to
  115. #
  116. # my_private_function(some, args, _ispytest=True)
  117. #
  118. # All other calls will get the default _ispytest=False and trigger
  119. # the warning (possibly error in the future).
  120. def check_ispytest(ispytest: bool) -> None:
  121. if not ispytest:
  122. warn(PRIVATE, stacklevel=3)