Why do I get a TypeError: requires a single typeΒΆ

The full error message looks something like this:

TypeError: typing.Optional requires a single type. Got Field(name=None,type=None,default=<dataclasses._MISSING_TYPE object at 0x7f79f4b0d700>,default_facto.

This error means the typing annotations for a model are ambiguous because they collide with a class field. If you use Python 3.7 or later, you can set PostponedAnnotations to true in the GeneratorOutput section of the generator config to solve this issue. This will enable Postponed Evaluations of Annotations (PEP 563) and the generated bindings will be able to be imported without errors. This feature is not available for Python 3.6 because annotations were added to the __future__ module in Python 3.7.0b1.

Example

from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.annotations.units import unit

__NAMESPACE__ = "http://domain.org/schema/model"


@dataclass
class Measurement:
    value: Optional[float] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    unit: Optional[unit] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class Weight(Measurement):
    class Meta:
        namespace = "http://domain.org/schema/model"