From c1125288e3c413d868070995b308085d2ddf402e Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 20 Apr 2023 13:41:39 -0400 Subject: 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 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: #` 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: #` 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 --- test/sql/test_resultset.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/sql') diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index 0537dc228..e382a7fb6 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -1595,6 +1595,15 @@ class CursorResultTest(fixtures.TablesTest): r = connection.exec_driver_sql("select user_name from users").first() eq_(len(r), 1) + def test_row_mapping_get(self, connection): + users = self.tables.users + + connection.execute(users.insert(), dict(user_id=1, user_name="foo")) + result = connection.execute(users.select()) + row = result.first() + eq_(row._mapping.get("user_id"), 1) + eq_(row._mapping.get(users.c.user_id), 1) + def test_sorting_in_python(self, connection): users = self.tables.users -- cgit v1.2.1