Skip to content

Commit e94faa2

Browse files
authored
WARN,TST check stacklevel for all warnings (#47998)
* use find_stack_level everywhere * fixup * pyx fixups * fixup test_optional_dependency * fixup api * set check_stacklevel=False for some tests * use lru_cache for currentframe * fixup import in __init__ * add missing imports to pyx files * add missing import * fixup import in conversion * revert some __init__ changes * start n=1 * temporarily dont check stacklevel in _check_plot_works * catch some more warnings * dont check stacklevel in check_plot_works * fixup * ignore stacklevel in check_plot_works
1 parent 9de1f0b commit e94faa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+352
-141
lines changed

doc/source/development/contributing_codebase.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Otherwise, you need to do it manually:
122122
.. code-block:: python
123123
124124
import warnings
125+
from pandas.util._exceptions import find_stack_level
125126
126127
127128
def old_func():
@@ -130,7 +131,11 @@ Otherwise, you need to do it manually:
130131
.. deprecated:: 1.1.0
131132
Use new_func instead.
132133
"""
133-
warnings.warn('Use new_func instead.', FutureWarning, stacklevel=2)
134+
warnings.warn(
135+
'Use new_func instead.',
136+
FutureWarning,
137+
stacklevel=find_stack_level(inspect.currentframe()),
138+
)
134139
new_func()
135140
136141

pandas/_config/config.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
ContextDecorator,
5555
contextmanager,
5656
)
57+
import inspect
5758
import re
5859
from typing import (
5960
Any,
@@ -70,6 +71,7 @@
7071
F,
7172
T,
7273
)
74+
from pandas.util._exceptions import find_stack_level
7375

7476

7577
class DeprecatedOption(NamedTuple):
@@ -657,7 +659,11 @@ def _warn_if_deprecated(key: str) -> bool:
657659
d = _get_deprecated_option(key)
658660
if d:
659661
if d.msg:
660-
warnings.warn(d.msg, FutureWarning)
662+
warnings.warn(
663+
d.msg,
664+
FutureWarning,
665+
stacklevel=find_stack_level(inspect.currentframe()),
666+
)
661667
else:
662668
msg = f"'{key}' is deprecated"
663669
if d.removal_ver:
@@ -667,7 +673,9 @@ def _warn_if_deprecated(key: str) -> bool:
667673
else:
668674
msg += ", please refrain from using it."
669675

670-
warnings.warn(msg, FutureWarning)
676+
warnings.warn(
677+
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
678+
)
671679
return True
672680
return False
673681

pandas/_libs/interval.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def _warning_interval(inclusive: str | None = None, closed: None | lib.NoDefault
230230
warnings.warn(
231231
"Argument `closed` is deprecated in favor of `inclusive`.",
232232
FutureWarning,
233-
stacklevel=2,
233+
stacklevel=find_stack_level(inspect.currentframe()),
234234
)
235235
if closed is None:
236236
inclusive = "right"

pandas/_libs/lib.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import abc
22
from decimal import Decimal
33
from enum import Enum
4+
import inspect
45
from typing import Literal
56
import warnings
67

@@ -30,6 +31,8 @@ from cython cimport (
3031
floating,
3132
)
3233

34+
from pandas.util._exceptions import find_stack_level
35+
3336
import_datetime()
3437

3538
import numpy as np
@@ -352,6 +355,7 @@ def fast_unique_multiple(list arrays, sort: bool = True):
352355
"The values in the array are unorderable. "
353356
"Pass `sort=False` to suppress this warning.",
354357
RuntimeWarning,
358+
stacklevel=find_stack_level(inspect.currentframe()),
355359
)
356360
pass
357361

pandas/_libs/parsers.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ from csv import (
88
QUOTE_NONNUMERIC,
99
)
1010
from errno import ENOENT
11+
import inspect
1112
import sys
1213
import time
1314
import warnings
1415

16+
from pandas.util._exceptions import find_stack_level
17+
1518
cimport cython
1619
from cpython.bytes cimport (
1720
PyBytes_AsString,
@@ -958,7 +961,7 @@ cdef class TextReader:
958961
"Defining usecols with out of bounds indices is deprecated "
959962
"and will raise a ParserError in a future version.",
960963
FutureWarning,
961-
stacklevel=6,
964+
stacklevel=find_stack_level(inspect.currentframe()),
962965
)
963966

964967
results = {}
@@ -1009,7 +1012,7 @@ cdef class TextReader:
10091012
warnings.warn((f"Both a converter and dtype were specified "
10101013
f"for column {name} - only the converter will "
10111014
f"be used."), ParserWarning,
1012-
stacklevel=5)
1015+
stacklevel=find_stack_level(inspect.currentframe()))
10131016
results[i] = _apply_converter(conv, self.parser, i, start, end)
10141017
continue
10151018

pandas/_libs/tslib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
import warnings
23

34
cimport cython
@@ -9,6 +10,8 @@ from cpython.datetime cimport (
910
tzinfo,
1011
)
1112

13+
from pandas.util._exceptions import find_stack_level
14+
1215
# import datetime C API
1316
import_datetime()
1417

@@ -845,7 +848,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
845848
"deprecated. In a future version, this will match Timestamp('now') "
846849
"and Timestamp.now()",
847850
FutureWarning,
848-
stacklevel=1,
851+
stacklevel=find_stack_level(inspect.currentframe()),
849852
)
850853

851854
return True

pandas/_libs/tslibs/conversion.pyx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import inspect
2+
13
cimport cython
24

35
import warnings
46

57
import numpy as np
68

9+
from pandas.util._exceptions import find_stack_level
10+
711
cimport numpy as cnp
812
from cpython.object cimport PyObject
913
from numpy cimport (
@@ -287,7 +291,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
287291
"Conversion of non-round float with unit={unit} is ambiguous "
288292
"and will raise in a future version.",
289293
FutureWarning,
290-
stacklevel=1,
294+
stacklevel=find_stack_level(inspect.currentframe()),
291295
)
292296

293297
ts = cast_from_unit(ts, unit)

pandas/_libs/tslibs/nattype.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import inspect
12
import warnings
23

4+
from pandas.util._exceptions import find_stack_level
5+
36
from cpython.datetime cimport (
47
PyDate_Check,
58
PyDateTime_Check,
@@ -135,7 +138,7 @@ cdef class _NaT(datetime):
135138
"order to match the standard library behavior. "
136139
"In a future version these will be considered non-comparable.",
137140
FutureWarning,
138-
stacklevel=1,
141+
stacklevel=find_stack_level(inspect.currentframe()),
139142
)
140143
return False
141144

@@ -379,7 +382,7 @@ class NaTType(_NaT):
379382
warnings.warn(
380383
"NaT.freq is deprecated and will be removed in a future version.",
381384
FutureWarning,
382-
stacklevel=1,
385+
stacklevel=find_stack_level(inspect.currentframe()),
383386
)
384387
return None
385388

pandas/_libs/tslibs/offsets.pyx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import inspect
12
import operator
23
import re
34
import time
45
import warnings
56

7+
from pandas.util._exceptions import find_stack_level
8+
69
cimport cython
710
from cpython.datetime cimport (
811
PyDate_Check,
@@ -499,7 +502,7 @@ cdef class BaseOffset:
499502
"DateOffset.__call__ is deprecated and will be removed in a future "
500503
"version. Use `offset + other` instead.",
501504
FutureWarning,
502-
stacklevel=1,
505+
stacklevel=find_stack_level(inspect.currentframe()),
503506
)
504507
return self._apply(other)
505508

@@ -509,7 +512,7 @@ cdef class BaseOffset:
509512
f"{type(self).__name__}.apply is deprecated and will be removed "
510513
"in a future version. Use `offset + other` instead",
511514
FutureWarning,
512-
stacklevel=2,
515+
stacklevel=find_stack_level(inspect.currentframe()),
513516
)
514517
return self._apply(other)
515518

@@ -820,15 +823,15 @@ cdef class BaseOffset:
820823
warnings.warn(
821824
"onOffset is a deprecated, use is_on_offset instead.",
822825
FutureWarning,
823-
stacklevel=1,
826+
stacklevel=find_stack_level(inspect.currentframe()),
824827
)
825828
return self.is_on_offset(dt)
826829

827830
def isAnchored(self) -> bool:
828831
warnings.warn(
829832
"isAnchored is a deprecated, use is_anchored instead.",
830833
FutureWarning,
831-
stacklevel=1,
834+
stacklevel=find_stack_level(inspect.currentframe()),
832835
)
833836
return self.is_anchored()
834837

pandas/_libs/tslibs/parsing.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""
22
Parsing functions for datetime and datetime-like strings.
33
"""
4+
import inspect
45
import re
56
import time
67
import warnings
78

9+
from pandas.util._exceptions import find_stack_level
10+
811
cimport cython
912
from cpython.datetime cimport (
1013
datetime,
@@ -214,15 +217,15 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
214217
format='MM/DD/YYYY',
215218
dayfirst='True',
216219
),
217-
stacklevel=4,
220+
stacklevel=find_stack_level(inspect.currentframe()),
218221
)
219222
elif not dayfirst and swapped_day_and_month:
220223
warnings.warn(
221224
PARSING_WARNING_MSG.format(
222225
format='DD/MM/YYYY',
223226
dayfirst='False (the default)',
224227
),
225-
stacklevel=4,
228+
stacklevel=find_stack_level(inspect.currentframe()),
226229
)
227230
# In Python <= 3.6.0 there is no range checking for invalid dates
228231
# in C api, thus we call faster C version for 3.6.1 or newer

pandas/_libs/tslibs/period.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import inspect
12
import warnings
23

4+
from pandas.util._exceptions import find_stack_level
5+
36
cimport numpy as cnp
47
from cpython.object cimport (
58
Py_EQ,
@@ -1827,7 +1830,7 @@ cdef class _Period(PeriodMixin):
18271830
"be removed in a future version. Use "
18281831
"`per.to_timestamp(...).tz_localize(tz)` instead.",
18291832
FutureWarning,
1830-
stacklevel=1,
1833+
stacklevel=find_stack_level(inspect.currentframe()),
18311834
)
18321835

18331836
how = validate_end_alias(how)

pandas/_libs/tslibs/timedeltas.pyx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import collections
2+
import inspect
23
import warnings
34

5+
from pandas.util._exceptions import find_stack_level
6+
47
cimport cython
58
from cpython.object cimport (
69
Py_EQ,
@@ -683,7 +686,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
683686
"Units 'M', 'Y' and 'y' do not represent unambiguous "
684687
"timedelta values and will be removed in a future version.",
685688
FutureWarning,
686-
stacklevel=3,
689+
stacklevel=find_stack_level(inspect.currentframe()),
687690
)
688691

689692
if unit == 'M':
@@ -1055,7 +1058,7 @@ cdef class _Timedelta(timedelta):
10551058
warnings.warn(
10561059
"Timedelta.freq is deprecated and will be removed in a future version",
10571060
FutureWarning,
1058-
stacklevel=1,
1061+
stacklevel=find_stack_level(inspect.currentframe()),
10591062
)
10601063
return None
10611064

@@ -1065,7 +1068,7 @@ cdef class _Timedelta(timedelta):
10651068
warnings.warn(
10661069
"Timedelta.is_populated is deprecated and will be removed in a future version",
10671070
FutureWarning,
1068-
stacklevel=1,
1071+
stacklevel=find_stack_level(inspect.currentframe()),
10691072
)
10701073
return self._is_populated
10711074

@@ -1269,7 +1272,7 @@ cdef class _Timedelta(timedelta):
12691272
warnings.warn(
12701273
"Timedelta.delta is deprecated and will be removed in a future version.",
12711274
FutureWarning,
1272-
stacklevel=1,
1275+
stacklevel=find_stack_level(inspect.currentframe()),
12731276
)
12741277
return self.value
12751278

0 commit comments

Comments
 (0)