python-injector / python-injector/injector
Feature request: Strict mode which enforces `Module` definitions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 94
- PR merge metrics
- No merged PRs in 30d
Description
I played around with injector and Flask-Injector the last few days, mostly because I came to realize that bigger Flask applications can easily become a nightmare when writing tests.
I have to say I really enjoy using the library so far. However, there is one design decision I am a bit afraid of... since you are way more into the topic of DI than me, maybe you can easily convince me that this is a non-issue, or maybe there is some easy way to work around it.
It's already in the first example of the the README:
>>> from injector import Injector, inject
>>> class Inner:
... def __init__(self):
... self.forty_two = 42
...
>>> class Outer:
... @inject
... def __init__(self, inner: Inner):
... self.inner = inner
...
>>> injector = Injector()
>>> outer = injector.get(Outer)
An instance of Outer gets created even though there is no Module with any further description of what instance (e.g., inheritance) to set up or which scope to use. Maybe you can proof me wrong, but I consider this kind of a dangerous design decision. Imagine someone writing integration tests that wipe the database after every round but forgetting to define the TestDbConnection dependency in the Module, so what the integration test really uses is some production database... ouch!
Even though this example sounds a bit constructed, what I would like to see is some kind of strict mode that tells injector not to create instances out of thin air IF I already use a Module and this module lacks a definition of the according dependency (so the example would still work if someone prefers not to use modules at all).
I have some experiences with the Spring Framework in the kotlin/java world, I also had a look into the documentations of guice, dependencies and punq. I think they all enforce this kind of setup.
Contributor guide
No contributing guide indexed for this repository
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 with the README's Injector and Module examples, then trace the existing behavior for dependencies that are created without explicit Module definitions. Clarify the strict-mode boundary, including how it behaves when no Module is used, and define tests for missing dependency definitions before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100