Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vmarkovtsev
Copy link

Closes #451

@kekekekule
Copy link

@elprans Hi! Please could you kindly take a look at this PR? We've faced the same problem with difficulties of Records' pickling :(

@@ -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):
Copy link

@frake23 frake23 Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suggest to write a test of pickling nested records

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested composite types are returned as nested Record instances, e.g:

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())

if (PyObject_Length(desc->mapping) != len) {
return NULL;
}
PyObject *record = ApgRecord_New(&ApgRecord_Type, (PyObject *)desc, len);
Copy link
Member

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.

@vmarkovtsev
Copy link
Author

vmarkovtsev commented Aug 18, 2023

OK, we seem to have different use cases and motivations with the maintainer here. Please feel free to close this PR since I am not planning any further work, unfortunately. If one doesn't leverage custom record classes and doesn't work with nested stuff, my fork is published and maintained as asyncpg-rkt on PyPi, together with many other improvements.

@elprans
Copy link
Member

elprans commented Oct 9, 2023

OK, we seem to have different use cases and motivations with the maintainer here

I'm not sure how you reached this conclusion. I simply pointed out the inaccuracy of your statement regarding nested records which would need to be addressed for this to get merged. Other than that this is a valid and valuable improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cant pickle asyncpg.record
4 participants