AttributeError: 'types.UnionType' object has no attribute '__origin__'
holtgrewe opened this issue · comments
Manuel Holtgrewe commented
Apparently, there is a problem with using union types with drf-pydantic.
The following model ...
class DragenStyleMetric(drf_pydantic.BaseModel):
"""Pydantic model for Dragen-style quality control metric entries"""
#: The section of the value
section: str | None
#: The "entry" in the section can be empty or the read group / sample
entry: str | None
#: The name of the metric
name: str | None
#: The count / ratio / time / etc. value
value: int | float | str | None
#: The count as percentage / seconds
value_float: float | None = None
... leads to the following error:
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/core/management/__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/django/apps/config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/holtgrem_c/Development/varfish-server/cases_import/models.py", line 32, in <module>
from cases_qc.io import cramino as io_cramino
File "/home/holtgrem_c/Development/varfish-server/cases_qc/io/cramino.py", line 7, in <module>
from cases_qc import models
File "/home/holtgrem_c/Development/varfish-server/cases_qc/models.py", line 16, in <module>
class DragenStyleMetric(drf_pydantic.BaseModel):
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/base_model.py", line 13, in __new__
setattr(cls, "drf_serializer", create_serializer_from_model(cls))
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/parse.py", line 68, in create_serializer_from_model
fields[field_name] = _convert_field(field)
File "/home/holtgrem_c/.local/share/virtualenvs/varfish-server-7_Oac6I8/lib/python3.10/site-packages/drf_pydantic/parse.py", line 141, in _convert_field
if field.type_.__origin__ is typing.Literal:
AttributeError: 'types.UnionType' object has no attribute '__origin__'
George Bocharov commented
Closing this issue because, sadly, DRF doesn't support union types and the intention of this package is to create a faithful mapping to standard DRF. Custom union types would create introspection issues. New v2 release addresses this by throwing an explicit exception when user provides unsupported union type - only optional union like <type> | None
is supported.