Skip to content

TST: Raise on pytest.PytestWarning #61996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
# It will be temporarily activated during tests with locale.setlocale
extra_loc: "zh_CN"
platform: ubuntu-24.04
- name: "Past no infer strings"
- name: "PANDAS_FUTURE_INFER_STRING=0"
env_file: actions-312.yaml
pandas_future_infer_string: "0"
platform: ubuntu-24.04
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_simple_new_requires_match(self, unit):
assert dta.dtype == dtype

wrong = DatetimeTZDtype("ns", "UTC")
with pytest.raises(AssertionError, match=""):
with pytest.raises(AssertionError, match="^$"):
DatetimeArray._simple_new(arr, dtype=wrong)

def test_std_non_nano(self, unit):
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ def test_alias_to_unit_raises(self):

def test_alias_to_unit_bad_alias_raises(self):
# 23990
with pytest.raises(TypeError, match=""):
with pytest.raises(
TypeError, match="Cannot construct a 'DatetimeTZDtype' from"
):
DatetimeTZDtype("this is a bad string")

with pytest.raises(TypeError, match=""):
with pytest.raises(
TypeError, match="Cannot construct a 'DatetimeTZDtype' from"
):
DatetimeTZDtype("datetime64[ns, US/NotATZ]")

def test_hash_vs_equality(self, dtype):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_unary_accumulate_axis():

def test_frame_outer_disallowed():
df = pd.DataFrame({"A": [1, 2]})
with pytest.raises(NotImplementedError, match=""):
with pytest.raises(NotImplementedError, match="^$"):
# deprecation enforced in 2.0
np.subtract.outer(df, df)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ def test_slice_locs_na(self):

def test_slice_locs_na_raises(self):
index = Index([np.nan, 1, 2])
with pytest.raises(KeyError, match=""):
with pytest.raises(KeyError, match="1.5"):
index.slice_locs(start=1.5)

with pytest.raises(KeyError, match=""):
with pytest.raises(KeyError, match="1.5"):
index.slice_locs(end=1.5)


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ def test_drop_by_str_label(self, index):
)
@pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]])
def test_drop_by_str_label_raises_missing_keys(self, index, keys):
with pytest.raises(KeyError, match=""):
with pytest.raises(KeyError, match=".* not found in axis"):
index.drop(keys)

@pytest.mark.parametrize(
Expand Down Expand Up @@ -741,7 +741,7 @@ def test_drop_by_numeric_label_loc(self):

def test_drop_by_numeric_label_raises_missing_keys(self):
index = Index([1, 2, 3])
with pytest.raises(KeyError, match=""):
with pytest.raises(KeyError, match=re.escape("[4] not found in axis")):
index.drop([3, 4])

@pytest.mark.parametrize(
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/indexing/multiindex/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ def test_series_getitem_returns_scalar(
(lambda s: s[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"),
(lambda s: s.loc[(2000, 3, 4)], KeyError, r"^\(2000, 3, 4\)$"),
(lambda s: s.loc[(2000, 3, 4, 5)], IndexingError, "Too many indexers"),
(lambda s: s.__getitem__(len(s)), KeyError, ""), # match should include len(s)
(lambda s: s[len(s)], KeyError, ""), # match should include len(s)
(
lambda s: s.__getitem__(len(s)),
KeyError,
"100",
), # match should include len(s)
(lambda s: s[len(s)], KeyError, "100"), # match should include len(s)
(
lambda s: s.iloc[len(s)],
IndexError,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_empty_array(self):
)
def test_accepted_input(self, data, record_path, exception_type):
if exception_type is not None:
with pytest.raises(exception_type, match=""):
with pytest.raises(exception_type, match="^$"):
json_normalize(data, record_path=record_path)
else:
result = json_normalize(data, record_path=record_path)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/common/test_chunksize.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_read_chunksize_and_nrows_changing_size(all_parsers):
tm.assert_frame_equal(reader.get_chunk(size=2), expected.iloc[:2])
tm.assert_frame_equal(reader.get_chunk(size=4), expected.iloc[2:5])

with pytest.raises(StopIteration, match=""):
with pytest.raises(StopIteration, match="^$"):
reader.get_chunk(size=3)


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ def test_api_chunksize_read(conn, request):

# reading the query in chunks with read_sql_query
if conn_name == "sqlite_buildin":
with pytest.raises(NotImplementedError, match=""):
with pytest.raises(NotImplementedError, match="^$"):
sql.read_sql_table("test_chunksize", conn, chunksize=5)
else:
res3 = DataFrame()
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
mpl = pytest.importorskip("matplotlib")
plt = pytest.importorskip("matplotlib.pyplot")

pytestmark = [
pytest.mark.filterwarnings(
"ignore:divide by zero encountered in scalar divide:RuntimeWarning"
),
pytest.mark.filterwarnings(
"ignore:invalid value encountered in scalar multiply:RuntimeWarning"
),
]


class TestDataFramePlots:
@pytest.mark.slow
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
from pandas.plotting._matplotlib.converter import DatetimeConverter
from pandas.plotting._matplotlib.style import get_standard_colors

pytestmark = [
pytest.mark.filterwarnings(
"ignore:divide by zero encountered in scalar divide:RuntimeWarning"
),
pytest.mark.filterwarnings(
"ignore:invalid value encountered in scalar multiply:RuntimeWarning"
),
]


@pytest.fixture
def ts():
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def test_outer():
ser = pd.Series([1, 2, 3])
obj = np.array([1, 2, 3])

with pytest.raises(NotImplementedError, match=""):
with pytest.raises(NotImplementedError, match="^$"):
np.subtract.outer(ser, obj)


Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ filterwarnings = [
"error:::pandas",
"error::ResourceWarning",
"error::pytest.PytestUnraisableExceptionWarning",
"error::pytest.PytestWarning",
# e.g. Module already imported so cannot be rewritten; _hypothesis_globals
"ignore::pytest.PytestAssertRewriteWarning",
"ignore::pytest.PytestCacheWarning",
# TODO(PY311-minimum): Specify EncodingWarning
# Ignore 3rd party EncodingWarning but raise on pandas'
"ignore:.*encoding.* argument not specified",
Expand Down
Loading