Skip to content

Instantly share code, notes, and snippets.

@Kludex
Created July 13, 2023 08:24
Show Gist options
  • Save Kludex/1d8c1458d4d9f2e0db56ad6e62792ba9 to your computer and use it in GitHub Desktop.
Save Kludex/1d8c1458d4d9f2e0db56ad6e62792ba9 to your computer and use it in GitHub Desktop.
How to create an `NonEmpty` type with Pydantic by Adrian
from typing import Any, Collection, List, TypeVar, Annotated
from annotated_types import MinLen
from pydantic import BaseModel
_CollectionT = TypeVar("_CollectionT", bound=Collection[Any])
NonEmpty = Annotated[_CollectionT, MinLen(1)]
class Book(BaseModel):
title: str
source_records: NonEmpty[List[NonEmpty[str]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment