Skip to content

wrong named tuple error for attempt to assign on row #7432

Closed
@zzzeek

Description

@zzzeek
Member

assigning to Row like row.x = 5 raises AttributeError but the error claims "could not locate column", which is misleading. See #7424

Activity

added this to the 1.4.x milestone on Dec 9, 2021
zzzeek

zzzeek commented on Dec 9, 2021

@zzzeek
MemberAuthor
diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py
index e4f07a7580..3a7032f8b3 100644
--- a/test/sql/test_resultset.py
+++ b/test/sql/test_resultset.py
@@ -746,6 +746,47 @@ class CursorResultTest(fixtures.TablesTest):
         eq_(r._mapping[users.c.user_name], "john")
         eq_(r.user_name, "john")
 
+    def test_named_tuple_attribute_errors(self, connection):
+        r = connection.execute(
+            select(literal(1).label("a"), literal(2).label("b"))
+        ).first()
+        eq_(r.a, 1)
+        eq_(r.b, 2)
+
+        with expect_raises_message(
+            AttributeError, "Could not locate column in row for column 'c'"
+        ):
+            r.c
+
+        with expect_raises_message(AttributeError, "can't set attribute"):
+            r.a = 5
+
+        with expect_raises_message(AttributeError, "can't set attribute"):
+            r.a += 5
+
+    def test_mapping_tuple_readonly_errors(self, connection):
+        r = connection.execute(
+            select(literal(1).label("a"), literal(2).label("b"))
+        ).first()
+        r = r._mapping
+        eq_(r["a"], 1)
+        eq_(r["b"], 2)
+
+        with expect_raises_message(
+            KeyError, "Could not locate column in row for column 'c'"
+        ):
+            r["c"]
+
+        with expect_raises_message(
+            TypeError, "'RowMapping' object does not support item assignment"
+        ):
+            r["a"] = 5
+
+        with expect_raises_message(
+            TypeError, "'RowMapping' object does not support item assignment"
+        ):
+            r["a"] += 5
+
     def test_column_accessor_err(self, connection):
         r = connection.execute(select(1)).first()
         assert_raises_message(
CaselIT

CaselIT commented on Dec 9, 2021

@CaselIT
Member

I had already opened #7431

sqla-tester

sqla-tester commented on Dec 9, 2021

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

implement correct errors for Row immutability https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3360

sqla-tester

sqla-tester commented on Dec 9, 2021

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the rel_1_4 branch:

implement correct errors for Row immutability https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3361

added
engineengines, connections, transactions, isolation levels, execution options
on Dec 9, 2021
added a commit that references this issue on Dec 12, 2021
5ae1c14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingengineengines, connections, transactions, isolation levels, execution options

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@sqla-tester@CaselIT

        Issue actions

          wrong named tuple error for attempt to assign on row · Issue #7432 · sqlalchemy/sqlalchemy