diff --git a/CHANGELOG.md b/CHANGELOG.md index 3867b46a3..e9593aa60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [Fix] Improve error when passing a non-identifier to start a connection (#764) * [Fix] Display a warning (instead of raising an error) if the `default` connection in the `.ini` file cannot start * [Fix] Display a message instead of an error when `toml` isn't installed and `pyproject.toml` is found (#825) +* [Fix] Show deprecation warnings for legacy plot API (#513) ## 0.10.0 (2023-08-19) diff --git a/src/sql/run/resultset.py b/src/sql/run/resultset.py index 5e79a00e7..77304fb32 100644 --- a/src/sql/run/resultset.py +++ b/src/sql/run/resultset.py @@ -14,6 +14,8 @@ from sql.run.table import CustomPrettyTable from sql._current import _config_feedback_all +import warnings + class ResultSet(ColumnGuesserMixin): """ @@ -282,6 +284,14 @@ def pie(self, key_word_sep=" ", title=None, **kwargs): Any additional keyword arguments will be passed through to ``matplotlib.pylab.pie``. """ + warnings.warn( + ( + ".pie() is deprecated and will be removed in a future version. " + "Use %sqlplot pie instead. " + "For more help, find us at https://ploomber.io/community " + ), + UserWarning, + ) self.guess_pie_columns(xlabel_sep=key_word_sep) import matplotlib.pylab as plt @@ -310,6 +320,14 @@ def plot(self, title=None, **kwargs): Any additional keyword arguments will be passed through to ``matplotlib.pylab.plot``. """ + warnings.warn( + ( + ".plot() is deprecated and will be removed in a future version. " + "For more help, find us at https://ploomber.io/community " + ), + UserWarning, + ) + import matplotlib.pylab as plt self.guess_plot_columns() @@ -351,6 +369,15 @@ def bar(self, key_word_sep=" ", title=None, **kwargs): Any additional keyword arguments will be passed through to ``matplotlib.pylab.bar``. """ + warnings.warn( + ( + ".bar() is deprecated and will be removed in a future version. " + "Use %sqlplot bar instead. " + "For more help, find us at https://ploomber.io/community " + ), + UserWarning, + ) + import matplotlib.pylab as plt ax = plt.gca() diff --git a/src/tests/test_resultset.py b/src/tests/test_resultset.py index 953ddfbaa..88a8689f4 100644 --- a/src/tests/test_resultset.py +++ b/src/tests/test_resultset.py @@ -15,6 +15,8 @@ from sql.run.resultset import ResultSet from sql.connection.connection import IS_SQLALCHEMY_ONE +import warnings + @pytest.fixture def config(): @@ -635,3 +637,56 @@ def test_doesnt_refresh_sqlaproxy_if_different_connection(): list(first_set) assert id(first_set._sqlaproxy) == original_id + + +@pytest.fixture +def result_warnings(): + df = pd.DataFrame({range(3), range(4, 7)}) # noqa + engine = sqlalchemy.create_engine("duckdb://") + + conn = SQLAlchemyConnection(engine) + result = conn.raw_execute("select * from df") + + yield result, conn + conn.close() + + +@pytest.fixture +def result_set_warnings(result_warnings, config): + result_set_warn, conn = result_warnings + return ResultSet(result_set_warn, config, statement="select * from df", conn=conn) + + +@pytest.mark.parametrize( + "function, expected_warning", + [ + ( + "pie", + ( + ".pie() is deprecated and will be removed in a future version. " + "Use %sqlplot pie instead. " + "For more help, find us at https://ploomber.io/community " + ), + ), + ( + "bar", + ( + ".bar() is deprecated and will be removed in a future version. " + "Use %sqlplot bar instead. " + "For more help, find us at https://ploomber.io/community " + ), + ), + ( + "plot", + ( + ".plot() is deprecated and will be removed in a future version. " + "For more help, find us at https://ploomber.io/community " + ), + ), + ], +) +def test_deprecated_warnings(result_set_warnings, function, expected_warning): + with warnings.catch_warnings(record=True) as record: + getattr(result_set_warnings, function)() + assert len(record) == 1 + assert str(record[0].message) == expected_warning