ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
694 stars 79 forks source link

Add attr_dc that works when using ClassVar annotation on class variable #71

Closed EspenAlbert closed 3 years ago

EspenAlbert commented 4 years ago

First I want to say that I really like this library. Great work :)

Using a ClassVar annotation on an attribute failed for dataclasses:

from dataclasses import dataclass
from typing import ClassVar
import inject

@dataclass
class MyClass:
    field: ClassVar[int] = inject.attr(int)

raises: inject.InjectorException: No injector is configured

I was not sure whether to change the _AttributeInjection or give an argument to the attr function (something like is_dataclass). Or if this is even desired since it works when you are not using the ClassVar annotation: field = inject.attr(int)

ivankorobkov commented 3 years ago

Hi!

Thank you for a pull request. Unfortunately, the tests do not pass. Please, fix them.

EspenAlbert commented 3 years ago

This is missing the latest commit that makes the tests work on 3.6. https://github.com/EspenAlbert/python-inject/commit/fe67991f12d73b1df9ac19203bbef0181c3ae6c6