Skip to content

Instantly share code, notes, and snippets.

@rscarrera27
Last active November 12, 2019 08:34
Show Gist options
  • Save rscarrera27/64f8c0e75a50e0953b7da662012135ff to your computer and use it in GitHub Desktop.
Save rscarrera27/64f8c0e75a50e0953b7da662012135ff to your computer and use it in GitHub Desktop.
Simple type check class
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