Skip to main content

strict-dataclass

English | 简体中文

A Python standard library dataclasses enhancement implementation that provides strict type checking.

This is an experimental library. Please do not use it in production environments. You might want to use pydantic or attrs instead.

Features

  • Advantages:
    • Compatible with standard library dataclasses usage
    • Strict type checking during initialization and attribute modification
    • Supports validation of nested complex data types
    • Unlike pydantic, it doesn't deep copy input values
    • Performs deep checking of nested complex data types (pydantic's BaseModel / dataclass doesn't perform deep checking)
  • Disadvantages:
    • Poor performance, object creation and attribute modification takes about 3 times longer than pydantic. However, it seems acceptable considering the cost of deep checking.

Installation

pip install strict-dataclass
# Python >= 3.11

Usage

Basic Usage

from strict_dataclasses import strict_dataclass

@strict_dataclass
class User:
    name: str
    age: int
    is_active: bool

# Correct usage - passes type checking
user = User(name="Alice", age=25, is_active=True)

# Incorrect usage - raises TypeError
user = User(name=123, age="25", is_active=1)  # TypeError: Field 'name' must be of type str

Complex Types

from typing import Optional
from strict_dataclasses import strict_dataclass

@strict_dataclass
class Student:
    name: str
    scores: dict[str, list[int]]
    tags: list[str]
    note: Optional[str] = None

# Correct usage
student = Student(
    name="Bob",
    scores={"math": [90], "english": [85]},
    tags=["good", "active"],
    note="Excellent student"
)

# Type errors
student = Student(
    name="Bob",
    scores={"math": ["90"]},  # TypeError: Value must be a list[int]
    tags="good",  # TypeError: Must be a list
    note=123  # TypeError: Must be a string or None
)

Release files for strict-dataclasses 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strict-dataclasses 0.1.0
File Size Uploaded
strict_dataclasses-0.1.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for strict-dataclasses 0.1.0
File Interpreter ABI Platform
strict_dataclasses-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / strict_dataclasses-0.1.0.tar.gz

Download URL strict_dataclasses-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b9ad259f531e7e717b87b2cf336dbcb0b5064e4ed50bc4f9bc82c00418a7e271
BLAKE2b-256 checksum
How to use checksums
9a7b372f660dbcf478f4a11781f5f0de70a3a950be6d483268d8ead27af65a7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release files / strict_dataclasses-0.1.0-py3-none-any.whl

Download URL strict_dataclasses-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34e5e1c5aabde0f23adc5d83a2c3f6c0fe8b2a20dfd54b724683168d0e323c3f
BLAKE2b-256 checksum
How to use checksums
66075f5be7765bde31edd2478331e2fc0fbd6a1641e3760a6f0d0760b0a71a5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page