woltapp / magic-di

Dependency Injector with minimal boilerplate code, built-in support for FastAPI and Celery, and seamless integration to basically anything.
MIT License
43 stars 1 forks source link

typing.get_type_hints with TYPE_CHECKING imports is breaking dependency inspection #5

Open RB387 opened 7 months ago

RB387 commented 7 months ago
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import annotated_types

class Dependency:
    def __init__(self, db: Database, broken_hint: annotated_types.Any = 1):
        self.broken_hint = broken_hint

injected = injector.inject(Dependency)()

raises Exception

NameError: name 'annotated_types' is not defined

magic_di.exceptions.InspectionError: Failed to inspect
See the exception above

Proposed solution: Add fallback to inspect.signature or obj.__annotations__