Skip to content

ManuelZierl/typed-dataclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages