Skip to content

Commit e84db62

Browse files
committed
MNT: use REF_COUNT instead of magic constants
1 parent 30ed65b commit e84db62

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

pandas/compat/_constants.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515

1616
PY311 = sys.version_info >= (3, 11)
1717
PY312 = sys.version_info >= (3, 12)
18+
PY314 = sys.version_info >= (3, 14)
1819
PYPY = platform.python_implementation() == "PyPy"
1920
WASM = (sys.platform == "emscripten") or (platform.machine() in ["wasm32", "wasm64"])
2021
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")
21-
REF_COUNT = 2 if PY311 else 3
22+
if PY311:
23+
REF_COUNT = 2
24+
elif PY314:
25+
REF_COUNT = 1
26+
else:
27+
# Python 3.10 and older
28+
REF_COUNT = 3
2229

2330
__all__ = [
2431
"IS64",

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4297,7 +4297,7 @@ def __setitem__(self, key, value) -> None:
42974297
# Values for 'a' and 'b' are completely ignored!
42984298
"""
42994299
if not PYPY and sys.version_info < (3, 14):
4300-
if sys.getrefcount(self) <= 3:
4300+
if sys.getrefcount(self) <= REF_COUNT + 1:
43014301
warnings.warn(
43024302
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
43034303
)

pandas/core/indexing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas._libs.indexing import NDFrameIndexerBase
1616
from pandas._libs.lib import item_from_zerodim
1717
from pandas.compat import PYPY
18+
from pandas.compat._constants import REF_COUNT
1819
from pandas.errors import (
1920
AbstractMethodError,
2021
ChainedAssignmentError,
@@ -914,7 +915,7 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None) -> None:
914915
@final
915916
def __setitem__(self, key, value) -> None:
916917
if not PYPY and sys.version_info < (3, 14):
917-
if sys.getrefcount(self.obj) <= 2:
918+
if sys.getrefcount(self.obj) <= REF_COUNT:
918919
warnings.warn(
919920
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
920921
)

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ def _get_value(self, label, takeable: bool = False):
10601060

10611061
def __setitem__(self, key, value) -> None:
10621062
if not PYPY and sys.version_info < (3, 14):
1063-
if sys.getrefcount(self) <= 3:
1063+
if sys.getrefcount(self) <= REF_COUNT + 1:
10641064
warnings.warn(
10651065
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
10661066
)

0 commit comments

Comments
 (0)