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
)

Metadata

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 Provenance Actions
strict_dataclasses-0.1.0.tar.gz 4.4 kB Without provenance Details for strict_dataclasses-0.1.0.tar.gz

Built distribution (wheel)

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

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

Security for strict-dataclasses 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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