From ffb64f2e888339797b50cbb024ed1209b29f7562 Mon Sep 17 00:00:00 2001 From: adamghill Date: Thu, 4 Jan 2024 18:47:26 -0500 Subject: [PATCH] Catch `NameError` for https://github.com/adamghill/django-unicorn/issues/639. --- django_unicorn/typer.py | 2 +- tests/test_typer.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django_unicorn/typer.py b/django_unicorn/typer.py index fc944979..8b15d395 100644 --- a/django_unicorn/typer.py +++ b/django_unicorn/typer.py @@ -78,7 +78,7 @@ def get_type_hints(obj) -> Dict: type_hints_cache[obj] = type_hints return type_hints - except TypeError: + except (TypeError, NameError): # Return an empty dictionary when there is a TypeError. From `get_type_hints`: "TypeError is # raised if the argument is not of a type that can contain annotations, and an empty dictionary # is returned if no annotations are present" diff --git a/tests/test_typer.py b/tests/test_typer.py index 66f7bd13..05dc942c 100644 --- a/tests/test_typer.py +++ b/tests/test_typer.py @@ -1,6 +1,8 @@ +import datetime from typing import Optional from typing import get_type_hints as typing_get_type_hints +from django_unicorn.components import UnicornView from django_unicorn.typer import cast_attribute_value, cast_value, get_type_hints from example.coffee.models import Flavor @@ -23,6 +25,16 @@ def test_func(input_str): assert actual == expected + +def test_get_type_hints_gh_639(): + class MyComponentView(UnicornView): + a_date: datetime.date + + expected = {"a_date": datetime.date} + actual = get_type_hints(MyComponentView(component_name="test", component_id="123")) + assert actual == expected + + class TestClass: integer: int boolean: bool