Skip to content
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

container is not passed to the factory when using stringified annotations #54

Closed
guacs opened this issue Nov 18, 2023 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@guacs
Copy link
Contributor

guacs commented Nov 18, 2023

A minimal reproduction:

from __future__ import annotations

from svcs import Container, Registry


def get_int() -> int:
    return 1


def get_str(container: Container) -> str:
    return str(container.get(int))


reg = Registry()

reg.register_factory(int, get_int)
reg.register_factory(str, get_str)

container = Container(reg)

assert str(container.get(int)) == container.get(str)

The reason this fails is because inspect.signature does not resolve stringified annotations by default.

@guacs guacs added the bug Something isn't working label Nov 18, 2023
@hynek
Copy link
Owner

hynek commented Nov 18, 2023

heh I had problems writing a test for this and then I noticed that I've already been cheating:

if (annot := p.annotation) is Container or annot == "svcs.Container":

get_type_hints is a bit slow and janky (cf other issue 😬) would you be OK if I just add "Container" to it and call it a day?

@guacs
Copy link
Contributor Author

guacs commented Nov 18, 2023

I think I have a proper fix that should work for most use cases. I'll raise a PR and you can check if you like that approach?

@hynek hynek changed the title Bug: container is not passed to the factory when using stringified annotations container is not passed to the factory when using stringified annotations Nov 18, 2023
@hynek
Copy link
Owner

hynek commented Nov 19, 2023

fixed by #55! 🎉

@hynek hynek closed this as completed Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants