Skip to content

Commit a57edea

Browse files
yileiDanielNoordPierre-Sassoulas
authored
Use stacklevel=2 in warnings.warn calls to DeprecationWarning where makes sense. (#7465)
Co-authored-by: Daniël van Noord <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 2792539 commit a57edea

23 files changed

+70
-0
lines changed

doc/whatsnew/fragments/7463.other

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Relevant `DeprecationWarnings` are now raised with `stacklevel=2`, so they have the callsite attached in the message.
2+
3+
Closes #7463

pylint/checkers/base_checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(self, linter: PyLinter) -> None:
5454
"longer supported. Child classes should only inherit BaseChecker or any "
5555
"of the other checker types from pylint.checkers.",
5656
DeprecationWarning,
57+
stacklevel=2,
5758
)
5859
if self.name is not None:
5960
self.name = self.name.lower()

pylint/checkers/mapreduce_checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self) -> None:
2020
"MapReduceMixin has been deprecated and will be removed in pylint 3.0. "
2121
"To make a checker reduce map data simply implement get_map_data and reduce_map_data.",
2222
DeprecationWarning,
23+
stacklevel=2,
2324
)
2425

2526
@abc.abstractmethod

pylint/checkers/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def is_inside_lambda(node: nodes.NodeNG) -> bool:
244244
"utils.is_inside_lambda will be removed in favour of calling "
245245
"utils.get_node_first_ancestor_of_type(x, nodes.Lambda) in pylint 3.0",
246246
DeprecationWarning,
247+
stacklevel=2,
247248
)
248249
return any(isinstance(parent, nodes.Lambda) for parent in node.node_ancestors())
249250

@@ -505,6 +506,7 @@ def check_messages(
505506
"utils.check_messages will be removed in favour of calling "
506507
"utils.only_required_for_messages in pylint 3.0",
507508
DeprecationWarning,
509+
stacklevel=2,
508510
)
509511

510512
return only_required_for_messages(*messages)
@@ -1516,6 +1518,7 @@ def is_class_subscriptable_pep585_with_postponed_evaluation_enabled(
15161518
"Use 'is_postponed_evaluation_enabled(node) and "
15171519
"is_node_in_type_annotation_context(node)' instead.",
15181520
DeprecationWarning,
1521+
stacklevel=2,
15191522
)
15201523
return (
15211524
is_postponed_evaluation_enabled(node)

pylint/config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def load_results(base: str) -> LinterStats | None:
4848
"'pylint.config.load_results' is deprecated, please use "
4949
"'pylint.lint.load_results' instead. This will be removed in 3.0.",
5050
DeprecationWarning,
51+
stacklevel=2,
5152
)
5253
return _real_load_results(base, PYLINT_HOME)
5354

@@ -61,5 +62,6 @@ def save_results(results: LinterStats, base: str) -> None:
6162
"'pylint.config.save_results' is deprecated, please use "
6263
"'pylint.lint.save_results' instead. This will be removed in 3.0.",
6364
DeprecationWarning,
65+
stacklevel=2,
6466
)
6567
return _real_save_results(results, base, PYLINT_HOME)

pylint/config/arguments_manager.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def options_providers(self) -> list[ConfigProvider]:
125125
warnings.warn(
126126
"options_providers has been deprecated. It will be removed in pylint 3.0.",
127127
DeprecationWarning,
128+
stacklevel=2,
128129
)
129130
return self._options_providers
130131

@@ -133,6 +134,7 @@ def options_providers(self, value: list[ConfigProvider]) -> None:
133134
warnings.warn(
134135
"Setting options_providers has been deprecated. It will be removed in pylint 3.0.",
135136
DeprecationWarning,
137+
stacklevel=2,
136138
)
137139
self._options_providers = value
138140

@@ -282,6 +284,7 @@ def reset_parsers(self, usage: str = "") -> None: # pragma: no cover
282284
"reset_parsers has been deprecated. Parsers should be instantiated "
283285
"once during initialization and do not need to be reset.",
284286
DeprecationWarning,
287+
stacklevel=2,
285288
)
286289
# configuration file parser
287290
self.cfgfile_parser = configparser.ConfigParser(
@@ -301,6 +304,7 @@ def register_options_provider(
301304
"arguments providers should be registered by initializing ArgumentsProvider. "
302305
"This automatically registers the provider on the ArgumentsManager.",
303306
DeprecationWarning,
307+
stacklevel=2,
304308
)
305309
self.options_providers.append(provider)
306310
non_group_spec_options = [
@@ -345,6 +349,7 @@ def add_option_group(
345349
"registered by initializing ArgumentsProvider. "
346350
"This automatically registers the group on the ArgumentsManager.",
347351
DeprecationWarning,
352+
stacklevel=2,
348353
)
349354
# add option group to the command line parser
350355
if group_name in self._mygroups:
@@ -381,6 +386,7 @@ def add_optik_option(
381386
"add_optik_option has been deprecated. Options should be automatically "
382387
"added by initializing an ArgumentsProvider.",
383388
DeprecationWarning,
389+
stacklevel=2,
384390
)
385391
with warnings.catch_warnings():
386392
warnings.filterwarnings("ignore", category=DeprecationWarning)
@@ -399,6 +405,7 @@ def optik_option(
399405
"optik_option has been deprecated. Parsing of option dictionaries should be done "
400406
"automatically by initializing an ArgumentsProvider.",
401407
DeprecationWarning,
408+
stacklevel=2,
402409
)
403410
optdict = copy.copy(optdict)
404411
if "action" in optdict:
@@ -436,6 +443,7 @@ def generate_config(
436443
warnings.warn(
437444
"generate_config has been deprecated. It will be removed in pylint 3.0.",
438445
DeprecationWarning,
446+
stacklevel=2,
439447
)
440448
options_by_section = {}
441449
sections = []
@@ -495,6 +503,7 @@ def load_provider_defaults(self) -> None: # pragma: no cover
495503
"load_provider_defaults has been deprecated. Parsing of option defaults should be done "
496504
"automatically by initializing an ArgumentsProvider.",
497505
DeprecationWarning,
506+
stacklevel=2,
498507
)
499508
for provider in self.options_providers:
500509
with warnings.catch_warnings():
@@ -512,6 +521,7 @@ def read_config_file(
512521
warnings.warn(
513522
"read_config_file has been deprecated. It will be removed in pylint 3.0.",
514523
DeprecationWarning,
524+
stacklevel=2,
515525
)
516526
if not config_file:
517527
if verbose:
@@ -583,6 +593,7 @@ def load_config_file(self) -> None: # pragma: no cover
583593
warnings.warn(
584594
"load_config_file has been deprecated. It will be removed in pylint 3.0.",
585595
DeprecationWarning,
596+
stacklevel=2,
586597
)
587598
parser = self.cfgfile_parser
588599
for section in parser.sections():
@@ -597,6 +608,7 @@ def load_configuration(self, **kwargs: Any) -> None: # pragma: no cover
597608
warnings.warn(
598609
"load_configuration has been deprecated. It will be removed in pylint 3.0.",
599610
DeprecationWarning,
611+
stacklevel=2,
600612
)
601613
with warnings.catch_warnings():
602614
warnings.filterwarnings("ignore", category=DeprecationWarning)
@@ -608,6 +620,7 @@ def load_configuration_from_config(
608620
warnings.warn(
609621
"DEPRECATED: load_configuration_from_config has been deprecated. It will be removed in pylint 3.0.",
610622
DeprecationWarning,
623+
stacklevel=2,
611624
)
612625
for opt, opt_value in config.items():
613626
opt = opt.replace("_", "-")
@@ -624,6 +637,7 @@ def load_command_line_configuration(
624637
warnings.warn(
625638
"load_command_line_configuration has been deprecated. It will be removed in pylint 3.0.",
626639
DeprecationWarning,
640+
stacklevel=2,
627641
)
628642
args = sys.argv[1:] if args is None else list(args)
629643
(options, args) = self.cmdline_parser.parse_args(args=args)
@@ -643,6 +657,7 @@ def help(self, level: int | None = None) -> str:
643657
"Supplying a 'level' argument to help() has been deprecated."
644658
"You can call help() without any arguments.",
645659
DeprecationWarning,
660+
stacklevel=2,
646661
)
647662
return self._arg_parser.format_help()
648663

@@ -654,6 +669,7 @@ def cb_set_provider_option( # pragma: no cover
654669
warnings.warn(
655670
"cb_set_provider_option has been deprecated. It will be removed in pylint 3.0.",
656671
DeprecationWarning,
672+
stacklevel=2,
657673
)
658674
if opt.startswith("--"):
659675
# remove -- on long option
@@ -673,6 +689,7 @@ def global_set_option(self, opt: str, value: Any) -> None: # pragma: no cover
673689
"global_set_option has been deprecated. You can use _arguments_manager.set_option "
674690
"or linter.set_option to set options on the global configuration object.",
675691
DeprecationWarning,
692+
stacklevel=2,
676693
)
677694
self.set_option(opt, value)
678695

@@ -776,12 +793,14 @@ def set_option(
776793
"The 'action' argument has been deprecated. You can use set_option "
777794
"without the 'action' or 'optdict' arguments.",
778795
DeprecationWarning,
796+
stacklevel=2,
779797
)
780798
if optdict != "default_value":
781799
warnings.warn(
782800
"The 'optdict' argument has been deprecated. You can use set_option "
783801
"without the 'action' or 'optdict' arguments.",
784802
DeprecationWarning,
803+
stacklevel=2,
785804
)
786805

787806
self.config = self._arg_parser.parse_known_args(

pylint/config/arguments_provider.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, *args: object) -> None:
2424
warnings.warn(
2525
"UnsupportedAction has been deprecated and will be removed in pylint 3.0",
2626
DeprecationWarning,
27+
stacklevel=2,
2728
)
2829
super().__init__(*args)
2930

@@ -55,6 +56,7 @@ def level(self) -> int:
5556
"The level attribute has been deprecated. It was used to display the checker in the help or not,"
5657
" and everything is displayed in the help now. It will be removed in pylint 3.0.",
5758
DeprecationWarning,
59+
stacklevel=2,
5860
)
5961
return self._level
6062

@@ -65,6 +67,7 @@ def level(self, value: int) -> None:
6567
"Setting the level attribute has been deprecated. It was used to display the checker in the help or not,"
6668
" and everything is displayed in the help now. It will be removed in pylint 3.0.",
6769
DeprecationWarning,
70+
stacklevel=2,
6871
)
6972
self._level = value
7073

@@ -75,6 +78,7 @@ def config(self) -> argparse.Namespace:
7578
"The checker-specific config attribute has been deprecated. Please use "
7679
"'linter.config' to access the global configuration object.",
7780
DeprecationWarning,
81+
stacklevel=2,
7882
)
7983
return self._arguments_manager.config
8084

@@ -85,6 +89,7 @@ def load_defaults(self) -> None: # pragma: no cover
8589
"registered by initializing an ArgumentsProvider. "
8690
"This automatically registers the group on the ArgumentsManager.",
8791
DeprecationWarning,
92+
stacklevel=2,
8893
)
8994
for opt, optdict in self.options:
9095
action = optdict.get("action")
@@ -105,6 +110,7 @@ def option_attrname(
105110
"option_attrname has been deprecated. It will be removed "
106111
"in a future release.",
107112
DeprecationWarning,
113+
stacklevel=2,
108114
)
109115
if optdict is None:
110116
with warnings.catch_warnings():
@@ -118,6 +124,7 @@ def option_value(self, opt: str) -> Any: # pragma: no cover
118124
"option_value has been deprecated. It will be removed "
119125
"in a future release.",
120126
DeprecationWarning,
127+
stacklevel=2,
121128
)
122129
return getattr(self._arguments_manager.config, opt.replace("-", "_"), None)
123130

@@ -136,6 +143,7 @@ def set_option( # pragma: no cover
136143
"set_option has been deprecated. You can use _arguments_manager.set_option "
137144
"or linter.set_option to set options on the global configuration object.",
138145
DeprecationWarning,
146+
stacklevel=2,
139147
)
140148
self._arguments_manager.set_option(optname, value)
141149

@@ -148,6 +156,7 @@ def get_option_def(self, opt: str) -> OptionDict: # pragma: no cover
148156
"get_option_def has been deprecated. It will be removed "
149157
"in a future release.",
150158
DeprecationWarning,
159+
stacklevel=2,
151160
)
152161
assert self.options
153162
for option in self.options:
@@ -174,6 +183,7 @@ def options_by_section(
174183
"options_by_section has been deprecated. It will be removed "
175184
"in a future release.",
176185
DeprecationWarning,
186+
stacklevel=2,
177187
)
178188
sections: dict[str, list[tuple[str, OptionDict, Any]]] = {}
179189
for optname, optdict in self.options:
@@ -195,6 +205,7 @@ def options_and_values(
195205
"options_and_values has been deprecated. It will be removed "
196206
"in a future release.",
197207
DeprecationWarning,
208+
stacklevel=2,
198209
)
199210
if options is None:
200211
options = self.options

pylint/config/configuration_mixin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
2121
warnings.warn(
2222
"ConfigurationMixIn has been deprecated and will be removed in pylint 3.0",
2323
DeprecationWarning,
24+
stacklevel=2,
2425
)
2526
if not args:
2627
kwargs.setdefault("usage", "")

pylint/config/find_default_config_files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def find_pylintrc() -> str | None:
122122
"Use find_default_config_files if you want access to pylint's configuration file "
123123
"finding logic.",
124124
DeprecationWarning,
125+
stacklevel=2,
125126
)
126127
for config_file in find_default_config_files():
127128
if str(config_file).endswith("pylintrc"):

pylint/config/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def __init__(self, *opts: Any, **attrs: Any) -> None:
190190
warnings.warn(
191191
"Option has been deprecated and will be removed in pylint 3.0",
192192
DeprecationWarning,
193+
stacklevel=2,
193194
)
194195
super().__init__(*opts, **attrs)
195196
if hasattr(self, "hide") and self.hide: # type: ignore[attr-defined]

pylint/config/option_manager_mixin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def __init__(self, usage: str) -> None:
7777
warnings.warn(
7878
"OptionsManagerMixIn has been deprecated and will be removed in pylint 3.0",
7979
DeprecationWarning,
80+
stacklevel=2,
8081
)
8182
self.reset_parsers(usage)
8283
# list of registered options providers

pylint/config/option_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, option_class, *args, **kwargs):
2525
warnings.warn(
2626
"OptionParser has been deprecated and will be removed in pylint 3.0",
2727
DeprecationWarning,
28+
stacklevel=2,
2829
)
2930
super().__init__(option_class=Option, *args, **kwargs)
3031

pylint/config/options_provider_mixin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self):
2929
warnings.warn(
3030
"OptionsProviderMixIn has been deprecated and will be removed in pylint 3.0",
3131
DeprecationWarning,
32+
stacklevel=2,
3233
)
3334
self.config = optparse.Values()
3435
self.load_defaults()

pylint/interfaces.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self) -> None:
5757
"Interface and all of its subclasses have been deprecated "
5858
"and will be removed in pylint 3.0.",
5959
DeprecationWarning,
60+
stacklevel=2,
6061
)
6162

6263
@classmethod
@@ -78,6 +79,7 @@ def implements(
7879
"implements has been deprecated in favour of using basic "
7980
"inheritance patterns without using __implements__.",
8081
DeprecationWarning,
82+
stacklevel=2,
8183
)
8284
implements_ = getattr(obj, "__implements__", ())
8385
if not isinstance(implements_, (list, tuple)):

0 commit comments

Comments
 (0)