I’ll make a more developped proposal etc but based on @fcurella’s branch I was able to get codegen working! Add unasync codegen helpers · rtpg/django@c4ac6ec · GitHub this is the POC commit, the short version is that:
@generate_unasynced_codegen
async def ainit_connection_state(self):
"""Initialize the database connection settings."""
global RAN_DB_VERSION_CHECK
if self.alias not in RAN_DB_VERSION_CHECK:
await self.acheck_database_version_supported()
RAN_DB_VERSION_CHECK.add(self.alias)
in a class definition will, after running the codegen script, insert
@from_codegen
@async_unsafe
def init_connection_state(self):
"""Initialize the database connection settings."""
global RAN_DB_VERSION_CHECK
if self.alias not in RAN_DB_VERSION_CHECK:
self.check_database_version_supported()
RAN_DB_VERSION_CHECK.add(self.alias)
just above it. Comments are preserved, the sync version “looks canonical”, the codegen script is idempotent, and at least so far I have been able to make it so that async → sync leads to no actual diff within the sync body (i.e. no behavioral changes).
Unreasonably excited about how easy this was (like 180 line transformation script, including having if ASYNC_TRUTH_MARKER: A else: B collapse to B for the unasync version, hacking in aconnection -> connection renames…), I imagine there’ll be dragons but given the psycopg proof of this strategy somewhat working… very pleased.