Skip to content

Commit 477f51e

Browse files
authored
TST: tighten stacklevel checks (#41560)
1 parent a395185 commit 477f51e

28 files changed

+96
-63
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,9 @@ def _validate_shift_value(self, fill_value):
599599
"will raise in a future version, pass "
600600
f"{self._scalar_type.__name__} instead.",
601601
FutureWarning,
602-
stacklevel=8,
602+
# There is no way to hard-code the level since this might be
603+
# reached directly or called from the Index or Block method
604+
stacklevel=find_stack_level(),
603605
)
604606
fill_value = new_fill
605607

pandas/core/arrays/datetimes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ def to_perioddelta(self, freq) -> TimedeltaArray:
11751175
"future version. "
11761176
"Use `dtindex - dtindex.to_period(freq).to_timestamp()` instead",
11771177
FutureWarning,
1178+
# stacklevel chosen to be correct for when called from DatetimeIndex
11781179
stacklevel=3,
11791180
)
11801181
from pandas.core.arrays.timedeltas import TimedeltaArray

pandas/core/frame.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,7 @@ def to_dict(self, orient: str = "dict", into=dict):
17641764
"will be used in a future version. Use one of the above "
17651765
"to silence this warning.",
17661766
FutureWarning,
1767+
stacklevel=2,
17671768
)
17681769

17691770
if orient.startswith("d"):

pandas/core/generic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,19 @@ def _data(self):
481481
@property
482482
def _AXIS_NUMBERS(self) -> dict[str, int]:
483483
""".. deprecated:: 1.1.0"""
484-
warnings.warn("_AXIS_NUMBERS has been deprecated.", FutureWarning, stacklevel=3)
484+
level = self.ndim + 1
485+
warnings.warn(
486+
"_AXIS_NUMBERS has been deprecated.", FutureWarning, stacklevel=level
487+
)
485488
return {"index": 0}
486489

487490
@property
488491
def _AXIS_NAMES(self) -> dict[int, str]:
489492
""".. deprecated:: 1.1.0"""
490-
warnings.warn("_AXIS_NAMES has been deprecated.", FutureWarning, stacklevel=3)
493+
level = self.ndim + 1
494+
warnings.warn(
495+
"_AXIS_NAMES has been deprecated.", FutureWarning, stacklevel=level
496+
)
491497
return {0: "index"}
492498

493499
@final

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3691,7 +3691,7 @@ def is_int(v):
36913691
"and will raise TypeError in a future version. "
36923692
"Use .loc with labels or .iloc with positions instead.",
36933693
FutureWarning,
3694-
stacklevel=6,
3694+
stacklevel=5,
36953695
)
36963696
indexer = key
36973697
else:

pandas/core/indexes/datetimes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
cache_readonly,
4141
doc,
4242
)
43+
from pandas.util._exceptions import find_stack_level
4344

4445
from pandas.core.dtypes.common import (
4546
DT64NS_DTYPE,
@@ -660,7 +661,7 @@ def _deprecate_mismatched_indexing(self, key) -> None:
660661
"raise KeyError in a future version. "
661662
"Use a timezone-aware object instead."
662663
)
663-
warnings.warn(msg, FutureWarning, stacklevel=5)
664+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
664665

665666
def get_loc(self, key, method=None, tolerance=None):
666667
"""

pandas/core/reshape/merge.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ def __init__(
673673
f"in a future version. ({left.columns.nlevels} levels on the left,"
674674
f"{right.columns.nlevels} on the right)"
675675
)
676+
# stacklevel chosen to be correct when this is reached via pd.merge
677+
# (and not DataFrame.join)
676678
warnings.warn(msg, FutureWarning, stacklevel=3)
677679

678680
self._validate_specification()

pandas/tests/arrays/test_datetimelike.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ def test_shift_fill_int_deprecated(self):
561561
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
562562
arr = self.array_cls(data, freq="D")
563563

564-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
564+
msg = "Passing <class 'int'> to shift"
565+
with tm.assert_produces_warning(FutureWarning, match=msg):
565566
result = arr.shift(1, fill_value=1)
566567

567568
expected = arr.copy()
@@ -783,10 +784,13 @@ def test_to_perioddelta(self, datetime_index, freqstr):
783784
dti = datetime_index
784785
arr = DatetimeArray(dti)
785786

786-
with tm.assert_produces_warning(FutureWarning):
787+
msg = "to_perioddelta is deprecated and will be removed"
788+
with tm.assert_produces_warning(FutureWarning, match=msg):
787789
# Deprecation GH#34853
788790
expected = dti.to_perioddelta(freq=freqstr)
789-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
791+
with tm.assert_produces_warning(
792+
FutureWarning, match=msg, check_stacklevel=False
793+
):
790794
# stacklevel is chosen to be "correct" for DatetimeIndex, not
791795
# DatetimeArray
792796
result = arr.to_perioddelta(freq=freqstr)

pandas/tests/dtypes/cast/test_promote.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,13 @@ def test_maybe_promote_any_with_datetime64(
406406
exp_val_for_scalar = fill_value
407407

408408
warn = None
409+
msg = "Using a `date` object for fill_value"
409410
if type(fill_value) is datetime.date and dtype.kind == "M":
410411
# Casting date to dt64 is deprecated
411412
warn = FutureWarning
412413

413-
with tm.assert_produces_warning(warn, check_stacklevel=False):
414+
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
415+
# stacklevel is chosen to make sense when called from higher-level functions
414416
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)
415417

416418

pandas/tests/dtypes/test_missing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,10 @@ def test_array_equivalent(dtype_equal):
443443
)
444444
def test_array_equivalent_series(val):
445445
arr = np.array([1, 2])
446+
msg = "elementwise comparison failed"
446447
cm = (
447-
tm.assert_produces_warning(FutureWarning, check_stacklevel=False)
448+
# stacklevel is chosen to make sense when called from .equals
449+
tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False)
448450
if isinstance(val, str)
449451
else nullcontext()
450452
)

0 commit comments

Comments
 (0)