mongodb-labs / full-stack-fastapi-mongodb

Full stack, modern web application generator. Using FastAPI, MongoDB as database, Docker, automatic HTTPS and more.
MIT License
453 stars 80 forks source link

backend exits "TypeError: type 'types.UnionType' is not subscriptable" #41

Closed burakbudanur closed 3 months ago

burakbudanur commented 4 months ago

Hi! When I follow Quickstart instructions, the backend exits with the following error. I realized this after login didn't work so https://github.com/mongodb-labs/full-stack-fastapi-mongodb/issues/34 might also be related.

2024-04-24 12:52:35 INFO:__main__:Initializing service
2024-04-24 12:52:35 INFO:__main__:Starting call to '__main__.init', this is the 1st time calling it.
2024-04-24 12:52:35 INFO:__main__:Service finished initializing
2024-04-24 12:52:40 Traceback (most recent call last):
2024-04-24 12:52:40   File "/app/app/initial_data.py", line 4, in <module>
2024-04-24 12:52:40     from app.db.init_db import init_db
2024-04-24 12:52:40   File "/app/app/db/init_db.py", line 3, in <module>
2024-04-24 12:52:40     from app import crud, schemas
2024-04-24 12:52:40   File "/app/app/crud/__init__.py", line 1, in <module>
2024-04-24 12:52:40     from .crud_user import user
2024-04-24 12:52:40   File "/app/app/crud/crud_user.py", line 7, in <module>
2024-04-24 12:52:40     from app.models.user import User
2024-04-24 12:52:40   File "/app/app/models/__init__.py", line 1, in <module>
2024-04-24 12:52:40     from .user import User
2024-04-24 12:52:40   File "/app/app/models/user.py", line 17, in <module>
2024-04-24 12:52:40     class User(Base):
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/odmantic/model.py", line 487, in __new__
2024-04-24 12:52:40     return super().__new__(mcs, name, bases, namespace, **kwargs)
2024-04-24 12:52:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/odmantic/model.py", line 405, in __new__
2024-04-24 12:52:40     cls = super().__new__(mcs, name, bases, namespace, **kwargs)
2024-04-24 12:52:40           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 178, in __new__
2024-04-24 12:52:40     set_model_fields(cls, bases, config_wrapper, types_namespace)
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 452, in set_model_fields
2024-04-24 12:52:40     fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
2024-04-24 12:52:40                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_fields.py", line 122, in collect_model_fields
2024-04-24 12:52:40     type_hints = get_cls_type_hints_lenient(cls, types_namespace)
2024-04-24 12:52:40                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 212, in get_cls_type_hints_lenient
2024-04-24 12:52:40     hints[name] = eval_type_lenient(value, globalns, localns)
2024-04-24 12:52:40                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 224, in eval_type_lenient
2024-04-24 12:52:40     return eval_type_backport(value, globalns, localns)
2024-04-24 12:52:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 240, in eval_type_backport
2024-04-24 12:52:40     return typing._eval_type(  # type: ignore
2024-04-24 12:52:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 12:52:40   File "/usr/local/lib/python3.11/typing.py", line 382, in _eval_type
2024-04-24 12:52:40     t = t.__origin__[args]
2024-04-24 12:52:40         ~~~~~~~~~~~~^^^^^^
2024-04-24 12:52:40 TypeError: type 'types.UnionType' is not subscriptable
2024-04-24 12:52:40 Traceback (most recent call last):
2024-04-24 12:52:40   File "<frozen runpy>", line 198, in _run_module_as_main
2024-04-24 12:52:40   File "<frozen runpy>", line 88, in _run_code
2024-04-24 12:52:40   File "/app/inboard/start.py", line 134, in <module>
2024-04-24 12:52:40     run_pre_start_script(logger=logger)
2024-04-24 12:52:40   File "/app/inboard/start.py", line 30, in run_pre_start_script
2024-04-24 12:52:40     subprocess.run([process, pre_start_path], check=True)
2024-04-24 12:52:40   File "/usr/local/lib/python3.11/subprocess.py", line 571, in run
2024-04-24 12:52:40     raise CalledProcessError(retcode, process.args,
2024-04-24 12:52:40 subprocess.CalledProcessError: Command '['sh', '/app/prestart.sh']' returned non-zero exit status 1.
2024-04-24 13:04:24 INFO:__main__:Initializing service
2024-04-24 13:04:24 INFO:__main__:Starting call to '__main__.init', this is the 1st time calling it.
2024-04-24 13:04:24 INFO:__main__:Service finished initializing
2024-04-24 13:04:26 Traceback (most recent call last):
2024-04-24 13:04:26   File "/app/app/initial_data.py", line 4, in <module>
2024-04-24 13:04:26     from app.db.init_db import init_db
2024-04-24 13:04:26   File "/app/app/db/init_db.py", line 3, in <module>
2024-04-24 13:04:26     from app import crud, schemas
2024-04-24 13:04:26   File "/app/app/crud/__init__.py", line 1, in <module>
2024-04-24 13:04:26     from .crud_user import user
2024-04-24 13:04:26   File "/app/app/crud/crud_user.py", line 7, in <module>
2024-04-24 13:04:26     from app.models.user import User
2024-04-24 13:04:26   File "/app/app/models/__init__.py", line 1, in <module>
2024-04-24 13:04:26     from .user import User
2024-04-24 13:04:26   File "/app/app/models/user.py", line 17, in <module>
2024-04-24 13:04:26     class User(Base):
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/odmantic/model.py", line 487, in __new__
2024-04-24 13:04:26     return super().__new__(mcs, name, bases, namespace, **kwargs)
2024-04-24 13:04:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/odmantic/model.py", line 405, in __new__
2024-04-24 13:04:26     cls = super().__new__(mcs, name, bases, namespace, **kwargs)
2024-04-24 13:04:26           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 178, in __new__
2024-04-24 13:04:26     set_model_fields(cls, bases, config_wrapper, types_namespace)
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 452, in set_model_fields
2024-04-24 13:04:26     fields, class_vars = collect_model_fields(cls, bases, config_wrapper, types_namespace, typevars_map=typevars_map)
2024-04-24 13:04:26                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_fields.py", line 122, in collect_model_fields
2024-04-24 13:04:26     type_hints = get_cls_type_hints_lenient(cls, types_namespace)
2024-04-24 13:04:26                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 212, in get_cls_type_hints_lenient
2024-04-24 13:04:26     hints[name] = eval_type_lenient(value, globalns, localns)
2024-04-24 13:04:26                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 224, in eval_type_lenient
2024-04-24 13:04:26     return eval_type_backport(value, globalns, localns)
2024-04-24 13:04:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/app/.venv/lib/python3.11/site-packages/pydantic/_internal/_typing_extra.py", line 240, in eval_type_backport
2024-04-24 13:04:26     return typing._eval_type(  # type: ignore
2024-04-24 13:04:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-24 13:04:26   File "/usr/local/lib/python3.11/typing.py", line 382, in _eval_type
2024-04-24 13:04:26     t = t.__origin__[args]
2024-04-24 13:04:26         ~~~~~~~~~~~~^^^^^^
2024-04-24 13:04:26 TypeError: type 'types.UnionType' is not subscriptable
2024-04-24 13:04:26 Traceback (most recent call last):
2024-04-24 13:04:26   File "<frozen runpy>", line 198, in _run_module_as_main
2024-04-24 13:04:26   File "<frozen runpy>", line 88, in _run_code
2024-04-24 13:04:26   File "/app/inboard/start.py", line 134, in <module>
2024-04-24 13:04:26     run_pre_start_script(logger=logger)
2024-04-24 13:04:26   File "/app/inboard/start.py", line 30, in run_pre_start_script
2024-04-24 13:04:26     subprocess.run([process, pre_start_path], check=True)
2024-04-24 13:04:26   File "/usr/local/lib/python3.11/subprocess.py", line 571, in run
2024-04-24 13:04:26     raise CalledProcessError(retcode, process.args,
2024-04-24 13:04:26 subprocess.CalledProcessError: Command '['sh', '/app/prestart.sh']' returned non-zero exit status 1.
Jibola commented 4 months ago

Hi @burakbudanur

Thansk for pulling up this issue. Upon further investigation, it looks like the types.UnionType in the User(Base) model of app/app/models/user.py doesn't support the types.UnionType operation. The Base class is an odmantic.Model type and (issue ticket here) that is where the error is occurring. I'm putting up a fix to switch that back to Optional[int].

Fix: #42

Let me know if this resolves your issue.