Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated Resolver #48

Open
ianjosephwilson opened this issue Feb 2, 2025 · 1 comment
Open

Annotated Resolver #48

ianjosephwilson opened this issue Feb 2, 2025 · 1 comment

Comments

@ianjosephwilson
Copy link

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.

@ianjosephwilson
Copy link
Author

ianjosephwilson commented Feb 2, 2025

Prototype output looks like this:

$  hatch run python -- -m wired_services.testit
Hello there, the date and time is 2025-02-01T18:36:30.806211-08:00.
Hello again there, the date and time is 2025-02-01T18:36:30.806311-08:00.
Greetings to you! Hello Ian, the date and time is 2025-02-01T18:36:30.806448-08:00. Your lucky number is 175. And good day to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant