Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType' #4698

Closed
sandeepeecs opened this issue Nov 27, 2023 · 2 comments
Closed
Labels

Comments

@sandeepeecs
Copy link

What happened?

When running
$ python manage.py migrate

def _create_user(self, email: str, password: str | None, **extra_fields):

TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

What should've happened instead?

migrations should be run properly

Additional details

I think this error is because its expecting python 3.11 but i wanted to use python 3.9 and it does not support use of | symbol

Error trace

(photoapp) (base) sandeep@sandeeps-MacBook-Pro photoapp % python manage.py migrate
Traceback (most recent call last):
File "/Users/sandeep/work/photoapp/photoapp/manage.py", line 31, in
execute_from_command_line(sys.argv)
File "/Users/sandeep/.venvs/photoapp/lib/python3.9/site-packages/django/core/management/init.py", line 442, in execute_from_command_line
utility.execute()
File "/Users/sandeep/.venvs/photoapp/lib/python3.9/site-packages/django/core/management/init.py", line 416, in execute
django.setup()
File "/Users/sandeep/.venvs/photoapp/lib/python3.9/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/sandeep/.venvs/photoapp/lib/python3.9/site-packages/django/apps/registry.py", line 116, in populate
app_config.import_models()
File "/Users/sandeep/.venvs/photoapp/lib/python3.9/site-packages/django/apps/config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
File "/Users/sandeep/.pyenv/versions/3.9.18/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/Users/sandeep/work/photoapp/photoapp/photoapp/users/models.py", line 6, in
from photoapp.users.managers import UserManager
File "/Users/sandeep/work/photoapp/photoapp/photoapp/users/managers.py", line 5, in
class UserManager(DjangoUserManager):
File "/Users/sandeep/work/photoapp/photoapp/photoapp/users/managers.py", line 8, in UserManager
def _create_user(self, email: str, password: str | None, **extra_fields):
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

Logs:
$ cookiecutter https://github.com/cookiecutter/cookiecutter-django
project_name [Project Name]: ...
@browniebroke
Copy link
Member

What's the python version you use? python --version

@luzfcb
Copy link
Collaborator

luzfcb commented Nov 27, 2023

@sandeepeecs From the logs and your comment, you are using python3.9. However the current version of cookiecutter-django expects python3.11.

To fix the problem on the users/managers.py, simply add from __future__ import annotations at the top (first line) of the users/managers.py file and any other python file that makes use of the | operator.

Observe this will not prevent/fix that the same error from happening due to some Python dependency making use of functionality that only exists in Python3.10+. Ideally, it would be best if you used Python3.11 or higher.

The | operator for type hint was included in Python3.10+ by PEP 604

@luzfcb luzfcb closed this as completed Nov 27, 2023
@cookiecutter cookiecutter locked and limited conversation to collaborators Nov 27, 2023
@luzfcb luzfcb converted this issue into discussion #4699 Nov 27, 2023
@luzfcb luzfcb added invalid and removed bug labels Nov 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants