Comments (3)
The noninjectable
decorator[1] will tell Injector to not try to provide the value for a parameter and it'll let you do what you need:
>>> import injector
>>>
>>> class MyClass:
2 @injector.inject
3 @injector.noninjectable('title')
4 def __init__(self, title: str = 'Default Title') -> None:
5 self.title = title
>>>
>>> injector_ = injector.Injector()
>>> injector_.get(MyClass).title
'Default Title'
>>>
>>> builder = injector_.get(injector.AssistedBuilder[MyClass])
>>> builder.build(title='Doctor Who').title
'Doctor Who'
Edit: The presence of the default parameter values doesn't change the Injector's behaviour right now - maybe it should.
[1] https://injector.readthedocs.io/en/latest/api.html#injector.noninjectable
from injector.
Oh, great, thanks! I guess I should have found that myself.
The fact that injector injects "value types" (such as str
, int
, bool
) is a bit counterintuitive to me, especially when there's a default value. But the noninjectable
decorator at least gives the option to handle the case.
from injector.
That being said, my issues are resolved. Feel free to close this issue if you don't want to change the way default arguments are handled. Thanks for your help!
from injector.
Related Issues (20)
- Support pep 604 style for optional types (`Type | None`)
- Bind a provider to multiple interfaces?
- Binding multiple interfaces to same implementation HOT 2
- Singletons for parent/child injectors are order-dependent HOT 9
- Maintainer status? HOT 2
- Potential resource leak HOT 3
- Binding of Annotated types does not work as one would expect HOT 8
- Is it an antipattern to have multiple instances of Injector (because of missing Generics support)? HOT 2
- Testing example in docs is not working. ImportError: cannot import name 'with_injector' from 'injector' HOT 6
- FastAPI support HOT 2
- Injection of two abstract classes and annotation
- Question about the example code and intended use HOT 2
- Differentiating between multiple providers HOT 4
- Providing different instances of the same type in the same module HOT 1
- Issue with ClassAssistedBuilder HOT 4
- Implementation of SessionScope Needed
- [Docs] No declarations of type `Name` and `Description` on Docs injection sample HOT 1
- use specific Injector instance as `inject` decorator HOT 5
- How to handle resources? HOT 4
- `injector.Injector.create_object` on pydantic-settings `BaseSettings` does not without factory function in Injector HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from injector.