summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine/row.py
diff options
context:
space:
mode:
authorAndy Freeland <andy@andyfreeland.net>2023-04-20 13:41:39 -0400
committerFederico Caselli <cfederico87@gmail.com>2023-04-20 20:55:21 +0200
commitc1125288e3c413d868070995b308085d2ddf402e (patch)
treecb8e1e04ed56a67d7ddd9bd19c2e727206fa95c1 /lib/sqlalchemy/engine/row.py
parentfc2bcead435a9bf0a2de8e9b15a1bd835f9d7fe4 (diff)
downloadsqlalchemy-c1125288e3c413d868070995b308085d2ddf402e.tar.gz
Fix `RowMapping`'s `Mapping` type to reflect that it supports `Column`s or strings
### Description I ran into this originally in sqlalchemy2-stubs: https://github.com/sqlalchemy/sqlalchemy2-stubs/pull/251, where `RowMapping` only supported string keys according to the type hints. I ran into a similar issue here upgrading our application where because `RowMapping` subclassed `Mapping[str, Any]`, `Row._mapping.get()` would fail to typecheck when used with `Column` objects. This patch adds a test to verify that `Row._mapping.get()` continues to work with both strings and `Column`s, though it doesn't look like mypy checks types in the tests. Fixes #9644. ### Checklist <!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once) --> This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. **Have a nice day!** Closes: #9643 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9643 Pull-request-sha: 6c33fe534cf457d6b5c73f4830a64880830f0f56 Change-Id: I1009c6defff109d73f13a9e8c51641009e6a79e2
Diffstat (limited to 'lib/sqlalchemy/engine/row.py')
-rw-r--r--lib/sqlalchemy/engine/row.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/sqlalchemy/engine/row.py b/lib/sqlalchemy/engine/row.py
index e2690ac2d..e15ea7b17 100644
--- a/lib/sqlalchemy/engine/row.py
+++ b/lib/sqlalchemy/engine/row.py
@@ -271,9 +271,11 @@ class ROMappingView(ABC):
__slots__ = ()
_items: Sequence[Any]
- _mapping: Mapping[str, Any]
+ _mapping: Mapping["_KeyType", Any]
- def __init__(self, mapping: Mapping[str, Any], items: Sequence[Any]):
+ def __init__(
+ self, mapping: Mapping["_KeyType", Any], items: Sequence[Any]
+ ):
self._mapping = mapping
self._items = items
@@ -297,16 +299,16 @@ class ROMappingView(ABC):
class ROMappingKeysValuesView(
- ROMappingView, typing.KeysView[str], typing.ValuesView[Any]
+ ROMappingView, typing.KeysView["_KeyType"], typing.ValuesView[Any]
):
__slots__ = ("_items",)
-class ROMappingItemsView(ROMappingView, typing.ItemsView[str, Any]):
+class ROMappingItemsView(ROMappingView, typing.ItemsView["_KeyType", Any]):
__slots__ = ("_items",)
-class RowMapping(BaseRow, typing.Mapping[str, Any]):
+class RowMapping(BaseRow, typing.Mapping["_KeyType", Any]):
"""A ``Mapping`` that maps column names and objects to :class:`.Row`
values.