diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-05-04 13:31:51 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-05-04 13:49:43 -0400 |
commit | d6ec248fa7083fee93b669ab27474f6c8f69944d (patch) | |
tree | 6f244ef0bde129a4519ca4439e25d8150a58b2b3 /lib/sqlalchemy | |
parent | f1f4f466fd809a14bff6d0c405a1d5da87438379 (diff) | |
download | sqlalchemy-d6ec248fa7083fee93b669ab27474f6c8f69944d.tar.gz |
Establish deprecation path for CursorResult.keys()
Established a deprecation path for calling upon the
:meth:`_cursor.CursorResult.keys` method for a statement that returns no
rows to provide support for legacy patterns used by the "records" package
as well as any other non-migrated applications. Previously, this would
raise :class:`.ResourceClosedException` unconditionally in the same way as
it does when attempting to fetch rows. While this is the correct behavior
going forward, the :class:`_cursor.LegacyCursorResult` object will now in
this case return an empty list for ``.keys()`` as it did in 1.3, while also
emitting a 2.0 deprecation warning. The :class:`_cursor.CursorResult`, used
when using a 2.0-style "future" engine, will continue to raise as it does
now.
Fixes: #6427
Change-Id: I4148f28c88039e4141deeab28b1a5994e6d6e098
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/engine/cursor.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/sqlalchemy/engine/cursor.py b/lib/sqlalchemy/engine/cursor.py index ff9dd0d6a..cf77d0835 100644 --- a/lib/sqlalchemy/engine/cursor.py +++ b/lib/sqlalchemy/engine/cursor.py @@ -1201,7 +1201,19 @@ class _NoResultMetaData(ResultMetaData): self._we_dont_return_rows() +class _LegacyNoResultMetaData(_NoResultMetaData): + @property + def keys(self): + util.warn_deprecated_20( + "Calling the .keys() method on a result set that does not return " + "rows is deprecated and will raise ResourceClosedError in " + "SQLAlchemy 2.0.", + ) + return [] + + _NO_RESULT_METADATA = _NoResultMetaData() +_LEGACY_NO_RESULT_METADATA = _LegacyNoResultMetaData() class BaseCursorResult(object): @@ -1259,7 +1271,7 @@ class BaseCursorResult(object): self._set_memoized_attribute("_row_getter", make_row) else: - self._metadata = _NO_RESULT_METADATA + self._metadata = self._no_result_metadata def _init_metadata(self, context, cursor_description): @@ -1763,6 +1775,7 @@ class CursorResult(BaseCursorResult, Result): _cursor_metadata = CursorResultMetaData _cursor_strategy_cls = CursorFetchStrategy + _no_result_metadata = _NO_RESULT_METADATA def _fetchiter_impl(self): fetchone = self.cursor_strategy.fetchone @@ -1838,6 +1851,8 @@ class LegacyCursorResult(CursorResult): _cursor_metadata = LegacyCursorResultMetaData _cursor_strategy_cls = CursorFetchStrategy + _no_result_metadata = _LEGACY_NO_RESULT_METADATA + def close(self): """Close this :class:`_engine.LegacyCursorResult`. |