how.wtf

Python dataclass validation

· Thomas Taylor

Creating dataclasses in Python is simplistic; however, what if additional validation needs to be completed during initialization?

Creating a dataclass

The following example has a class named Person that models information about an individual:

1from dataclasses import dataclass
2
3@dataclass
4class Person:
5    first_name: str
6    last_name: str
7    age: int

This class can be instantiated in several ways:

1john = Person(first_name="john", last_name="doe", age=35)
2sally = Person(first_name="sally", last_name="may", age=-20)
3zachary = Person(first_name="zachary", last_name="taylor", age=12.5)
4print(john) # Person(first_name='john', last_name='doe', age=35)
5print(sally) # Person(first_name='sally', last_name='may', age=-20)
6print(zachary) # Person(first_name='zachary', last_name='taylor', age=12.5)

How to validate inputs for dataclasses

In the previous example, sally’s age was a non-positive integer and zachary’s age was a float value. To resolve erroneous input, a __post_init__ method can be used:

 1from dataclasses import dataclass
 2
 3@dataclass
 4class Person:
 5    first_name: str
 6    last_name: str
 7    age: int
 8
 9    def __post_init__(self):
10        if not isinstance(self.age, int):
11            raise ValueError("age is not an int")
12        if self.age <= 0:
13            raise ValueError("age must be a positive integer greater than 0")

sally’s error:

1ValueError: age must be a positive integer greater than 0

zachary’s error:

1ValueError: age is not an int

#Python  

Reply to this post by email ↪