reagento / dishka

Cute DI framework with agreeable API and everything you need
https://dishka.readthedocs.io
Apache License 2.0
428 stars 48 forks source link

deprecating aliases/providers passing #311

Open RonnyPfannschmidt opened 4 days ago

RonnyPfannschmidt commented 4 days ago

when changing a codebase with numerous providers its sometimes desired to migrate from one type to another one across time

in such cases it would be very helpful to provide a mechanism that warns on usages of deprecated providers/decorators

from dishka import Provider, make_container, provide, Scope

class Old:
   ...

class New: 
   ...

@dataclass
class Extra:
    old: Old

USE_NEW = DeprecationWarning("Old is depreated please use New")

class ExampleProvider(Provider):
    scope = Scope.APP

    old = provide(Old, warn_on_use=USE_NEW)
    new = provide(New)

    extra = provide(Extra)

    @provide
    def extras(old: Old) -> list[Extra]:
        return [Extra(old)]

cont = make_container(ExampleProvider())

cont.get(Old) # warn with the get call as location

cont.get(Extra) # warn with the Extra type location

cont.get(list[Extra]) #warn with the provider definition as ocation
Tishka17 commented 4 days ago

Can you, please, provide an example, what do you want to see and where: console, IDE or what? From what I understood by now you can add warn() call inside factory function, but probably it won't give you enough metadata

RonnyPfannschmidt commented 4 days ago

yes, basically i wand dishka to call warn_explicit with either the call site container.get or the providing point (definition of the provider method

i'll get some examples on what pytest/pluggy does for hooks/nodes + how that looks