From 662f60af86536036889661e5abe26c156dd26681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Fri, 6 Dec 2024 17:54:36 +0100 Subject: [PATCH] fix: attribute types detection --- canaille/backends/models.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/canaille/backends/models.py b/canaille/backends/models.py index fc2889dc..bce8c00a 100644 --- a/canaille/backends/models.py +++ b/canaille/backends/models.py @@ -4,6 +4,7 @@ import typing from collections import ChainMap from typing import Annotated from typing import ClassVar +from typing import Union from typing import get_origin from typing import get_type_hints @@ -11,6 +12,14 @@ from canaille.app import classproperty from canaille.app import models from canaille.backends import Backend +try: + from types import UnionType # type: ignore + + UNION_TYPES = [Union, UnionType] +except ImportError: + # Python 3.9 has no UnionType + UNION_TYPES = [Union] + class Model: """The model abstract class. @@ -100,6 +109,13 @@ class BackendModel: else annotations ) + # Extract the Optional and Union type + attribute_type = ( + typing.get_args(attribute_type)[0] + if typing.get_origin(attribute_type) in UNION_TYPES + else attribute_type + ) + # Extract the Annotated annotation attribute_type, metadata = ( typing.get_args(attribute_type)