-
Notifications
You must be signed in to change notification settings - Fork 402
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
Allow using custom Record class #599
Conversation
7d8361b
to
0715bf8
Compare
*args, | ||
prefetch=None, | ||
timeout=None, | ||
record_class=None |
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.
Instead of adding record_class
everywhere I'd allow passing it to the constructor. Or add a set_record_class_factory
method.
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.
Is there any evidence that users want multiple different record types on one connection?
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.
The entire impetus of this push was from #577, where the record_class
is used as a TypeDict
-like typehint for the result, which gets typechecked also.
I'd allow passing it to the constructor.
It is also allowed to be passed in the constructor if you want a connection-wide subclass.
@elprans is this PR ok to be merged? I'm really looking forward for this and the typings PR 😄 |
Add the new `record_class` parameter to the `create_pool()` and `connect()` functions, as well as to the `cursor()`, `prepare()`, `fetch()` and `fetchrow()` connection methods. This not only allows adding custom functionality to the returned objects, but also assists with typing (see #577 for discussion). Fixes: #40.
@elprans Can you provide an example how to implement custom record_class with dot-notation? |
Not sure if this is correct but should work class MyRecord(Record):
def __getattr__(self, item):
return self.get(item) |
#960 adds an example of this to the FAQ item. |
I tried implementing that example, but I kept getting errors from the GraphQL execution engine because it passed https://docs.python.org/3/reference/datamodel.html#object.__getattr__ So I updated the code to this, and it started working: class AttrRecord( asyncpg.Record ):
def __getattr__( self, name ):
if name not in self:
raise AttributeError
return self[ name ] |
Add the new
record_class
parameter to thecreate_pool()
andconnect()
functions, as well as to thecursor()
,prepare()
,fetch()
andfetchrow()
connection methods.This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).
Fixes: #40.