Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pkg-py/src/querychat/_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ def get_data(self) -> pd.DataFrame:
"""
...

@abstractmethod
def cleanup(self) -> None:
Comment on lines +86 to +87
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring states 'The default implementation does nothing' but this is marked as @abstractmethod, which means subclasses must implement it and there is no default implementation. Remove the sentence about the default implementation or remove the @abstractmethod decorator if a default no-op implementation is intended.

Copilot uses AI. Check for mistakes.
"""
Clean up resources associated with the data source.

This method should clean up any connections or resources used by the
data source.

Returns
-------
None

"""


class DataFrameSource(DataSource):
"""A DataSource implementation that wraps a pandas DataFrame using DuckDB."""
Expand Down Expand Up @@ -212,6 +226,18 @@ def get_data(self) -> pd.DataFrame:
# TODO(@gadenbuie): This should just return `self._df` and not a pandas DataFrame
return self._df.lazy().collect().to_pandas()

def cleanup(self) -> None:
"""
Close the DuckDB connection.

Returns
-------
None

"""
if self._conn:
self._conn.close()


class SQLAlchemySource(DataSource):
"""
Expand Down Expand Up @@ -440,3 +466,15 @@ def _get_sql_type_name(self, type_: sqltypes.TypeEngine) -> str: # noqa: PLR091
def _get_connection(self) -> Connection:
"""Get a connection to use for queries."""
return self._engine.connect()

def cleanup(self) -> None:
"""
Dispose of the SQLAlchemy engine.

Returns
-------
None

"""
if self._engine:
self._engine.dispose()
14 changes: 14 additions & 0 deletions pkg-py/src/querychat/_querychat.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ def data_source(self):
"""
return self._data_source

def cleanup(self) -> None:
"""
Clean up resources associated with the data source.

Call this method when you are done using the QueryChat object to close
database connections and avoid resource leaks.

Returns
-------
None

"""
self._data_source.cleanup()


class QueryChat(QueryChatBase):
"""
Expand Down