Skip to content

Commit db2d239

Browse files
committed
feat: add deprecation warnings and alias handling for auto_register_python_variables in SessionContext
1 parent 92dde5b commit db2d239

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

python/datafusion/context.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import re
2525
import warnings
2626
from functools import cache
27-
from typing import TYPE_CHECKING, Any, Iterator, Protocol
27+
from typing import TYPE_CHECKING, Any, Protocol
2828

2929
try:
3030
from warnings import deprecated # Python 3.13+
@@ -64,6 +64,13 @@ def _load_optional_module(module_name: str) -> Any | None:
6464
return None
6565

6666

67+
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED = (
68+
"SessionContext.auto_register_python_variables is deprecated; use "
69+
"SessionContext.set_python_table_lookup() or the "
70+
"'auto_register_python_objects' keyword argument instead."
71+
)
72+
73+
6774
class ArrowStreamExportable(Protocol):
6875
"""Type hint for object exporting Arrow C Stream via Arrow PyCapsule Interface.
6976
@@ -503,7 +510,8 @@ def __init__(
503510
config: SessionConfig | None = None,
504511
runtime: RuntimeEnvBuilder | None = None,
505512
*,
506-
auto_register_python_objects: bool = True,
513+
auto_register_python_objects: bool | None = None,
514+
auto_register_python_variables: bool | None = None,
507515
) -> None:
508516
"""Main interface for executing queries with DataFusion.
509517
@@ -517,6 +525,9 @@ def __init__(
517525
auto_register_python_objects: Automatically register referenced
518526
Python objects (such as pandas or PyArrow data) when ``sql``
519527
queries reference them by name.
528+
auto_register_python_variables: Deprecated alias for
529+
``auto_register_python_objects``. When provided, it overrides
530+
the automatic registration behavior.
520531
521532
Example usage:
522533
@@ -532,7 +543,33 @@ def __init__(
532543
config.config_internal if config is not None else None,
533544
runtime.config_internal if runtime is not None else None,
534545
)
535-
self._auto_python_table_lookup = auto_register_python_objects
546+
if auto_register_python_variables is not None:
547+
warnings.warn(
548+
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED,
549+
DeprecationWarning,
550+
stacklevel=2,
551+
)
552+
553+
if (
554+
auto_register_python_objects is not None
555+
and auto_register_python_variables is not None
556+
and auto_register_python_objects != auto_register_python_variables
557+
):
558+
conflict_message = (
559+
"auto_register_python_objects and auto_register_python_variables "
560+
"were provided with conflicting values."
561+
)
562+
raise ValueError(conflict_message)
563+
564+
if auto_register_python_objects is None:
565+
if auto_register_python_variables is None:
566+
auto_python_table_lookup = True
567+
else:
568+
auto_python_table_lookup = auto_register_python_variables
569+
else:
570+
auto_python_table_lookup = auto_register_python_objects
571+
572+
self._auto_python_table_lookup = auto_python_table_lookup
536573

537574
def __repr__(self) -> str:
538575
"""Print a string representation of the Session Context."""
@@ -579,6 +616,25 @@ def set_python_table_lookup(self, enabled: bool = True) -> SessionContext:
579616
self._auto_python_table_lookup = enabled
580617
return self
581618

619+
@property
620+
def auto_register_python_variables(self) -> bool:
621+
"""Deprecated alias for :py:meth:`set_python_table_lookup`."""
622+
warnings.warn(
623+
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED,
624+
DeprecationWarning,
625+
stacklevel=2,
626+
)
627+
return getattr(self, "_auto_python_table_lookup", True)
628+
629+
@auto_register_python_variables.setter
630+
def auto_register_python_variables(self, enabled: bool) -> None:
631+
warnings.warn(
632+
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED,
633+
DeprecationWarning,
634+
stacklevel=2,
635+
)
636+
self.set_python_table_lookup(enabled)
637+
582638
def register_object_store(
583639
self, schema: str, store: Any, host: str | None = None
584640
) -> None:

python/tests/test_context.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,10 @@ def test_from_pylist(ctx):
257257

258258

259259
def test_sql_missing_table_without_auto_register(ctx):
260+
ctx.set_python_table_lookup(False)
260261
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
261262

262-
with pytest.raises(Exception, match="not found") as excinfo:
263+
with pytest.raises(Exception, match="not found|No table named") as excinfo:
263264
ctx.sql("SELECT * FROM arrow_table").collect()
264265

265266
# Test that our extraction method works correctly
@@ -319,6 +320,39 @@ def test_sql_auto_register_polars_dataframe():
319320
assert result[0].column(0).to_pylist()[0] == 2
320321

321322

323+
def test_session_context_constructor_alias_disables_lookup():
324+
with pytest.deprecated_call():
325+
ctx = SessionContext(auto_register_python_variables=False)
326+
327+
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
328+
329+
with pytest.raises(Exception, match="not found|No table named"):
330+
ctx.sql("SELECT * FROM arrow_table").collect()
331+
332+
with pytest.deprecated_call():
333+
assert ctx.auto_register_python_variables is False
334+
335+
336+
def test_session_context_property_alias_setter_enables_lookup():
337+
ctx = SessionContext(auto_register_python_objects=False)
338+
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
339+
340+
with pytest.raises(Exception, match="not found|No table named"):
341+
ctx.sql("SELECT COUNT(*) FROM arrow_table").collect()
342+
343+
with pytest.deprecated_call():
344+
ctx.auto_register_python_variables = True
345+
346+
result = ctx.sql(
347+
"SELECT SUM(value) AS total FROM arrow_table",
348+
).collect()
349+
350+
assert result[0].column(0).to_pylist()[0] == 6
351+
352+
with pytest.deprecated_call():
353+
assert ctx.auto_register_python_variables is True
354+
355+
322356
def test_from_pydict(ctx):
323357
# create a dataframe from Python dictionary
324358
data = {"a": [1, 2, 3], "b": [4, 5, 6]}

0 commit comments

Comments
 (0)