Last active
November 12, 2019 08:34
-
-
Save rscarrera27/64f8c0e75a50e0953b7da662012135ff to your computer and use it in GitHub Desktop.
Simple type check class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from typing import get_type_hints | |
@dataclass | |
class SimpleTypeCheck: | |
def __post_init__(self): | |
for k, t in get_type_hints(type(self)).items(): | |
k_v = getattr(self, k) | |
if not isinstance(k_v, t): | |
raise Exception( | |
f"Attribute [{k}] expected type [{t}] but got type [{type(k_v)}]" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment