adriangb / di

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

feat: allow classes to be constructed via an @classmethod __call__ #64

Closed adriangb closed 2 years ago

codecov-commenter commented 2 years ago

Codecov Report

Merging #64 (d1bd56f) into main (a89b6b7) will increase coverage by 0.01%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #64      +/-   ##
==========================================
+ Coverage   97.52%   97.54%   +0.01%     
==========================================
  Files          47       49       +2     
  Lines        2021     2037      +16     
  Branches      341      342       +1     
==========================================
+ Hits         1971     1987      +16     
  Misses         39       39              
  Partials       11       11              
Impacted Files Coverage Δ
di/dependant.py 97.59% <100.00%> (+0.18%) :arrow_up:
tests/docs/test_async_constructor.py 100.00% <100.00%> (ø)
tests/docs/test_async_init_dependency.py 100.00% <100.00%> (ø)
adriangb commented 2 years ago

There's no particular reason for this have to be __call__. But at the same time this has to be done by convention, not configuration, because otherwise you'd need some way to inject that configuration at which point you might as well use a Dependant marker.

So maybe we should call this __di_new__, __di_construct__ or something like that? It would also save us a bit of introspection because we can just say "if the object is a class and has this attribute, use that".