-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the handling of stringified annotations in _takes_container
#55
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK 3.8 & 3.9 are failing again – but you're not even using get_type_hints? :|
I should have read the documentation in more detail :P Maybe instead of |
FWIW, I'm OK if you write two versions hidden behind a huge
Yes, but as you'll notice, it's a bit more complicated that it looks like. 🤓 Because I've tried cobbling something together and your PR was so fast I gave up on it. |
Yeah, this is quite frustrating actually. If the factory functions are annotated, then we can use the |
Compromise: dumb string compare on Legacy Pythons, your new code on Good Pythons – what do you say? |
I have a doubt. The factories are only supposed to take zero or one argument and if it takes in an argument, it has to be the container correct? If this is the case, can't we just return |
hmmm the reason I'm strict with this is that I want to keep the option of adding more arguments down the road. It correct though that it is a bit stupid to error out on non-0/1 and just ignore the one if it has the wrong name/type. Technically it should error out, not return False. 🤔 |
Like one example of what I maybe want to do in the future is allowing passing arguments to factories ( |
This is really nice. Okay then here's another idea. What if you place the following constraints:
|
Hmm, why would we enforce that tho? Like: what problem are we solving right now? I find it nicer to say to users of at least 3 years old Pythons that their accuracy of container detection is a bit lower (for comparison: both attrs and data classes check for ClassVar using string compare to avoid importing typing) than enforcing new rules on everybody. Or am I missing something? |
The current way of comparing the strings wouldn't work if a user aliases |
Agreed, but I'm fine to say "no aliasing pre-3.10" and let everyone use |
Yes, but unfortunately aliasing will not work if it's under a from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from svcs import Container as ServiceContainer
def get_service(container: ServiceContainer) -> Service:
... The reason I feel it would be nice to support this scenario is because it's a common pattern now and linters like Of course, you could just document all these edge cases/caveats and tell users to not alias within a |
Yeah TYPE_CHECKING comes with tons of caveats which is why I've stopped using it unless I have to for breaking import cycles. Eg autodoc also completely falls apart. Since we're generally agreeing, what would you suggest concretely? |
My suggestion is the following:
EDIT: I just realized that the aliasing, type hint related issues can all be avoided if the user uses |
I agree with points 1 and 2 and I think that's the only ones relevant to this PR. If you want to do only the first point, that would be already above and beyond too! |
I'll update the PR accordingly. I'll try to write the documentation, but if it's something you'd like to do yourself then just let me know :) Also, regarding the 3rd point, maybe "recommend" isn't the right word. I felt it would be nice if the docs mentioned that this is a workaround in case they have some limitation they're facing with respect to the typing and the containers not being injected into their factory functions. I was facing this issue and it took me some time to figure out that the easiest workaround was to just name my parameter |
Yeah I'll add something like that – don't worry about it and just fix what's breaking you. :) |
This equality check is for handling compatibility with Python 3.8 and 3.9 since the 'eval_str' parameter for 'inspect.signature' was only introduced in 3.10
@hynek I think it's ready for another review. The tests are passing for me on both 3.8 and 3.9 locally. Also, we don't need to have different implementations based on the version since if there's some exception then it falls back to using only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super cool, thanks!
Summary
Pull Request Check List
main
branch – use a separate branch!docs/core-concepts.md
or one of the integration guides by hand.versionadded
,versionchanged
, ordeprecated
directives..rst
and.md
files is written using semantic newlines.This should work in all the cases that are included in the tests, but it will still fail in the following case:
This can be handled, but we would have to add something like this to the
Registry
and then pass in that signature namespace mapping to_takes_container
which would then pass it to thelocals
ofinspect.signature
.Please let me know if there are any changes that needs to be made and I'd be happy to do so :)
NOTE: I haven't updated the changelog yet.