Replies: 2 comments 3 replies
-
Hey hello, Thank you for playing with this, I figured out the redesigned connection class would have been useful for psycopg2. ...but no, I prefer to not add "scratch attributes". The solution of subclassing works adequately well. Mypy not liking it is not really a big issue, there are so many things that mypy doesn't like (yet) and the right way to work around them is with a You can also drop the dynamic |
Beta Was this translation helpful? Give feedback.
-
I've uploaded the backport to a repo here: https://github.com/bluetech/psycopg-pool-backport @dvarrazzo Would you be OK with me publishing it to PyPI under the name |
Beta Was this translation helpful? Give feedback.
-
I'd like to add a
scratch
attribute toconnection
which is by defaultNone
with typeAny
but which may be set by 3rd parties for private use.This must sound like an XY problem so let me explain why I'm asking for this.
I'm using Postgres with Django and have a need for a proper (process-level) connection pool rather than Django's persistent connections. I know that psycopg3 has a nice new connection pool, better than the psycopg2's ones. However, Django doesn't support psycopg3 yet, so I decided to port psycopg3's
ConnectionPool
to psycopg2 (minus the asyncio stuff). The port is very straightforward, the only real problem I ran into is thatConnectionPool
sets a couple of attributes on the connection instance (_pool
and_expire_at
), but psycopg2'sconnection
class is slotted and doesn't allow extra attributes.The workaround I'm using now is to use a
connection
subclass which has a__dict__
so allows extra attributes, but this wrecks the types somehow sinceconnection_class
is given dynamically and mypy doesn't much like subclassing this. Another possible workaround is to use a weakref dict. But I thought I'd ask if psycopg2 could add some "scratch space" on the connection so I can use that directly instead.BTW, from playing with the connection pool so far, it works well!
Beta Was this translation helpful? Give feedback.
All reactions