Skip to content

Commit b2ee1df

Browse files
committed
improve rowmapping key type
the accepted keys are also orm attributes, column elements, functions etc, not only columns Change-Id: I354de9b9668bc02b8b305a3c1f065744b28f8030
1 parent 15b1e14 commit b2ee1df

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

lib/sqlalchemy/engine/result.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
from ..util.typing import Unpack
5252

5353
if typing.TYPE_CHECKING:
54-
from ..sql.schema import Column
54+
from ..sql.elements import SQLCoreOperations
5555
from ..sql.type_api import _ResultProcessorType
5656

57-
_KeyType = Union[str, "Column[Any]"]
58-
_KeyIndexType = Union[str, "Column[Any]", int]
57+
_KeyType = Union[str, "SQLCoreOperations[Any]"]
58+
_KeyIndexType = Union[_KeyType, int]
5959

6060
# is overridden in cursor using _CursorKeyMapRecType
6161
_KeyMapRecType = Any

lib/sqlalchemy/orm/mapper.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,7 +3442,7 @@ def _result_has_identity_key(self, result, adapter=None):
34423442

34433443
def identity_key_from_row(
34443444
self,
3445-
row: Optional[Union[Row[Unpack[TupleAny]], RowMapping]],
3445+
row: Union[Row[Unpack[TupleAny]], RowMapping],
34463446
identity_token: Optional[Any] = None,
34473447
adapter: Optional[ORMAdapter] = None,
34483448
) -> _IdentityKeyType[_O]:
@@ -3461,14 +3461,15 @@ def identity_key_from_row(
34613461
if adapter:
34623462
pk_cols = [adapter.columns[c] for c in pk_cols]
34633463

3464+
mapping: RowMapping
34643465
if hasattr(row, "_mapping"):
3465-
mapping = row._mapping # type: ignore
3466+
mapping = row._mapping
34663467
else:
3467-
mapping = cast("Mapping[Any, Any]", row)
3468+
mapping = row # type: ignore[assignment]
34683469

34693470
return (
34703471
self._identity_class,
3471-
tuple(mapping[column] for column in pk_cols), # type: ignore
3472+
tuple(mapping[column] for column in pk_cols),
34723473
identity_token,
34733474
)
34743475

test/typing/plain_files/sql/typed_results.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sqlalchemy import Column
99
from sqlalchemy import column
1010
from sqlalchemy import create_engine
11+
from sqlalchemy import func
1112
from sqlalchemy import insert
1213
from sqlalchemy import Integer
1314
from sqlalchemy import MetaData
@@ -117,9 +118,22 @@ def t_result_ctxmanager() -> None:
117118
reveal_type(r4)
118119

119120

120-
def t_core_mappings() -> None:
121+
def t_mappings() -> None:
121122
r = connection.execute(select(t_user)).mappings().one()
122-
r.get(t_user.c.id)
123+
r["name"] # string
124+
r.get(t_user.c.id) # column
125+
126+
r2 = connection.execute(select(User)).mappings().one()
127+
r2[User.id] # orm attribute
128+
r2[User.__table__.c.id] # form clause column
129+
130+
m2 = User.id * 2
131+
s2 = User.__table__.c.id + 2
132+
fn = func.abs(User.id)
133+
r3 = connection.execute(select(m2, s2, fn)).mappings().one()
134+
r3[m2] # col element
135+
r3[s2] # also col element
136+
r3[fn] # function
123137

124138

125139
def t_entity_varieties() -> None:

0 commit comments

Comments
 (0)