forked from Github-Mirrors/canaille
fix: attribute types detection
This commit is contained in:
parent
1bf196b5a2
commit
662f60af86
1 changed files with 16 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue