Package: injectable
Injectable: Dependency Injection for Humans™Usage Examples 🚩|Developer Reference 👩‍💻|Authors 👫Injectableis an elegant and simple Dependency Injection framework built with Heart
and designed for Humans.frominjectableimportAutowired,autowiredfromtypingimportListfrommodelsimportDatabasefrommessagingimportBrokerclassService:@autowireddef__init__(self,database:Autowired(Database),message_brokers:Autowired(List[Broker]),):pending=database.get_pending_messages()forbrokerinmessage_brokers:broker.send_pending(pending)fromabcimportABCclassBroker(ABC):defsend_pending(messages):...frominjectableimportinjectable@injectableclassDatabase:...frommessagingimportBrokerfrominjectableimportinjectable@injectableclassKafkaProducer(Broker):...frommessagingimportBrokerfrominjectableimportinjectable@injectableclassSQSProducer(Broker):...Features you’ll love ❤️Autowiring: injection is transparent to the function. Just decorate the function
with@autowiredand annotate parameters withAutowired, that’s it.Automatic dependency discovery: just callload_injection_container()at
the root of your project or pass the root path as an argument. All classes decorated
with@injectablewill be automatically discovered and ready for injection.Qualifier overloading: declare as many injectables as you like for a single
qualifier or extending the same base class. You can inject all of them just by
specifying atyping.ListtoAutowired:deps:Autowired(List["qualifier"]).Transparent lazy initialization: passing the argumentlazy=TrueforAutowiredwill make your dependency to be initialized only when actually used, all
in a transparent fashion.Singletons: decorate your class with@injectable(singleton=True)and only a
single instance will be initialized and shared for injection.Namespaces: specify different namespaces for injectables as in@injectable(namespace="foo")and then just use them when annotating your
parameters as indep:Autowired(...,namespace="foo").Linters friendly:Autowiredis carefully designed to comply with static linter
analysis such as PyCharm’s to preserve the parameter original type hint.These are just a few cool and carefully built features for you. Check out ourdocs!Changelog3.4.7 (2021-08-15)Fix injectable crashing when relative imports are used in files containing injectables.3.4.6 (2021-03-20)Fixtesting.register_injectablesnot creating the namespace when it doesn’t exist
yet3.4.5 (2021-03-11)Fix opening of UTF-8 files & allow for user set encoding3.4.4 (2020-07-29)Fixinjectreturn type hint3.4.3 (2020-06-24)Fix Injectable failing to resolve complex/entangled imports3.4.2 (2020-05-22)Fix optional injection bug when the namespace is empty3.4.1 (2020-05-11)Fix the use of named args by the caller breaking autowired functions injection3.4.0 (2020-05-09)DeprecateInjectionContainer::loadin favor ofload_injection_container.Change default namespace name from"_GLOBAL"to"DEFAULT_NAMESPACE".Fix minor quirks with Python 3.7 and 3.8.Add tons of unit tests.Addreset_injection_containerutility toinjectable.testing.3.3.0 (2020-04-20)Include theinjectable.testingutilities to ease mocking injectables.3.2.1 (2020-04-19)InjectionContainer::loadis more resilient against duplicated injectables
registering3.2.0 (2020-04-15)Support for optional injection in declarative fashion:Autowired(Optional[...])3.1.4 (2020-04-15)FixAutowired(List[...])not working with qualifiers3.1.3 (2020-04-15)Fix Windows injectables not being loaded.3.1.2 (2020-04-14)Remove unusedinspectimports.3.1.1 (2020-04-13)Fix bug of scanning the same module more than once whenInjectionContainer.load()is called multiple times with different relative search paths.3.1.0 (2020-04-13)Added@injectable_factorydecorator for declaring injectable factory methodsInclude the console output in the examples3.0.1 (2020-04-13)Fix package content missing3.0.0 (2020-04-12)Drop support for autowiring without previous initialization of the InjectionContainerRefactor@autowireddecorator for working with theAutowiredtype annotationAdded@injectabledecorator for registering injectables to the InjectionContainerSupport for qualifiers, groups and namespacesAddedAutowiredtype annotation for marking parameters for autowiringAddedinjectandinject_multipleas service locatorsAdded InjectionContainer for registering injectablesOfficial support for Python 3.7 and 3.8Official support for Ubuntu, Windows and MacOSDrop Python 3.4 and 3.5 official supportGeneral code refactoringOfficial documentationAdded usage examples2.0.0 (2018-02-24)Drop Python 3.3 official support1.1.2 (2018-02-24)Support for dependencies of classes without signatureFix bug of builtin types not being accepted for injectable dependencies1.1.1 (2018-02-23)Statically infer dependency’s constructor suitability for injection instead of using
trial instantiationFix bug of raisingTypeErrorwhen injectable fails on the trial dependency
instantiation which can happen when the dependency does provide a default
constructor with no arguments but the running environment (possibly a test suite
environment) will make the instantiation fail1.1.0 (2018-02-10)Enable the use of@autowireddecorator without parenthesis1.0.1 (2018-02-10)Fixes required dependencylazy_object_proxynot being installed when installing
injectable through pip1.0.0 (2018-02-06)First stable release0.2.0 (2018-02-06)Support for lazy dependency initializationSupport for type annotations with strings0.1.1 (2018-02-05)Python 3.3 and 3.4 support0.1.0 (2018-02-05)First beta release