Skip to content

Commit dc1b392

Browse files
committed
refactor: clean up unused imports and simplify conditional logic in SessionContext
1 parent 6454b8c commit dc1b392

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

python/datafusion/context.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
from __future__ import annotations
2121

22-
import importlib
2322
import inspect
2423
import re
2524
import warnings
26-
from functools import cache
2725
from typing import TYPE_CHECKING, Any, Protocol
2826

2927
try:
@@ -54,16 +52,6 @@
5452

5553
from datafusion.plan import ExecutionPlan, LogicalPlan
5654

57-
58-
@cache
59-
def _load_optional_module(module_name: str) -> Any | None:
60-
"""Return the module for *module_name* if it can be imported."""
61-
try:
62-
return importlib.import_module(module_name)
63-
except ModuleNotFoundError:
64-
return None
65-
66-
6755
_AUTO_REGISTER_PYTHON_VARIABLES_DEPRECATED = (
6856
"SessionContext.auto_register_python_variables is deprecated; use "
6957
"SessionContext.set_python_table_lookup() or the "
@@ -554,13 +542,16 @@ def __init__(
554542
stacklevel=2,
555543
)
556544

557-
if auto_register_python_variables is not None and auto_register_python_objects is not None:
558-
if auto_register_python_objects != auto_register_python_variables:
559-
conflict_message = (
560-
"auto_register_python_objects and auto_register_python_variables "
561-
"were provided with conflicting values."
562-
)
563-
raise ValueError(conflict_message)
545+
if (
546+
auto_register_python_variables is not None
547+
and auto_register_python_objects is not None
548+
and auto_register_python_objects != auto_register_python_variables
549+
):
550+
conflict_message = (
551+
"auto_register_python_objects and auto_register_python_variables "
552+
"were provided with conflicting values."
553+
)
554+
raise ValueError(conflict_message)
564555

565556
# Determine the final value for python table lookup
566557
if auto_register_python_objects is not None:
@@ -569,9 +560,7 @@ def __init__(
569560
auto_python_table_lookup = auto_register_python_variables
570561
else:
571562
# Default to session config value or False if not configured
572-
auto_python_table_lookup = getattr(
573-
config, "_python_table_lookup", False
574-
)
563+
auto_python_table_lookup = getattr(config, "_python_table_lookup", False)
575564

576565
self._auto_python_table_lookup = bool(auto_python_table_lookup)
577566

@@ -703,12 +692,11 @@ def sql(self, query: str, options: SQLOptions | None = None) -> DataFrame:
703692
Returns:
704693
DataFrame representation of the SQL query.
705694
"""
695+
706696
def _execute_sql() -> DataFrame:
707697
if options is None:
708698
return DataFrame(self.ctx.sql(query))
709-
return DataFrame(
710-
self.ctx.sql_with_options(query, options.options_internal)
711-
)
699+
return DataFrame(self.ctx.sql_with_options(query, options.options_internal))
712700

713701
auto_lookup_enabled = getattr(self, "_auto_python_table_lookup", False)
714702

@@ -829,6 +817,7 @@ def _register_python_object(self, name: str, obj: Any) -> bool:
829817
return True
830818

831819
return False
820+
832821
def create_dataframe(
833822
self,
834823
partitions: list[list[pa.RecordBatch]],

0 commit comments

Comments
 (0)