Skip to content

Commit

Permalink
adjust the error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Feb 1, 2024
1 parent 5183517 commit 91718ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions edgedb/protocol/codecs/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,20 @@ cdef class BaseNamedRecordCodec(BaseRecordCodec):
for i in range(objlen):
if is_dict:
name = datatypes.record_desc_pointer_name(self.descriptor, i)
item = obj[name]
try:
item = obj[name]
except KeyError:
raise ValueError(
f"named tuple dict is missing '{name}' key",
) from None
elif is_namedtuple:
name = datatypes.record_desc_pointer_name(self.descriptor, i)
item = getattr(obj, name)
try:
item = getattr(obj, name)
except AttributeError:
raise ValueError(
f"named tuple is missing '{name}' attribute",
) from None
else:
item = obj[i]

Expand Down
14 changes: 14 additions & 0 deletions tests/test_namedtuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from collections import namedtuple, UserDict

import edgedb
from edgedb import _testbase as tb


Expand All @@ -36,3 +37,16 @@ async def test_namedtuple_01(self):
''', val)

self.assertEqual(res, (10, 'y'))

async def test_namedtuple_02(self):
NT1 = namedtuple('NT2', ['x', 'z'])

with self.assertRaisesRegex(edgedb.InvalidArgumentError, 'is missing'):
self.client.query_single('''
select <tuple<x: int64, y: str>>$0
''', dict(x=20, z='test'))

with self.assertRaisesRegex(edgedb.InvalidArgumentError, 'is missing'):
self.client.query_single('''
select <tuple<x: int64, y: str>>$0
''', NT1(x=20, z='test'))

0 comments on commit 91718ae

Please sign in to comment.