mmerickel / wired

A service locator implementation for Python.
https://wired.readthedocs.io
MIT License
17 stars 9 forks source link

Dataclasses and "declarative DI": Leave as docs, consider it an extra, or ??? #7

Closed pauleveritt closed 5 years ago

pauleveritt commented 5 years ago

I wrote a tutorial in which I constructed a DI system based on dataclasses and sniffing at type annotations to inject things. I think it's valuable in two respects:

For the latter, I could of course just leave it as a sample app. We could, though, consider it software (once it is done of course.) Choices:

Obviously this is all still experimentation, on a branch or fork, etc.

mmerickel commented 5 years ago

I'm very interested in wired.dataclasses or wired.ext.dataclasses or something with support for automatically creating factories for dataclass objects. I don't see a need at this point to put it in a separate package from wired anyway.

pauleveritt commented 5 years ago

I'll let you decide whether you want "ext" in there. I think it's worth promoting that wired is tiny and there are optional things, so maybe ext has some merit.

For now, not a rush. I'll stick to doing this as a second tutorial.

mmerickel commented 5 years ago

Let's go with wired.dataclasses for now if that makes sense to you. I'm thinking of it as patterns / functions / helpers for doing stuff with dataclasses to hook into wired. If that's not how you see it let me know.

pauleveritt commented 5 years ago

I'm fine with that. But FTR I view it as DI first and dataclasses second. wired advertises itself as a foundation for building a DI system. The thing I'm thinking about is one such DI system.

In fact, parts of it might not be dataclasses. There might be standalone functions, or class methods, that have their arguments sniffed and injected-on-call.

mmerickel commented 5 years ago

From what I've seen to this point, the dataclass bindings you've written are pretty well dependent on dataclass apis for introspecting the objects. Similarly attrs has its own way of pulling out metadata from properties. It seems at least at that level it needs to be per-library to find a way to auto-generate factories.

If you are talking about higher level stuff combining objects from different libraries my thought is that's done just via lookup of factories by type (container.get(Foo)) where no one really cares whether Foo is a dataclass or attrs object or anything else.

pauleveritt commented 5 years ago

Agreed, well said. Dataclasses it is.

pauleveritt commented 5 years ago

Let's close this ticket. I believe you've agreed to make it a part of the package.