Skip to main content

Generate randomized instances of pydantic classes for testing

Project description

pydantic-rng

A simple library for generating random, valid Pydantic models.

This tool helps you quickly create mock data for testing, prototyping, and demonstration purposes by generating random values that conform to your Pydantic model schemas. It supports a wide range of types and validation constraints.

✨ Features

  • Type-aware Generation: Generates appropriate data for common Python types (str, int, float, bool, list, dict, set, etc.).
  • pydantic Integration: Works seamlessly with Pydantic's BaseModel for recursive data generation.
  • Constraint Support: Respects a wide variety of Pydantic validation constraints from the annotated_types library, including MinLen, MaxLen, Ge, Gt, Le, Lt, and MultipleOf.
  • Configurable: Customize the behavior of the random generator with global settings for things like numeric and string ranges, and the probability of generating None for optional fields.

🚀 Installation

Install the package (usually as a dev dependency):

uv add --dev pydantic-rng

Generate Basic Class RNG

from pydantic import BaseModel
from pydantic_rng import generate

class User(BaseModel):
    user_id: int
    name: str
    is_active: bool
    email: str | None

random_user = generate(User)
print(random_user.model_dump_json(indent=2))

Or, more usually...

from typing import Annotated

from tqdm.rich import trange # requires `rich` as well
from pydantic import BaseModel, Field
from pydantic_rng import generate

N = 1_000_000


class World(BaseModel):
  radius: Annotated[float, Field(gt=0, lt=360)]

with open("random-data.jsonl", "w") as f:
  for _ in trange(N):
    item = generate(World)
    f.write(item.model_dump_json() + "\n")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_rng-0.1.6.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_rng-0.1.6-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_rng-0.1.6.tar.gz.

File metadata

  • Download URL: pydantic_rng-0.1.6.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.14

File hashes

Hashes for pydantic_rng-0.1.6.tar.gz
Algorithm Hash digest
SHA256 41232ab13f74548f92619006ed9b4b8f0000ebac02e0913317ce639cbaf78447
MD5 4c4f00c8aea29fb4f2646683fe27a019
BLAKE2b-256 cd91bd3983c4b881e2b9ac781fac3ab1eedfcace68d3d474451bdc166655fdda

See more details on using hashes here.

File details

Details for the file pydantic_rng-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_rng-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f4b7788b64ad3bf23ce7820c74fec69052de36f55fa4915f22621ac9d97bec36
MD5 603668db3a05fb51993cebb92af356db
BLAKE2b-256 b8665dec511b2f3fcd99e488e317e230fbc8875b1c41840022bb421fbf40242f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page