Assign Type Checking

Type checking can be done by adding types to assign() method. Type checking ensures the type of **kwargs values that are assigned to attributes of current instance of class.

In the following example attribute speed can be of type float or of type int. All other type will result in an error.

from kwhelp import KwargsHelper

class MyClass:
    def __init__(self, **kwargs):
        kw = KwargsHelper(self, {**kwargs})
        kw.assign(key="speed", types=[int, float])
>>> myclass = MyClass(speed=123)
>>> print(myclass._speed)
123
>>> myclass = MyClass(speed=19.8)
>>> print(myclass._speed)
19.8
>>> myclass = MyClass(speed="a")
TypeError: MyClass arg 'speed' is expected to be of '<class 'float'> | <class 'int'>' but got 'str'