python-dependency-injector
python-dependency-injector copied to clipboard
Usage of built-in collection type aliases with starlette breaks wiring
trafficstars
from typing import List
from dependency_injector import containers
from dependency_injector.wiring import inject
# MyList = List[int]
MyList = list[int]
class Container(containers.DeclarativeContainer):
pass
@inject
def main():
pass
if __name__ == "__main__":
container = Container()
container.wire(modules=[__name__])
main()
The code above fails with the following error:
Traceback (most recent call last):
File "C:\Users\user\di_experiment\main.py", line 21, in <module>
container.wire(modules=[__name__])
File "src/dependency_injector/containers.pyx", line 317, in dependency_injector.containers.DynamicContainer.wire
File "C:\Users\user\.env3.10\lib\site-packages\dependency_injector\wiring.py", line 347, in wire
if _inspect_filter.is_excluded(member):
File "C:\Users\user\.env3.10\lib\site-packages\dependency_injector\wiring.py", line 311, in is_excluded
elif self._is_starlette_request_cls(instance):
File "C:\Users\user\.env3.10\lib\site-packages\dependency_injector\wiring.py", line 324, in _is_starlette_request_cls
and issubclass(instance, starlette.requests.Request)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class
Replacing list with typing.List solves the problem. Python version 3.10.11 dependency-injector==4.37.0 starlette==0.16.0