Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Any, Dict
- class PropertyTypeMixin:
- """
- Enforce type checking of attributes at runtime
- """
- _properties_type: Dict[str, Any] = {}
- def __setattr__(self, key, value):
- type_hint = self._properties_type.get(key)
- if type_hint and value and not isinstance(value, type_hint):
- err = (f'class {self.__class__.__name__}: Invalid type provided for attribute {key}. '
- f'Expected {type_hint.__name__}, got {key} = {value} ({type(value)}).')
- raise AttributeError(err)
- super().__setattr__(key, value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement