Skip to main content

make dataclasses decorator type strict

Project description

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
)

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

strict_dataclasses-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

strict_dataclasses-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file strict_dataclasses-0.1.0.tar.gz.

File metadata

  • Download URL: strict_dataclasses-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for strict_dataclasses-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b9ad259f531e7e717b87b2cf336dbcb0b5064e4ed50bc4f9bc82c00418a7e271
MD5 7720259443c4a6ea94b5e9851aa388ac
BLAKE2b-256 9a7b372f660dbcf478f4a11781f5f0de70a3a950be6d483268d8ead27af65a7a

See more details on using hashes here.

File details

Details for the file strict_dataclasses-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for strict_dataclasses-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34e5e1c5aabde0f23adc5d83a2c3f6c0fe8b2a20dfd54b724683168d0e323c3f
MD5 e81ed2bdac12bafa4dbd26b3eaefa762
BLAKE2b-256 66075f5be7765bde31edd2478331e2fc0fbd6a1641e3760a6f0d0760b0a71a5b

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