fix: attribute types detection

This commit is contained in:
Éloi Rivard 2024-12-06 17:54:36 +01:00
parent 1bf196b5a2
commit 662f60af86
No known key found for this signature in database
GPG key ID: 7EDA204EA57DD184

View file

@ -4,6 +4,7 @@ import typing
from collections import ChainMap from collections import ChainMap
from typing import Annotated from typing import Annotated
from typing import ClassVar from typing import ClassVar
from typing import Union
from typing import get_origin from typing import get_origin
from typing import get_type_hints from typing import get_type_hints
@ -11,6 +12,14 @@ from canaille.app import classproperty
from canaille.app import models from canaille.app import models
from canaille.backends import Backend 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: class Model:
"""The model abstract class. """The model abstract class.
@ -100,6 +109,13 @@ class BackendModel:
else annotations 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 # Extract the Annotated annotation
attribute_type, metadata = ( attribute_type, metadata = (
typing.get_args(attribute_type) typing.get_args(attribute_type)