tortoise / tortoise/tortoise-orm
PyPy incompatibility (and suggested resolution)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
For PostgreSQL access, Tortoise is not yet capable of running under PyPy.
This is because:
- for PostgreSQL async, Tortoise is hardwired to use only
asyncpgclient - asyncpg is unavailable under PyPy Pip, due to its explicit CPython dependency
- There are no PyPy-compatible Pip packages that can implement the needed asyncpg classes
There is a solution:
- while neither
psycopg2norpsycopg2-binaryare installable in PyPy, there is a fully plug-compatible librarypsycopg2cffi, which does work fine in pypy, except it doesn't import as psycopg2 - psycopg2cffi, once imported, can be patched into
sys.modulesto act aspsycopg2 - once psycopg2cffi is in sys.modules, masquerading as psycopg2, then
aiopgcan be safely imported - a new alternate backend in Tortoise, using aiopg, will work
It's fortunate that Tortoise pip installation doesn't include asyncpg as a dependency. This means that installing Tortoise via PyPy pip will not fail out due to a broken dependency package.
(I've already got an aiopg backend partially working to proof of concept stage, but I think this task is more productively undertaken by folks who are already up to speed in the deeper Tortoise codebase.)
So, in PyPy, the sequence for preparing a compatible async pg stack would be:
1. Package installation
Assume $PYPY is the PyPy executable, and $PYPY_PIP is set to PyPy's pip executable
$PYPY -m ensurepip
$PYPY -m pip install --upgrade pip
$PYPY_PIP install aiopg
# the aiopg install succeeds, but aiopg can't be readily imported
# note that a broken version of psycopg2 will be present in site-packages
# workaround is suggested further down
$PYPY_PIP install psycopg2cffi
$PYPY_PIP install tortoise-orm
2. PyPy Program Startup
import sys
import psycopg2cffi as psycopg2
# fool other package into thinking psycopg2 or psycopg2-binary is already imported
# note that psycopg2cffi appears fully 'plug-compatible' with both psycopg2 and psycopg2-binary
sys.modules['psycopg2'] = psycopg2
import aiopg # this succeeds now
import tortoise
# then, within a running event loop:
config = { ... } # prep an appropriate config dict using the aiopg-compatible backend
await Tortoise.init(config=config)
# then, Tortoise is now ready for action and is PyPy-compabible for using PostgreSQL
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the PostgreSQL backend and the configuration path used by Tortoise.init, then compare how the existing asyncpg integration is loaded. The issue's proposed direction is an alternate aiopg backend with psycopg2cffi support for PyPy; done means PostgreSQL access works under PyPy through that backend.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100