TrustPoint-Project / trustpoint

MIT License
5 stars 0 forks source link

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

Closed FHatCSW closed 8 months ago

FHatCSW commented 8 months ago

When I try to setup the trustpoint I get a TypeError for trustpoint/util/x509/credentials.py:

python manage.py makemigrations

Traceback (most recent call last):
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/manage.py", line 22, in <module>
    main()
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/management/base.py", line 453, in execute
    self.check()
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 494, in check
    for pattern in self.url_patterns:
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/trustpoint/urls.py", line 29, in <module>
    path('pki/', include('pki.urls')),
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/venv/lib/python3.9/site-packages/django/urls/conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/pki/urls.py", line 2, in <module>
    from . import views
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/pki/views.py", line 3, in <module>
    from util.x509.credentials import CredentialUploadHandler
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/util/x509/credentials.py", line 41, in <module>
    class P12:
  File "/Users/florianhandke/PycharmProjects/trustpoint_stack/trustpoint/util/x509/credentials.py", line 52, in P12
    def full_cert_chain_as_json(self) -> list[dict[str, [str, str | None]]]:
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

After adding from __future__ import annotations to trustpoint/util/x509/credentials.py I can execute python manage.py makemigrations No error that breaks the setup but a SystemCheckError:

python manage.py migrate

SystemCheckError: System check identified some issues:

ERRORS:
pki.IssuingCa.config_type: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
pki.IssuingCa.curve: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
pki.IssuingCa.key_type: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
pki.IssuingCa.localization: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.

WARNINGS:
?: (urls.W005) URL namespace 'onboarding' isn't unique. You may not be able to reverse all URLs in this namespace

Python Version: Python 3.9.18

FHatCSW commented 8 months ago

The issue seems to be related to the Python version. After updating to Python 3.12.1 it works like a charm ;)

@Aircoookie, @AlexCampusSchwarzwald This is something we should consider and simplify.

AlexHx8472 commented 8 months ago

I guess I used new features of the python typing part. Should be working with python >= 3.11 though. Operand | meaning OR. Before you'd have to use Optional[typeX] or Union[typeA, typeB, ...] I would like to use the latest typing features. It's just more concise and better to read.

from future import annotations should make it available for older python versions, if we intend to support older ones.

Regarding the second issue. It may be better if we do not push migrations to the repo. That may break things quickly in development. To clear all db and migrations, delete the db.sqlite3 file and also delete all migration files. That is all files within the migrations directories except the init.py files. (Keep the init.py files!)

Then execute makemigrations and migrate again. (Remember to execute createsuperuser again, if you want to login / use the admin page: localhost:8000/admin)