Skip to content

Commit be51736

Browse files
committed
fix: synchronize stacklevel for deprecation warnings in TableProvider
1 parent 82bc41a commit be51736

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

python/datafusion/table_provider.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
_InternalTableProvider = df_internal.TableProvider
2828

29+
# Keep in sync with ``datafusion._internal.TableProvider.from_view``.
30+
_FROM_VIEW_WARN_STACKLEVEL = 2
31+
2932

3033
class TableProvider:
3134
"""High level wrapper around :mod:`datafusion._internal.TableProvider`."""
@@ -53,10 +56,9 @@ def from_dataframe(cls, df: Any) -> TableProvider:
5356
"""Create a :class:`TableProvider` from a :class:`DataFrame`."""
5457
from datafusion.dataframe import DataFrame as DataFrameWrapper
5558

56-
if isinstance(df, DataFrameWrapper):
57-
dataframe = df
58-
else:
59-
dataframe = DataFrameWrapper(df)
59+
dataframe = (
60+
df if isinstance(df, DataFrameWrapper) else DataFrameWrapper(df)
61+
)
6062

6163
provider = dataframe.into_view()
6264
if isinstance(provider, cls):
@@ -78,8 +80,8 @@ def from_view(cls, df: Any) -> TableProvider:
7880
warnings.warn(
7981
"TableProvider.from_view is deprecated; use DataFrame.into_view or "
8082
"TableProvider.from_dataframe instead.",
81-
DeprecationWarning,
82-
stacklevel=2,
83+
category=DeprecationWarning,
84+
stacklevel=_FROM_VIEW_WARN_STACKLEVEL,
8385
)
8486
return cls(provider)
8587

python/tests/test_context.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
# under the License.
1717
import datetime as dt
1818
import gzip
19+
import inspect
1920
import pathlib
21+
import warnings
2022

2123
import pyarrow as pa
2224
import pyarrow.dataset as ds
2325
import pytest
2426
from datafusion import (
25-
DataFrame,
2627
EXPECTED_PROVIDER_MSG,
28+
DataFrame,
2729
RuntimeEnvBuilder,
2830
SessionConfig,
2931
SessionContext,
@@ -358,6 +360,46 @@ def test_table_provider_from_dataframe(ctx):
358360
assert [b.to_pydict() for b in result] == [{"a": [1, 2]}]
359361

360362

363+
def test_table_provider_from_view_warning_origin(ctx):
364+
from datafusion.table_provider import TableProvider as WrapperTableProvider
365+
366+
wrapper_df = ctx.from_pydict({"a": [1]})
367+
test_path = pathlib.Path(__file__).resolve()
368+
369+
with warnings.catch_warnings(record=True) as caught:
370+
warnings.simplefilter("always")
371+
call_lineno = inspect.currentframe().f_lineno + 1
372+
WrapperTableProvider.from_view(wrapper_df)
373+
374+
assert len(caught) >= 1
375+
376+
rust_warning = next(
377+
(
378+
warning
379+
for warning in caught
380+
if "PyTableProvider.from_view()" in str(warning.message)
381+
),
382+
None,
383+
)
384+
assert rust_warning is not None
385+
assert issubclass(rust_warning.category, DeprecationWarning)
386+
assert pathlib.Path(rust_warning.filename).resolve() == test_path
387+
assert rust_warning.lineno == call_lineno
388+
389+
py_warning = next(
390+
(
391+
warning
392+
for warning in caught
393+
if "TableProvider.from_view is deprecated" in str(warning.message)
394+
),
395+
None,
396+
)
397+
assert py_warning is not None
398+
assert issubclass(py_warning.category, DeprecationWarning)
399+
assert pathlib.Path(py_warning.filename).resolve() == test_path
400+
assert py_warning.lineno == call_lineno
401+
402+
361403
def test_register_table_capsule_direct(ctx):
362404
df = ctx.from_pydict({"a": [1, 2]})
363405
provider = df.into_view()

src/table.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ impl PyTableProvider {
9191
#[staticmethod]
9292
pub fn from_view(py: Python<'_>, df: &PyDataFrame) -> PyDataFusionResult<Self> {
9393
let kwargs = PyDict::new(py);
94-
kwargs.set_item("stacklevel", 3)?;
94+
// Keep stack level consistent with python/datafusion/table_provider.py
95+
kwargs.set_item("stacklevel", 2)?;
9596
py.import("warnings")?.call_method(
9697
"warn",
9798
(

0 commit comments

Comments
 (0)