Conversation
7d8361b to
0715bf8
Compare
| *args, | ||
| prefetch=None, | ||
| timeout=None, | ||
| record_class=None |
There was a problem hiding this comment.
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.
Is there any evidence that users want multiple different record types on one connection?
There was a problem hiding this comment.
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_classparameter 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.