deprecation_actions.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. # pylint: disable=too-many-arguments, redefined-builtin
  5. """Deprecated option actions."""
  6. from __future__ import annotations
  7. import argparse
  8. import warnings
  9. from collections.abc import Sequence
  10. from typing import Any
  11. class _OldNamesAction(argparse._StoreAction):
  12. """Store action that also sets the value to old names."""
  13. def __init__(
  14. self,
  15. option_strings: Sequence[str],
  16. dest: str,
  17. nargs: None = None,
  18. const: None = None,
  19. default: None = None,
  20. type: None = None,
  21. choices: None = None,
  22. required: bool = False,
  23. help: str = "",
  24. metavar: str = "",
  25. old_names: list[str] | None = None,
  26. ) -> None:
  27. assert old_names
  28. self.old_names = old_names
  29. super().__init__(
  30. option_strings,
  31. dest,
  32. 1,
  33. const,
  34. default,
  35. type,
  36. choices,
  37. required,
  38. help,
  39. metavar,
  40. )
  41. def __call__(
  42. self,
  43. parser: argparse.ArgumentParser,
  44. namespace: argparse.Namespace,
  45. values: str | Sequence[Any] | None,
  46. option_string: str | None = None,
  47. ) -> None:
  48. assert isinstance(values, list)
  49. setattr(namespace, self.dest, values[0])
  50. for old_name in self.old_names:
  51. setattr(namespace, old_name, values[0])
  52. class _NewNamesAction(argparse._StoreAction):
  53. """Store action that also emits a deprecation warning about a new name."""
  54. def __init__(
  55. self,
  56. option_strings: Sequence[str],
  57. dest: str,
  58. nargs: None = None,
  59. const: None = None,
  60. default: None = None,
  61. type: None = None,
  62. choices: None = None,
  63. required: bool = False,
  64. help: str = "",
  65. metavar: str = "",
  66. new_names: list[str] | None = None,
  67. ) -> None:
  68. assert new_names
  69. self.new_names = new_names
  70. super().__init__(
  71. option_strings,
  72. dest,
  73. 1,
  74. const,
  75. default,
  76. type,
  77. choices,
  78. required,
  79. help,
  80. metavar,
  81. )
  82. def __call__(
  83. self,
  84. parser: argparse.ArgumentParser,
  85. namespace: argparse.Namespace,
  86. values: str | Sequence[Any] | None,
  87. option_string: str | None = None,
  88. ) -> None:
  89. assert isinstance(values, list)
  90. setattr(namespace, self.dest, values[0])
  91. warnings.warn(
  92. f"{self.option_strings[0]} has been deprecated. Please look into "
  93. f"using any of the following options: {', '.join(self.new_names)}.",
  94. DeprecationWarning,
  95. )