Coverage for C:\Repos\ekr-pylint\pylint\config\deprecation_actions.py: 48%

25 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE 

3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5# pylint: disable=too-many-arguments, redefined-builtin 

6 

7"""Deprecated option actions.""" 

8 

9from __future__ import annotations 

10 

11import argparse 

12import warnings 

13from collections.abc import Sequence 

14from typing import Any 

15 

16 

17class _OldNamesAction(argparse._StoreAction): 

18 """Store action that also sets the value to old names.""" 

19 

20 def __init__( 

21 self, 

22 option_strings: Sequence[str], 

23 dest: str, 

24 nargs: None = None, 

25 const: None = None, 

26 default: None = None, 

27 type: None = None, 

28 choices: None = None, 

29 required: bool = False, 

30 help: str = "", 

31 metavar: str = "", 

32 old_names: list[str] | None = None, 

33 ) -> None: 

34 assert old_names 

35 self.old_names = old_names 

36 super().__init__( 

37 option_strings, 

38 dest, 

39 1, 

40 const, 

41 default, 

42 type, 

43 choices, 

44 required, 

45 help, 

46 metavar, 

47 ) 

48 

49 def __call__( 

50 self, 

51 parser: argparse.ArgumentParser, 

52 namespace: argparse.Namespace, 

53 values: str | Sequence[Any] | None, 

54 option_string: str | None = None, 

55 ): 

56 assert isinstance(values, list) 

57 setattr(namespace, self.dest, values[0]) 

58 for old_name in self.old_names: 

59 setattr(namespace, old_name, values[0]) 

60 

61 

62class _NewNamesAction(argparse._StoreAction): 

63 """Store action that also emits a deprecation warning about a new name.""" 

64 

65 def __init__( 

66 self, 

67 option_strings: Sequence[str], 

68 dest: str, 

69 nargs: None = None, 

70 const: None = None, 

71 default: None = None, 

72 type: None = None, 

73 choices: None = None, 

74 required: bool = False, 

75 help: str = "", 

76 metavar: str = "", 

77 new_names: list[str] | None = None, 

78 ) -> None: 

79 assert new_names 

80 self.new_names = new_names 

81 super().__init__( 

82 option_strings, 

83 dest, 

84 1, 

85 const, 

86 default, 

87 type, 

88 choices, 

89 required, 

90 help, 

91 metavar, 

92 ) 

93 

94 def __call__( 

95 self, 

96 parser: argparse.ArgumentParser, 

97 namespace: argparse.Namespace, 

98 values: str | Sequence[Any] | None, 

99 option_string: str | None = None, 

100 ): 

101 assert isinstance(values, list) 

102 setattr(namespace, self.dest, values[0]) 

103 warnings.warn( 

104 f"{self.option_strings[0]} has been deprecated. Please look into " 

105 f"using any of the following options: {', '.join(self.new_names)}.", 

106 DeprecationWarning, 

107 )