Skip to content

Commit 15b5cec

Browse files
committed
refactor: replace auto_register_python_variables with auto_register_python_objects in SessionContext
1 parent ac1d6e1 commit 15b5cec

File tree

2 files changed

+23
-114
lines changed

2 files changed

+23
-114
lines changed

python/datafusion/context.py

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353

5454
from datafusion.plan import ExecutionPlan, LogicalPlan
5555

56-
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED = (
57-
"SessionContext.auto_register_python_variables is deprecated; use "
58-
"SessionContext.set_python_table_lookup() or the "
59-
"'auto_register_python_objects' keyword argument instead."
60-
)
61-
6256

6357
class ArrowStreamExportable(Protocol):
6458
"""Type hint for object exporting Arrow C Stream via Arrow PyCapsule Interface.
@@ -500,7 +494,6 @@ def __init__(
500494
runtime: RuntimeEnvBuilder | None = None,
501495
*,
502496
auto_register_python_objects: bool | None = None,
503-
auto_register_python_variables: bool | None = None,
504497
) -> None:
505498
"""Main interface for executing queries with DataFusion.
506499
@@ -517,9 +510,6 @@ def __init__(
517510
the value configured via
518511
:py:meth:`~datafusion.SessionConfig.with_python_table_lookup`
519512
(``False`` unless explicitly enabled).
520-
auto_register_python_variables: Deprecated alias for
521-
``auto_register_python_objects``. When provided, it overrides
522-
the automatic registration behavior.
523513
524514
Example usage:
525515
@@ -536,29 +526,9 @@ def __init__(
536526
runtime.config_internal if runtime is not None else None,
537527
)
538528

539-
if auto_register_python_variables is not None:
540-
warnings.warn(
541-
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED,
542-
DeprecationWarning,
543-
stacklevel=2,
544-
)
545-
546-
if (
547-
auto_register_python_variables is not None
548-
and auto_register_python_objects is not None
549-
and auto_register_python_objects != auto_register_python_variables
550-
):
551-
conflict_message = (
552-
"auto_register_python_objects and auto_register_python_variables "
553-
"were provided with conflicting values."
554-
)
555-
raise ValueError(conflict_message)
556-
557529
# Determine the final value for python table lookup
558530
if auto_register_python_objects is not None:
559531
auto_python_table_lookup = auto_register_python_objects
560-
elif auto_register_python_variables is not None:
561-
auto_python_table_lookup = auto_register_python_variables
562532
else:
563533
# Default to session config value or False if not configured
564534
auto_python_table_lookup = getattr(config, "_python_table_lookup", False)
@@ -596,9 +566,7 @@ def enable_url_table(self) -> SessionContext:
596566
obj._auto_python_table_lookup = getattr(
597567
self, "_auto_python_table_lookup", False
598568
)
599-
obj._python_table_bindings = getattr(
600-
self, "_python_table_bindings", {}
601-
).copy()
569+
obj._python_table_bindings = getattr(self, "_python_table_bindings", {}).copy()
602570
return obj
603571

604572
def set_python_table_lookup(self, enabled: bool = True) -> SessionContext:
@@ -616,25 +584,6 @@ def set_python_table_lookup(self, enabled: bool = True) -> SessionContext:
616584
self._auto_python_table_lookup = enabled
617585
return self
618586

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 bool(getattr(self, "_auto_python_table_lookup", False))
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(bool(enabled))
637-
638587
def register_object_store(
639588
self, schema: str, store: Any, host: str | None = None
640589
) -> None:
@@ -792,34 +741,29 @@ def _register_python_tables(self, tables: list[str]) -> bool:
792741
def _lookup_python_object(name: str) -> Any | None:
793742
frame = inspect.currentframe()
794743
try:
795-
if frame is not None:
796-
frame = frame.f_back
744+
frame = frame.f_back if frame is not None else None
797745
lower_name = name.lower()
798746

799747
def _match(mapping: dict[str, Any]) -> Any | None:
800-
if not mapping:
801-
return None
802-
803748
value = mapping.get(name)
804749
if value is not None:
805750
return value
806751

807752
for key, candidate in mapping.items():
808-
if isinstance(key, str) and key.lower() == lower_name:
809-
if candidate is not None:
810-
return candidate
753+
if (
754+
isinstance(key, str)
755+
and key.lower() == lower_name
756+
and candidate is not None
757+
):
758+
return candidate
811759

812760
return None
813761

814762
while frame is not None:
815-
locals_dict = frame.f_locals
816-
match = _match(locals_dict)
817-
if match is not None:
818-
return match
819-
globals_dict = frame.f_globals
820-
match = _match(globals_dict)
821-
if match is not None:
822-
return match
763+
for scope in (frame.f_locals, frame.f_globals):
764+
match = _match(scope)
765+
if match is not None:
766+
return match
823767
frame = frame.f_back
824768
finally:
825769
del frame

python/tests/test_context.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def __init__(self) -> None:
295295

296296

297297
def test_sql_auto_register_arrow_table():
298-
ctx = SessionContext(auto_register_python_variables=True)
298+
ctx = SessionContext(auto_register_python_objects=True)
299299
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
300300

301301
result = ctx.sql(
@@ -326,9 +326,7 @@ def test_sql_auto_register_multiple_tables_single_query():
326326
).collect()
327327

328328
actual = pa.Table.from_batches(result)
329-
expected = pa.Table.from_pydict(
330-
{"customer_id": [1, 2], "order_id": [100, 200]}
331-
)
329+
expected = pa.Table.from_pydict({"customer_id": [1, 2], "order_id": [100, 200]})
332330

333331
assert actual.equals(expected)
334332
assert ctx.table_exist("customers")
@@ -337,7 +335,7 @@ def test_sql_auto_register_multiple_tables_single_query():
337335

338336
def test_sql_auto_register_arrow_outer_scope():
339337
ctx = SessionContext()
340-
ctx.auto_register_python_variables = True
338+
ctx.set_python_table_lookup(True)
341339
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3, 4]}) # noqa: F841
342340

343341
def run_query():
@@ -365,7 +363,7 @@ def run_query():
365363

366364
def test_sql_auto_register_case_insensitive_lookup():
367365
ctx = SessionContext(auto_register_python_objects=True)
368-
MyTable = pa.Table.from_pydict({"value": [2, 3]}) # noqa: F841
366+
MyTable = pa.Table.from_pydict({"value": [2, 3]}) # noqa: N806,F841
369367

370368
batches = ctx.sql(
371369
"SELECT SUM(value) AS total FROM mytable",
@@ -377,7 +375,7 @@ def test_sql_auto_register_case_insensitive_lookup():
377375
def test_sql_auto_register_pandas_dataframe():
378376
pd = pytest.importorskip("pandas")
379377

380-
ctx = SessionContext(auto_register_python_variables=True)
378+
ctx = SessionContext(auto_register_python_objects=True)
381379
pandas_df = pd.DataFrame({"value": [1, 2, 3, 4]}) # noqa: F841
382380

383381
result = ctx.sql(
@@ -411,7 +409,7 @@ def test_sql_auto_register_refreshes_reassigned_dataframe():
411409
def test_sql_auto_register_polars_dataframe():
412410
pl = pytest.importorskip("polars")
413411

414-
ctx = SessionContext(auto_register_python_variables=True)
412+
ctx = SessionContext(auto_register_python_objects=True)
415413
polars_df = pl.DataFrame({"value": [2, 4, 6]}) # noqa: F841
416414

417415
result = ctx.sql(
@@ -421,39 +419,6 @@ def test_sql_auto_register_polars_dataframe():
421419
assert result[0].column(0).to_pylist()[0] == 2
422420

423421

424-
def test_session_context_constructor_alias_disables_lookup():
425-
with pytest.deprecated_call():
426-
ctx = SessionContext(auto_register_python_variables=False)
427-
428-
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
429-
430-
with pytest.raises(Exception, match="not found|No table named"):
431-
ctx.sql("SELECT * FROM arrow_table").collect()
432-
433-
with pytest.deprecated_call():
434-
assert ctx.auto_register_python_variables is False
435-
436-
437-
def test_session_context_property_alias_setter_enables_lookup():
438-
ctx = SessionContext(auto_register_python_objects=False)
439-
arrow_table = pa.Table.from_pydict({"value": [1, 2, 3]}) # noqa: F841
440-
441-
with pytest.raises(Exception, match="not found|No table named"):
442-
ctx.sql("SELECT COUNT(*) FROM arrow_table").collect()
443-
444-
with pytest.deprecated_call():
445-
ctx.auto_register_python_variables = True
446-
447-
result = ctx.sql(
448-
"SELECT SUM(value) AS total FROM arrow_table",
449-
).collect()
450-
451-
assert result[0].column(0).to_pylist()[0] == 6
452-
453-
with pytest.deprecated_call():
454-
assert ctx.auto_register_python_variables is True
455-
456-
457422
def test_from_pydict(ctx):
458423
# create a dataframe from Python dictionary
459424
data = {"a": [1, 2, 3], "b": [4, 5, 6]}
@@ -486,7 +451,7 @@ def test_from_pandas(ctx):
486451

487452
def test_sql_from_local_arrow_table(ctx):
488453
ctx.set_python_table_lookup(True) # Enable implicit table lookup
489-
arrow_table = pa.Table.from_pydict({"a": [1, 2], "b": ["x", "y"]})
454+
arrow_table = pa.Table.from_pydict({"a": [1, 2], "b": ["x", "y"]}) # noqa: F841
490455

491456
result = ctx.sql("SELECT * FROM arrow_table ORDER BY a").collect()
492457
actual = pa.Table.from_batches(result)
@@ -498,7 +463,7 @@ def test_sql_from_local_arrow_table(ctx):
498463
def test_sql_from_local_pandas_dataframe(ctx):
499464
ctx.set_python_table_lookup(True) # Enable implicit table lookup
500465
pd = pytest.importorskip("pandas")
501-
pandas_df = pd.DataFrame({"a": [3, 1], "b": ["z", "y"]})
466+
pandas_df = pd.DataFrame({"a": [3, 1], "b": ["z", "y"]}) # noqa: F841
502467

503468
result = ctx.sql("SELECT * FROM pandas_df ORDER BY a").collect()
504469
actual = pa.Table.from_batches(result)
@@ -510,7 +475,7 @@ def test_sql_from_local_pandas_dataframe(ctx):
510475
def test_sql_from_local_polars_dataframe(ctx):
511476
ctx.set_python_table_lookup(True) # Enable implicit table lookup
512477
pl = pytest.importorskip("polars")
513-
polars_df = pl.DataFrame({"a": [2, 1], "b": ["beta", "alpha"]})
478+
polars_df = pl.DataFrame({"a": [2, 1], "b": ["beta", "alpha"]}) # noqa: F841
514479

515480
result = ctx.sql("SELECT * FROM polars_df ORDER BY a").collect()
516481
actual = pa.Table.from_batches(result)
@@ -520,7 +485,7 @@ def test_sql_from_local_polars_dataframe(ctx):
520485

521486

522487
def test_sql_from_local_unsupported_object(ctx):
523-
unsupported = object()
488+
unsupported = object() # noqa: F841
524489

525490
with pytest.raises(Exception, match="table 'unsupported' not found"):
526491
ctx.sql("SELECT * FROM unsupported").collect()
@@ -876,7 +841,7 @@ def test_sql_with_options_no_statements(ctx):
876841
def test_session_config_python_table_lookup_enables_auto_registration():
877842
pd = pytest.importorskip("pandas")
878843

879-
ctx = SessionContext(config=SessionConfig().with_python_table_lookup(True))
844+
ctx = SessionContext(config=SessionConfig().with_python_table_lookup(enabled=True))
880845
pdf = pd.DataFrame({"value": [1, 2, 3]})
881846
assert len(pdf) == 3
882847

0 commit comments

Comments
 (0)