hynek / svcs

A Flexible Service Locator for Python.
https://svcs.hynek.me
MIT License
294 stars 19 forks source link

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

Closed guacs closed 11 months ago

guacs commented 11 months ago

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.

hynek commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

fixed by #55! 🎉