Skip to main content

Typed Dataclass

Typed Dataclass is a Python library providing a decorator for dataclasses that enables runtime type checking.

Installation

Use the package manager pip to install typed-dataclass.

pip install typed-dataclass

Usage

This library should be used in conjunction with the @dataclas decorator and should be placed below @dataclass. On object initialization it checks types by using typeguard package. Internally this works by modifying the __post__init__ method of the dataclass.

from dataclasses import dataclass
from typed_dataclass import typed_dataclass

@dataclass
@typed_dataclass
class MyClass:
    my_field: int

MyClass(12)   # will succeed
MyClass ("a") # will fail

Pre Validation

You can also implement logic that is happening before Type checks defining __before_type_check__ function:

from dataclasses import dataclass
from typed_dataclass import typed_dataclass

@dataclass
@typed_dataclass
class MyClass:
    my_int: int
    
    def __before_type_check__(self):
        self.my_int = int(self.my_int)

Testing

python -m unittest discover tests

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Metadata

Release files for typed-dataclass 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 typed-dataclass 0.1.0
File Size Uploaded
typed-dataclass-0.1.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for typed-dataclass 0.1.0
File Interpreter ABI Platform
typed_dataclass-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / typed-dataclass-0.1.0.tar.gz

Download URL typed-dataclass-0.1.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
626d5bd43f6d56f461a1e91b4a9f567af89c8fcc899fb14e6d579f8512249c6a
BLAKE2b-256 checksum
How to use checksums
983833acff8663f464d0575be8c96ee253a2ee6d9042ecb1cbd1d8c3cf123184
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

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

Download URL typed_dataclass-0.1.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19941da4caa633f2b904385d76d4503c88c21f0c0b4f1d708e53b7187ef68acb
BLAKE2b-256 checksum
How to use checksums
5357b80196490592706ebef9abb05fca812d7cbe22ca2c69023f07bd7de33118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

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