-
Notifications
You must be signed in to change notification settings - Fork 403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support pickling Record-s #1000
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,11 +287,6 @@ def test_record_get(self): | |
self.assertEqual(r.get('nonexistent'), None) | ||
self.assertEqual(r.get('nonexistent', 'default'), 'default') | ||
|
||
def test_record_not_pickleable(self): | ||
r = Record(R_A, (42,)) | ||
with self.assertRaises(Exception): | ||
pickle.dumps(r) | ||
|
||
def test_record_empty(self): | ||
r = Record(None, ()) | ||
self.assertEqual(r, ()) | ||
|
@@ -575,3 +570,11 @@ class MyRecordBad: | |
'record_class is expected to be a subclass of asyncpg.Record', | ||
): | ||
await self.connect(record_class=MyRecordBad) | ||
|
||
def test_record_pickle(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i suggest to write a test of pickling nested records There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Postgres doesn't support returning nested records. asyncpg doesn't support nested records anywhere. I don't think it's a good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested composite types are returned as nested import asyncio
import asyncpg
async def main():
conn = await asyncpg.connect()
await conn.execute('CREATE TYPE complex AS (r float, imag float)')
print(await conn.fetchrow("SELECT 1, '2', (3, 4)::complex"))
asyncio.run(main()) |
||
r = pickle.loads(pickle.dumps(Record(R_AB, (42, 43)))) | ||
self.assertEqual(len(r), 2) | ||
self.assertEqual(r[0], 42) | ||
self.assertEqual(r[1], 43) | ||
self.assertEqual(r['a'], 42) | ||
self.assertEqual(r['b'], 43) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would lose the original record subtype if one was used, so you need to pickle the class reference also.