Skip to content

Annotated Resolver #48

@ianjosephwilson

Description

@ianjosephwilson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions