FEAT: Row string-key indexing #589
Conversation
There was a problem hiding this comment.
Pull request overview
This PR restores/extends Row usability by allowing row["ColumnName"] access (in addition to positional and attribute access), addressing the regression reported in #582.
Changes:
- Updated
Row.__getitem__to accept string keys that map to column names. - Added unit and integration tests covering string-key access and expected error behavior.
- Added a test intending to cover case-insensitive string-key lookup when lowercase mode is enabled.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/row.py | Adds string-key handling to Row.__getitem__ (including an intended case-insensitive path). |
| tests/test_001_globals.py | Adds unit tests for string-key indexing and a case-insensitive variant. |
| tests/test_004_cursor.py | Adds an integration test verifying string-key indexing against real query results. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 59.7%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 76.1%
mssql_python.row.py: 76.9%
mssql_python.__init__.py: 77.3%
mssql_python.pybind.connection.connection.cpp: 77.5%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.connection.py: 85.4%
mssql_python.logging.py: 85.5%🔗 Quick Links
|
bewithgaurav
left a comment
There was a problem hiding this comment.
just added a test suggestion for e2e scenario - non-blocker
diff code coverage covers unit tests and is already at 100% so can skip
PR lgtm
| assert isinstance(row._values, list) | ||
|
|
||
|
|
||
| def test_row_string_key_indexing(): |
There was a problem hiding this comment.
nit: suggesting a test for the real cursor-built lowercase path end-to-end. the unit test covers Row with an injected column_map_lower, and this integration test covers string-key access with lowercase=False. what's missing is mixed-case DB columns + lowercase=True through an actual cursor. test_lowercase_attribute already has that exact table setup but only checks description column
rough shape for a test which can be e2e:
- create table with mixed-case columns (
ID INT, UserName VARCHAR(50)) - insert a row
- set
mssql_python.lowercase = True, open a fresh cursor, execute SELECT - verify string-key access:
row["username"]androw["USERNAME"]both return the value - verify attribute access:
row.usernameandrow.USERNAMEboth work - verify exact-case still works:
row["id"](lowercase, since that's what the cursor emits when lowercase is on) - verify missing key still raises
KeyError - restore
mssql_python.lowercaseinfinally
Work Item / Issue Reference
Summary
This pull request enhances the usability of the
Rowclass by allowing access to row values using column names as string keys (e.g.,row["col"]), in addition to existing integer index and attribute access. It also introduces case-insensitive string-key access when the cursor'slowercaseattribute is enabled. Comprehensive tests have been added to ensure correct behavior for these new access patterns.Enhancements to Row Access Patterns
Row.__getitem__method inmssql_python/row.pyto support accessing values by column name as a string key, including case-insensitive lookup when the cursor'slowercaseattribute is set.Testing Additions
test_row_string_key_indexingandtest_row_string_key_case_insensitive_with_lowercasetotests/test_001_globals.pyto verify string-key and case-insensitive access, as well as backward compatibility with integer and slice access.test_row_string_key_indexingtotests/test_004_cursor.pyto test string-key access on real query results, ensuring consistency with index and attribute access and correct error handling for missing keys.