Use Annotated
instead of default arg notation?
#44
SyntaxColoring
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
Both are actually supported: https://www.adriangb.com/di/0.32.5/wiring/#manual-wiring There is one advantage to the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! Chiming in here because I had some thoughts on the sketch that @adriangb posted over in the pytest issue tracker. Sorry if you've already considered this—I haven't had a chance to look through all the details of your project yet.
Here was that sketch:
My feedback is to reconsider the
log: Log = Depends(Log)
syntax shown here. It can be pretty dangerous.The problem is that the value returned by
Depends(...)
is not actually valid to use as an argument—it's only there as a placeholder for the dependency injection framework. So if the function is ever called without that dependency injection framework, like this:Then all hell breaks loose. The internal placeholder object, which is definitely not an
int
orLog
, will leak into the function and cause confusing errors down the line. And there's nothing that mypy can do to catch this.FastAPI has this problem, and it repeatedly bites the unwary. (If you want, I can dig up examples on their issue tracker.)
I would instead consider something like this:
Beta Was this translation helpful? Give feedback.
All reactions