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

Add support for typing.Annotated #721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maintain0404
Copy link

Resolve #693.

This PR supports using typing.Annotated instead of parameter defaults when defining markers.

@TimNekk
Copy link

TimNekk commented Dec 6, 2023

This addition is really nice! I hope it will be merged

@mquinnfd
Copy link

mquinnfd commented Dec 8, 2023

@TimNekk Agreed! Have been watching this PR for a while now 🍿

@4c0n
Copy link

4c0n commented Mar 1, 2024

This would be really nice to have, perhaps it would make a bigger chance of getting merged in this fork: https://github.com/anton-petrov/python-dependency-injector ?

@maintain0404
Copy link
Author

@4c0n If this commit is used, rather than the copied code, you're welcome.

@abdok96
Copy link

abdok96 commented Mar 28, 2024

@maintain0404 sorry for submitting a review by mistake. I was just checking the changes.

I hope your PR get merged soon! However I noticed that your implementation don't support injections into modules and class attributes.

The following code should add support for that:

def _get_members_and_annotated(obj: Any) -> list[tuple[str, Any]]:
    members = inspect.getmembers(obj)
    for ann_name, annotation in inspect.get_annotations(obj).items():
        if get_origin(annotation) is Annotated:
            member = get_args(annotation)[1]
            members.append((ann_name, member))
    return members

And then in wire you use that function instead of inspect.getmembers:

def wire(  # noqa: C901
    container: Container,
    *,
    modules: Optional[Iterable[ModuleType]] = None,
    packages: Optional[Iterable[ModuleType]] = None,
) -> None:
    ....
    for module in modules:
        for member_name, member in _get_members_and_annotated(module):
             ...
             elif inspect.isclass(member):
                cls = member
                try:
                    cls_members = _get_members_and_annotated(cls)
                except Exception:  # noqa
                    # Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/441
                    continue

@JobaDiniz
Copy link

Hey, is this dead? Why didn't this get merged?

@wadinj
Copy link

wadinj commented Oct 17, 2024

Hello,

Is there any plan to getting this merge?

@stounfo
Copy link

stounfo commented Nov 19, 2024

Hey @rmk135,
Any chance that this pr will be considered soon?
Many thanks for considering this request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Injecting with typing.Annotated
8 participants