Blame SOURCES/0002-Fix-evaluations-on-Python-3.12.patch

4898f3
From 76aaa36bfaa7a22cfc2fe49e59400873c0c2c3e2 Mon Sep 17 00:00:00 2001
4898f3
From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
4898f3
Date: Mon, 12 Feb 2024 19:47:57 -0500
4898f3
Subject: [PATCH 2/6] Fix evaluations on Python 3.12
4898f3
4898f3
List comprehensions no longer get their own scope [1], so adding a level
4898f3
to the (eventual) call to `sys._getframe` goes outside the actual
4898f3
caller. If running in `pytest` (so that there is a scope outside the
4898f3
caller), you end up looking in some unrelated scope. If you are running
4898f3
a script, then `sys._getframe` raises an error that the level is out of
4898f3
bounds.
4898f3
4898f3
The `Bitwise operations` warning in `test_scalar_unary` appears to
4898f3
always be raised, so remove the condition.
4898f3
4898f3
[1] https://docs.python.org/3.12/whatsnew/3.12.html#whatsnew312-pep709
4898f3
4898f3
Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
4898f3
---
4898f3
 pandas/io/pytables.py                 | 7 ++++++-
4898f3
 pandas/tests/computation/test_eval.py | 4 +---
4898f3
 2 files changed, 7 insertions(+), 4 deletions(-)
4898f3
4898f3
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
4898f3
index 1139519d2b..b3ed92a725 100644
4898f3
--- a/pandas/io/pytables.py
4898f3
+++ b/pandas/io/pytables.py
4898f3
@@ -40,6 +40,7 @@ from pandas._libs import (
4898f3
 )
4898f3
 from pandas._libs.lib import is_string_array
4898f3
 from pandas._libs.tslibs import timezones
4898f3
+from pandas.compat import PY312
4898f3
 from pandas.compat._optional import import_optional_dependency
4898f3
 from pandas.compat.pickle_compat import patch_pickle
4898f3
 from pandas.errors import (
4898f3
@@ -178,8 +179,12 @@ def _ensure_term(where, scope_level: int):
4898f3
     # list
4898f3
     level = scope_level + 1
4898f3
     if isinstance(where, (list, tuple)):
4898f3
+        # Python 3.12 does not a scope for list comprehensions, but older versions did:
4898f3
+        # https://docs.python.org/3.12/whatsnew/3.12.html#whatsnew312-pep709
4898f3
+        if not PY312:
4898f3
+            level += 1
4898f3
         where = [
4898f3
-            Term(term, scope_level=level + 1) if maybe_expression(term) else term
4898f3
+            Term(term, scope_level=level) if maybe_expression(term) else term
4898f3
             for term in where
4898f3
             if term is not None
4898f3
         ]
4898f3
diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py
4898f3
index 17630f14b0..eab14c3fcd 100644
4898f3
--- a/pandas/tests/computation/test_eval.py
4898f3
+++ b/pandas/tests/computation/test_eval.py
4898f3
@@ -557,9 +557,7 @@ class TestEval:
4898f3
 
4898f3
     def test_scalar_unary(self, engine, parser):
4898f3
         msg = "bad operand type for unary ~: 'float'"
4898f3
-        warn = None
4898f3
-        if PY312 and not (engine == "numexpr" and parser == "pandas"):
4898f3
-            warn = DeprecationWarning
4898f3
+        warn = DeprecationWarning if PY312 else None
4898f3
         with pytest.raises(TypeError, match=msg):
4898f3
             pd.eval("~1.0", engine=engine, parser=parser)
4898f3
 
4898f3
-- 
4898f3
2.43.0
4898f3