@@ -91,13 +91,19 @@ class PostgresEngine:
9191 """A class for managing connections to a Cloud SQL for Postgres database."""
9292
9393 _connector : Optional [Connector ] = None
94+ __create_key = object ()
9495
9596 def __init__ (
9697 self ,
98+ key : object ,
9799 engine : AsyncEngine ,
98100 loop : Optional [asyncio .AbstractEventLoop ],
99101 thread : Optional [Thread ],
100102 ):
103+ if key != PostgresEngine .__create_key :
104+ raise Exception (
105+ "Only create class through 'create' or 'create_sync' methods!"
106+ )
101107 self ._engine = engine
102108 self ._loop = loop
103109 self ._thread = thread
@@ -191,7 +197,7 @@ async def getconn() -> asyncpg.Connection:
191197 "postgresql+asyncpg://" ,
192198 async_creator = getconn ,
193199 )
194- return cls (engine , loop , thread )
200+ return cls (cls . __create_key , engine , loop , thread )
195201
196202 @classmethod
197203 async def afrom_instance (
@@ -218,7 +224,7 @@ async def afrom_instance(
218224
219225 @classmethod
220226 def from_engine (cls , engine : AsyncEngine ) -> PostgresEngine :
221- return cls (engine , None , None )
227+ return cls (cls . __create_key , engine , None , None )
222228
223229 async def _aexecute (self , query : str , params : Optional [dict ] = None ):
224230 """Execute a SQL query."""
0 commit comments