Skip to content

UnicornView._attribute_names() fails in some components because of unresolved type hints #639

@pa-hqt

Description

@pa-hqt

One of my components crashes on page load due to recent type hint changes in django_unicorn.components.unicorn_view.UnicornView._attribute_names() of version 0.58.0 (commit 8a9448793)

The head of my component looks like

import datetime
from django_unicorn.components import UnicornView
from my_app.models import AggregatSt

class MyComponentView(UnicornView):
    aggregate: AggregatSt = None
    date: datetime.date = None

Exception:

Traceback (most recent call last):
[...]
  File "...\venv\lib\site-packages\django\template\base.py", line 966, in render_annotated
    return self.render(context)
  File "...\venv\lib\site-packages\django_unicorn\templatetags\unicorn.py", line 179, in render
    self.view = UnicornView.create(
  File "<decorator-gen-19>", line 2, in create
  File "...\venv\lib\site-packages\django_unicorn\components\unicorn_view.py", line 895, in create
    component = construct_component(
  File "<decorator-gen-3>", line 2, in construct_component
  File "...\venv\lib\site-packages\django_unicorn\components\unicorn_view.py", line 151, in construct_component
    component = component_class(
  File "...\venv\lib\site-packages\django_unicorn\components\unicorn_view.py", line 245, in __init__
    self._set_caches()
  File "<decorator-gen-5>", line 2, in _set_caches
  File "...\venv\lib\site-packages\django_unicorn\components\unicorn_view.py", line 273, in _set_caches
    self._attribute_names_cache = self._attribute_names()
  File "<decorator-gen-13>", line 2, in _attribute_names
  File "...\venv\lib\site-packages\django_unicorn\components\unicorn_view.py", line 593, in _attribute_names
    for type_hint_attribute_name in get_type_hints(self).keys():
  File "...\venv\lib\site-packages\django_unicorn\typer.py", line 75, in get_type_hints
    type_hints = typing_get_type_hints(obj)
  File "C:\Users\philip.arnold\AppData\Local\Programs\Python\Python310\lib\typing.py", line 1870, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "C:\Users\philip.arnold\AppData\Local\Programs\Python\Python310\lib\typing.py", line 327, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "C:\Users\philip.arnold\AppData\Local\Programs\Python\Python310\lib\typing.py", line 694, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
NameError: name 'AggregatSt' is not defined

In typing.get_type_hints()#L1859 the hints dictionary has following values:

hints = {'aggregate': 'AggregatSt', 'date': 'datetime.date'}

Since the values are strings, they are used to construct ForwardRef, but they cannot be solved, because globalns and localns are empty.

Any ideas, why the imports fail?
May it be a solution to add NameError to catch block in django_unicorn.typer#L81?

(same happens with 'datetime' if I remove the type hint of my custom class 'AggregatSt')

I am using python 3.10, Django 4.2.9 and django-unicorn 0.58.0, here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions