adriangb / di

Pythonic dependency injection
https://www.adriangb.com/di/
MIT License
303 stars 13 forks source link

feat: Add injectable base class #68

Closed adriangb closed 2 years ago

adriangb commented 2 years ago

This makes it easier to make use of the API introduced in #65 by allowing users to simply subclass a base class and pass in parameters to __init_subclass__.

The alternative is something like:

class Foo:
    ...

InjectFoo = Annotated[Foo, Depends(scope=...)]`

This is less verbose, has less imports, propagates to subclasses and allows a single object (the type itself) to be used to construct the object directly as well as for dependency injection

codecov-commenter commented 2 years ago

Codecov Report

Merging #68 (a8c12e6) into main (f00205b) will increase coverage by 0.02%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #68      +/-   ##
==========================================
+ Coverage   97.70%   97.73%   +0.02%     
==========================================
  Files          50       51       +1     
  Lines        2007     2033      +26     
  Branches      329      333       +4     
==========================================
+ Hits         1961     1987      +26     
  Misses         37       37              
  Partials        9        9              
Impacted Files Coverage Δ
di/dependant.py 100.00% <100.00%> (ø)
tests/test_injectable_class.py 100.00% <100.00%> (ø)