Skip to content

Commit

Permalink
TST: tighten stacklevel checks (#41560)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored May 19, 2021
1 parent a395185 commit 477f51e
Show file tree
Hide file tree
Showing 28 changed files with 96 additions and 63 deletions.
4 changes: 3 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ def _validate_shift_value(self, fill_value):
"will raise in a future version, pass "
f"{self._scalar_type.__name__} instead.",
FutureWarning,
stacklevel=8,
# There is no way to hard-code the level since this might be
# reached directly or called from the Index or Block method
stacklevel=find_stack_level(),
)
fill_value = new_fill

Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ def to_perioddelta(self, freq) -> TimedeltaArray:
"future version. "
"Use `dtindex - dtindex.to_period(freq).to_timestamp()` instead",
FutureWarning,
# stacklevel chosen to be correct for when called from DatetimeIndex
stacklevel=3,
)
from pandas.core.arrays.timedeltas import TimedeltaArray
Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,7 @@ def to_dict(self, orient: str = "dict", into=dict):
"will be used in a future version. Use one of the above "
"to silence this warning.",
FutureWarning,
stacklevel=2,
)

if orient.startswith("d"):
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,19 @@ def _data(self):
@property
def _AXIS_NUMBERS(self) -> dict[str, int]:
""".. deprecated:: 1.1.0"""
warnings.warn("_AXIS_NUMBERS has been deprecated.", FutureWarning, stacklevel=3)
level = self.ndim + 1
warnings.warn(
"_AXIS_NUMBERS has been deprecated.", FutureWarning, stacklevel=level
)
return {"index": 0}

@property
def _AXIS_NAMES(self) -> dict[int, str]:
""".. deprecated:: 1.1.0"""
warnings.warn("_AXIS_NAMES has been deprecated.", FutureWarning, stacklevel=3)
level = self.ndim + 1
warnings.warn(
"_AXIS_NAMES has been deprecated.", FutureWarning, stacklevel=level
)
return {0: "index"}

@final
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3691,7 +3691,7 @@ def is_int(v):
"and will raise TypeError in a future version. "
"Use .loc with labels or .iloc with positions instead.",
FutureWarning,
stacklevel=6,
stacklevel=5,
)
indexer = key
else:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
cache_readonly,
doc,
)
from pandas.util._exceptions import find_stack_level

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

def get_loc(self, key, method=None, tolerance=None):
"""
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ def __init__(
f"in a future version. ({left.columns.nlevels} levels on the left,"
f"{right.columns.nlevels} on the right)"
)
# stacklevel chosen to be correct when this is reached via pd.merge
# (and not DataFrame.join)
warnings.warn(msg, FutureWarning, stacklevel=3)

self._validate_specification()
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ def test_shift_fill_int_deprecated(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
arr = self.array_cls(data, freq="D")

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

expected = arr.copy()
Expand Down Expand Up @@ -783,10 +784,13 @@ def test_to_perioddelta(self, datetime_index, freqstr):
dti = datetime_index
arr = DatetimeArray(dti)

with tm.assert_produces_warning(FutureWarning):
msg = "to_perioddelta is deprecated and will be removed"
with tm.assert_produces_warning(FutureWarning, match=msg):
# Deprecation GH#34853
expected = dti.to_perioddelta(freq=freqstr)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
# stacklevel is chosen to be "correct" for DatetimeIndex, not
# DatetimeArray
result = arr.to_perioddelta(freq=freqstr)
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ def test_maybe_promote_any_with_datetime64(
exp_val_for_scalar = fill_value

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

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


Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,10 @@ def test_array_equivalent(dtype_equal):
)
def test_array_equivalent_series(val):
arr = np.array([1, 2])
msg = "elementwise comparison failed"
cm = (
tm.assert_produces_warning(FutureWarning, check_stacklevel=False)
# stacklevel is chosen to make sense when called from .equals
tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False)
if isinstance(val, str)
else nullcontext()
)
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/frame/methods/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ def test_merge_join_different_levels(self):
# join, see discussion in GH#12219
columns = ["a", "b", ("a", ""), ("c", "c1")]
expected = DataFrame(columns=columns, data=[[1, 11, 0, 44], [0, 22, 1, 33]])
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "merging between different levels is deprecated"
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
# stacklevel is chosen to be correct for pd.merge, not DataFrame.join
result = df1.join(df2, on="a")
tm.assert_frame_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def test_reset_index_with_datetimeindex_cols(self, name):
)
df.index.name = name

with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
result = df.reset_index()

item = name if name is not None else "index"
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def test_to_dict_invalid_orient(self):
def test_to_dict_short_orient_warns(self, orient):
# GH#32515
df = DataFrame({"A": [0, 1]})
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "Using short name for 'orient' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.to_dict(orient=orient)

@pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict])
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,16 @@ def test_axis_names_deprecated(self, frame_or_series):
# GH33637
box = frame_or_series
obj = box(dtype=object)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "_AXIS_NAMES has been deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
obj._AXIS_NAMES

def test_axis_numbers_deprecated(self, frame_or_series):
# GH33637
box = frame_or_series
obj = box(dtype=object)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "_AXIS_NUMBERS has been deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
obj._AXIS_NUMBERS

def test_flags_identity(self, frame_or_series):
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,11 @@ def test_engine_reference_cycle(self, simple_index):
def test_getitem_2d_deprecated(self, simple_index):
# GH#30588
idx = simple_index
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
msg = "Support for multi-dimensional indexing"
check = not isinstance(idx, (RangeIndex, CategoricalIndex))
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=check
):
res = idx[:, None]

assert isinstance(res, np.ndarray), type(res)
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def test_get_slice_bounds_datetime_within(
key = box(year=2000, month=1, day=7)

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected
Expand All @@ -753,7 +753,7 @@ def test_get_slice_bounds_datetime_outside(
key = box(year=year, month=1, day=7)

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected
Expand All @@ -767,7 +767,7 @@ def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
key = box(2010, 1, 1)

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
# GH#36148 will require tzawareness-compat
result = index.slice_locs(key, box(2010, 1, 2))
expected = (0, 1)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def index(self, request):
@pytest.mark.parametrize("subtype", ["int64", "uint64"])
def test_subtype_integer(self, index, subtype):
dtype = IntervalDtype(subtype, "right")
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = index.astype(dtype)
expected = IntervalIndex.from_arrays(
index.left.astype(subtype),
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_getitem_2d_deprecated(self, simple_index):
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
idx = simple_index
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
idx[:, None]


Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/interval/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ def test_constructor_dtype(self, constructor, breaks, subtype):
# astype(int64) deprecated
warn = FutureWarning

with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
expected_kwargs = self.get_kwargs_from_breaks(breaks.astype(subtype))
expected = constructor(**expected_kwargs)

result_kwargs = self.get_kwargs_from_breaks(breaks)
iv_dtype = IntervalDtype(subtype, "right")
for dtype in (iv_dtype, str(iv_dtype)):
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):

result = constructor(dtype=dtype, **result_kwargs)
tm.assert_index_equal(result, expected)
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_constructor_pass_closed(self, constructor, breaks):
result_kwargs = self.get_kwargs_from_breaks(breaks)

for dtype in (iv_dtype, str(iv_dtype)):
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):

result = constructor(dtype=dtype, closed="left", **result_kwargs)
assert result.dtype.closed == "left"
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_astype_preserves_name(self, index, dtype):
warn = FutureWarning
try:
# Some of these conversions cannot succeed so we use a try / except
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
result = index.astype(dtype)
except (ValueError, TypeError, NotImplementedError, SystemError):
return
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ def test_loc_with_positional_slice_deprecation():
# GH#31840
ser = Series(range(4), index=["A", "B", "C", "D"])

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
ser.loc[:3] = 2

expected = Series([2, 2, 2, 3], index=["A", "B", "C", "D"])
Expand All @@ -2423,14 +2423,14 @@ def test_loc_slice_disallows_positional():
with pytest.raises(TypeError, match=msg):
obj.loc[1:3]

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#31840 deprecated incorrect behavior
obj.loc[1:3] = 1

with pytest.raises(TypeError, match=msg):
df.loc[1:3, 1]

with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#31840 deprecated incorrect behavior
df.loc[1:3, 1] = 2

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def test_astype(self, t):
mgr = create_mgr("a,b: object; c: bool; d: datetime; e: f4; f: f2; g: f8")

t = np.dtype(t)
with tm.assert_produces_warning(warn, check_stacklevel=False):
with tm.assert_produces_warning(warn):
tmgr = mgr.astype(t, errors="ignore")
assert tmgr.iget(2).dtype.type == t
assert tmgr.iget(4).dtype.type == t
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,25 @@ def test_getitem_setitem_datetimeindex():
assert result == expected

result = ts.copy()
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4)] = 0
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4)] = ts[4]
tm.assert_series_equal(result, ts)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result = ts[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)]
expected = ts[4:8]
tm.assert_series_equal(result, expected)

result = ts.copy()
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)] = 0
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)] = ts[4:8]
tm.assert_series_equal(result, ts)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_truncate_datetimeindex_tz(self):
# GH 9243
idx = date_range("4/1/2005", "4/30/2005", freq="D", tz="US/Pacific")
s = Series(range(len(idx)), index=idx)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#36148 in the future will require tzawareness compat
s.truncate(datetime(2005, 4, 2), datetime(2005, 4, 4))

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,9 @@ def test_series_ops_name_retention(
else:
# GH#37374 logical ops behaving as set ops deprecated
warn = FutureWarning if is_rlogical and box is Index else None
with tm.assert_produces_warning(warn, check_stacklevel=False):
msg = "operating as a set operation is deprecated"
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
# stacklevel is correct for Index op, not reversed op
result = op(left, right)

if box is Index and is_rlogical:
Expand Down
Loading

0 comments on commit 477f51e

Please sign in to comment.