ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
3.86k stars 303 forks source link

The example “fastapi-sqlalchemy” cannot be executed ... #501

Open hustlmh opened 3 years ago

hustlmh commented 3 years ago

hi, I cloned the git-master code and try to run the example without any modification in my miniconda environment where python version is 3.8.11, but the example “fastapi-sqlalchemy” cannot be executed. The error message is "TypeError: 'ABCMeta' object is not subscriptable". related environment is uvicorn 0.15.0 fastapi 0.68.1 dependency-injector 4.36.0

how to fix it ? Thanks !

fastapi-sqlalchemy % python -m webapp.application

Traceback (most recent call last): File "/Users/zoo/miniconda3/envs/gui/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/Users/zoo/miniconda3/envs/gui/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/Users/zoo/Downloads/python-dependency-injector-master/examples/miniapps/fastapi-sqlalchemy/webapp/application.py", line 5, in from .containers import Container File "/Users/zoo/Downloads/python-dependency-injector-master/examples/miniapps/fastapi-sqlalchemy/webapp/containers.py", line 5, in from .database import Database File "/Users/zoo/Downloads/python-dependency-injector-master/examples/miniapps/fastapi-sqlalchemy/webapp/database.py", line 16, in class Database: File "/Users/zoo/Downloads/python-dependency-injector-master/examples/miniapps/fastapi-sqlalchemy/webapp/database.py", line 32, in Database def session(self) -> Callable[..., AbstractContextManager[Session]]: TypeError: 'ABCMeta' object is not subscriptable

rmk135 commented 3 years ago

Thanks for reporting the issue @hustlmh . After a quick search, I found that adding from __future__ import annotations might help as a workaround. I didn't test it yet. A dirtier but 100% working fix is to just remove type annotations from line 32:

    @contextmanager
    def session(self) -> Callable[..., AbstractContextManager[Session]]:

I'll also try to see how to make it work on all supported Python versions. Thanks again.

vlio20 commented 2 years ago

@rmk135 where should this import be set? @hustlmh , did it work for you?