I wrote my own mutant version of the wired dataclass dependency resolver for a project but then I started using Annotated and it seems to fit much better. This is related to #35 but I was not sure if I should continue there so I just started a new issue. I was hoping to integrate my changes here but I wasn't sure what would be the best way forward or if this approach fits with wired.
Features
- accepts a function OR class with dependencies marked within
Annotated using Wired()
- allows
attr, key or call_kwargs/call_args to further extract values from a service
- resolver can be used standalone
- works with dataclasses, inheritance and plain classes (needs further testing though)
Limitations
- Python 3.9+ AFAIK
- Converting Annotated to a normalized form might be more complicated.
- Might not integrate well with static type checkers
Why
Annotated seems to work the same between function signatures and class properties which makes for a simpler API and implementation
- Avoids direct interaction with
dataclass fields which makes for a simpler API because there is no need to proxy the behavior of field()
- The resolver can be used directly which could support
pyramid view mappers.
AnnotatedDependencyExtractor in https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L32 extracts the annotations into specifications of what to resolve
DependencyResolver in https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L80
Injector actually injects the dependencies -- https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L102
wired_service is the decorator that can be loaded with venusian to use the extractor, resolver and injector to construct the service.
I wrote my own mutant version of the wired dataclass dependency resolver for a project but then I started using Annotated and it seems to fit much better. This is related to #35 but I was not sure if I should continue there so I just started a new issue. I was hoping to integrate my changes here but I wasn't sure what would be the best way forward or if this approach fits with wired.
Features
AnnotatedusingWired()attr,keyorcall_kwargs/call_argsto further extract values from a serviceLimitations
Why
Annotatedseems to work the same between function signatures and class properties which makes for a simpler API and implementationdataclassfields which makes for a simpler API because there is no need to proxy the behavior offield()pyramidview mappers.AnnotatedDependencyExtractorin https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L32 extracts the annotations into specifications of what to resolveDependencyResolverin https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L80Injectoractually injects the dependencies -- https://github.com/ianjosephwilson/wired_services/blob/main/src/wired_services/servicetools.py#L102wired_serviceis the decorator that can be loaded with venusian to use the extractor, resolver and injector to construct the service.