Skip to content
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: 0 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,3 @@ jobs:

- name: Run Tests
uses: ./.github/actions/run-tests
# TEMP allow this to fail until we fixed all test failures (related to chained assignment warnings)
continue-on-error: true
11 changes: 7 additions & 4 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

from pandas._config import using_copy_on_write

from pandas.compat import PYPY
from pandas.compat import (
PYPY,
WARNING_CHECK_DISABLED,
)
from pandas.errors import ChainedAssignmentError

from pandas import set_option
Expand Down Expand Up @@ -204,11 +207,11 @@ def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()

return nullcontext()

if PYPY and not extra_warnings:
if (PYPY or WARNING_CHECK_DISABLED) and not extra_warnings:
from contextlib import nullcontext

return nullcontext()
elif PYPY and extra_warnings:
elif (PYPY or WARNING_CHECK_DISABLED) and extra_warnings:
return assert_produces_warning(
extra_warnings,
match="|".join(extra_match),
Expand Down Expand Up @@ -247,7 +250,7 @@ def assert_cow_warning(warn=True, match=None, **kwargs):
"""
from pandas._testing import assert_produces_warning

if not warn:
if not warn or WARNING_CHECK_DISABLED:
from contextlib import nullcontext

return nullcontext()
Expand Down
2 changes: 2 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PY312,
PY314,
PYPY,
WARNING_CHECK_DISABLED,
)
import pandas.compat.compressors
from pandas.compat.numpy import is_numpy_dev
Expand Down Expand Up @@ -208,4 +209,5 @@ def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
"PY312",
"PY314",
"PYPY",
"WARNING_CHECK_DISABLED",
]
2 changes: 2 additions & 0 deletions pandas/compat/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
PYPY = platform.python_implementation() == "PyPy"
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")
REF_COUNT = 2 if PY311 else 3
WARNING_CHECK_DISABLED = PY314


__all__ = [
"IS64",
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
from pandas._libs.hashtable import duplicated
from pandas._libs.lib import is_range_indexer
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat._constants import (
REF_COUNT,
WARNING_CHECK_DISABLED,
)
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -4274,12 +4277,12 @@ def isetitem(self, loc, value) -> None:
self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs)

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= 3:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
elif not PYPY and not WARNING_CHECK_DISABLED and not using_copy_on_write():
if sys.getrefcount(self) <= 3 and (
warn_copy_on_write()
or (
Expand Down Expand Up @@ -8983,14 +8986,19 @@ def update(
2 3 6.0
"""

if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
stacklevel=2,
)
elif not PYPY and not using_copy_on_write() and self._is_view_after_cow_rules():
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_warning_method_msg,
Expand Down
29 changes: 20 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@
npt,
)
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat._constants import (
REF_COUNT,
WARNING_CHECK_DISABLED,
)
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -7285,7 +7288,7 @@ def fillna(
"""
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -7294,6 +7297,7 @@ def fillna(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -7588,7 +7592,7 @@ def ffill(
downcast = self._deprecate_downcast(downcast, "ffill")
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -7597,6 +7601,7 @@ def ffill(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -7792,7 +7797,7 @@ def bfill(
downcast = self._deprecate_downcast(downcast, "bfill")
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -7801,6 +7806,7 @@ def bfill(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -7963,7 +7969,7 @@ def replace(

inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -7972,6 +7978,7 @@ def replace(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -8415,7 +8422,7 @@ def interpolate(
inplace = validate_bool_kwarg(inplace, "inplace")

if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -8424,6 +8431,7 @@ def interpolate(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -9057,7 +9065,7 @@ def clip(
inplace = validate_bool_kwarg(inplace, "inplace")

if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -9066,6 +9074,7 @@ def clip(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -10975,7 +10984,7 @@ def where(
"""
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -10984,6 +10993,7 @@ def where(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down Expand Up @@ -11058,7 +11068,7 @@ def mask(
) -> Self | None:
inplace = validate_bool_kwarg(inplace, "inplace")
if inplace:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
Expand All @@ -11067,6 +11077,7 @@ def mask(
)
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
Expand Down
13 changes: 7 additions & 6 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pandas._libs.indexing import NDFrameIndexerBase
from pandas._libs.lib import item_from_zerodim
from pandas.compat import PYPY
from pandas.compat._constants import WARNING_CHECK_DISABLED
from pandas.errors import (
AbstractMethodError,
ChainedAssignmentError,
Expand Down Expand Up @@ -881,12 +882,12 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:

@final
def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
elif not PYPY and not WARNING_CHECK_DISABLED and not using_copy_on_write():
ctr = sys.getrefcount(self.obj)
ref_count = 2
if not warn_copy_on_write() and _check_cacher(self.obj):
Expand Down Expand Up @@ -2575,12 +2576,12 @@ def __getitem__(self, key):
return super().__getitem__(key)

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
elif not PYPY and not WARNING_CHECK_DISABLED and not using_copy_on_write():
ctr = sys.getrefcount(self.obj)
ref_count = 2
if not warn_copy_on_write() and _check_cacher(self.obj):
Expand Down Expand Up @@ -2616,12 +2617,12 @@ def _convert_key(self, key):
return key

def __setitem__(self, key, value) -> None:
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self.obj) <= 2:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
elif not PYPY and not WARNING_CHECK_DISABLED and not using_copy_on_write():
ctr = sys.getrefcount(self.obj)
ref_count = 2
if not warn_copy_on_write() and _check_cacher(self.obj):
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
)
from pandas._libs.lib import is_range_indexer
from pandas.compat import PYPY
from pandas.compat._constants import REF_COUNT
from pandas.compat._constants import (
REF_COUNT,
WARNING_CHECK_DISABLED,
)
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
from pandas.errors import (
Expand Down Expand Up @@ -1269,12 +1272,12 @@ def _get_value(self, label, takeable: bool = False):

def __setitem__(self, key, value) -> None:
warn = True
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= 3:
warnings.warn(
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
)
elif not PYPY and not using_copy_on_write():
elif not PYPY and not WARNING_CHECK_DISABLED and not using_copy_on_write():
ctr = sys.getrefcount(self)
ref_count = 3
if not warn_copy_on_write() and _check_cacher(self):
Expand Down Expand Up @@ -3621,14 +3624,19 @@ def update(self, other: Series | Sequence | Mapping) -> None:
2 3
dtype: int64
"""
if not PYPY and using_copy_on_write():
if not PYPY and not WARNING_CHECK_DISABLED and using_copy_on_write():
if sys.getrefcount(self) <= REF_COUNT:
warnings.warn(
_chained_assignment_method_msg,
ChainedAssignmentError,
stacklevel=2,
)
elif not PYPY and not using_copy_on_write() and self._is_view_after_cow_rules():
elif (
not PYPY
and not WARNING_CHECK_DISABLED
and not using_copy_on_write()
and self._is_view_after_cow_rules()
):
ctr = sys.getrefcount(self)
ref_count = REF_COUNT
if _check_cacher(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import pytest

from pandas.compat import PY311
from pandas.compat import (
PY311,
WARNING_CHECK_DISABLED,
)
from pandas.errors import (
ChainedAssignmentError,
SettingWithCopyWarning,
Expand Down Expand Up @@ -150,6 +153,9 @@ def test_series_setitem(indexer, using_copy_on_write, warn_copy_on_write):

# using custom check instead of tm.assert_produces_warning because that doesn't
# fail if multiple warnings are raised
if WARNING_CHECK_DISABLED:
return

with pytest.warns() as record:
df["a"][indexer] = 0
assert len(record) == 1
Expand Down
Loading
Loading