mitchelllisle/sparkdantic

Better handling of typing.Literal

ealtamir opened this issue · 1 comments

I'm trying to derive the Spark schema of a BaseModel which I don't want to turn into a SparkModel, thus I'm creating a new model which subclasses from my original BaseModel and from SparkModel.

class MyClas(BaseModel):
    is_this_a_field: Literal["yes", "no"]


class SparkMyClass(SparkModel, MyClass):
    pass

When when trying to build a schema from SparkMyClass I get

>       if issubclass(t, Enum):
E       TypeError: issubclass() arg 1 must be a class

To fix this I need to redefine the Literal field in the base class.

My suggestion would be to add a check for Literal types and add a Spark field corresponding to the type of all the elements in the Literal, or fail if there's more than one type.

Hi @ealtamir,
I have put up a PR here #105

Will give you a chance to review over the next day or two before I merge it in and do a release.