Skip to content

Pydantic does not seem to have a validator for NoneType #2095

@Zac-HD

Description

@Zac-HD

I encountered this while working on #2017:

from typing import List
from pydantic import BaseModel, Json

class JsonDemo(BaseModel):
    # I'd expect this to work like typing.List[None], but at definition-time it raises
    # RuntimeError: error checking inheritance of None (type: NoneType)
    none: Json[None]
JsonDemo(none="null")


class JsonWorkaround(BaseModel):
    # The workaround of using type(None) also fails at definition-time, raising
    # RuntimeError: no validator found for <class 'NoneType'>, see `arbitrary_types_allowed` in Config
    # The same error is apparent for List[None], so I think it's a more general bug
    json_null: Json[type(None)]
    list_of_none: List[None]

I would expect both Json[None] and List[None] to work, accepting in respectively "null" or a list (or sequence) where all the elements are None - casting need not be supported because None is a singleton.

Metadata

Metadata

Assignees

No one assigned

    Labels

    changeSuggested alteration to Pydantic, not a new feature nor a bugfeature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions