Set `RedisModel.Meta.database` at runtime, not import time
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.3k
- Forks
- 128
- PR merge metrics
- No merged PRs in 30d
Description
Often the software I write does not know the configuration at import time. It has to load it from multiple sources, as it is executed in different environments and with different (CLI) options.
Generally, It's best practice to not open database or network connections (or do any I/O) at import time.
Having to set the database connection at import time also makes testing harder. Things have to be patched before importing any dependency that might import the module with the RedisModel.
It would be more convenient if the database connection could be set at runtime... e.g. MyModel.Meta.database = ... when my software has finished collecting the configuration and other setup steps.
This does currently not work, because the creation of the class through the metaclass stores the value somewhere I cannot change it later.
Alternatively (and maybe less work to implement with the current code - not that I have looked at it) would be to allow the value of RedisModel.Meta.database to be a callable (type Callable[[], Redis]). That could be provided at import time without having to know the database connection yet, and would not even need patching in tests (as the test can just create a testing configuration) - it's dependency injection.
The built-in callable() can be used to test, if the value in MyModel.Meta.database is a callable. Luckily a Redis connection pool object is not a callable.
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 tracing how the RedisModel metaclass reads and stores RedisModel.Meta.database during class creation. Compare the runtime-assignment and callable alternatives described in the issue, then identify the existing tests covering model database configuration; done means configuration can be supplied after import without breaking current Redis connection behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, redis
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100