Introduce TortoiseContext, remove global state#2069
Conversation
| @@ -13,8 +13,10 @@ | |||
|
|
|||
| from ptpython.repl import embed | |||
There was a problem hiding this comment.
Django shell support ipython(which is much more popular than ptpython) as optional deps and will use the built-in Python REPL if ipython not installed.
Is it possible for us to support both REPL/ipython/ptpython?
There was a problem hiding this comment.
Problem with default REPL is that it doesn't natively support "await ..."
ipython doesn't have such problem
Only advantage ptpython has that it is smaller dependency than ipython
Probably we could remove ptpython from dependencies and optionally allow ipython and ptpython, depending on what's available
Not sure if it worth it to implement fallback to default REPL
There was a problem hiding this comment.
- No need to fallback REPL
- Optional ipython/ptpython is OK
There was a problem hiding this comment.
I'll update it in separate PR
Since start of Tortoise - it's state, with connections and apps, was stored globally, as class vars on "Tortoise".
This brought number of problems, including #1110 , because in tests we were forced to use hacks with global variables to initiate tests, while also limiting users in how they can initialise tortoise
This PR introduces new TortoiseContext, which encapsulates state of tortoise, allowing you to have several of those simultaneously
Also removed old test infrastructure, replacing it with pytest style one. I hope it should ease adding of new tests as now they are less polluting each other
This PR also introduces a lot of breaking changes, so it will be released as part of 1.0
AI Summary:
This pull request introduces major changes as part of the context-first architecture migration for Tortoise ORM, including breaking changes to test infrastructure, connection handling, and documentation. The update replaces legacy test helpers and fixtures with modern, context-aware pytest fixtures, revises connection access patterns, and updates documentation to reflect the new API and migration guidance.
Breaking changes and migration to context-first architecture:
TortoiseContextinstances, and legacy test classes/helpers (test.TestCase,initializer(), etc.) have been removed.Tortoise.init()now returns aTortoiseContextinstead ofNone. Multipleasyncio.run()calls in sequence now require explicit context management.connectionssingleton is deprecated; new functionsget_connection()andget_connections()provide connection access from the current context. [1] [2]Testing infrastructure overhaul:
db,db_simple,db_isolated,db_truncate) usingtortoise_test_contextfor robust, context-based test isolation. A fixture factory (make_db_fixture) is provided for custom test modules.Documentation and migration guidance:
connections.rstdocumentation is rewritten to describe new connection access patterns, context usage, and migration from the deprecated singleton. It now includes practical examples, API reference, and a migration guide table.Minor improvements:
These changes modernize Tortoise ORM's architecture, improve test reliability and speed, and provide clear migration paths for users upgrading from earlier versions.