How to write interface (contract) for object property (attribute) existence in python? How to write interface (contract) for object property (attribute) existence in python? python-3.x python-3.x

How to write interface (contract) for object property (attribute) existence in python?


You don't need to create new interface or implement ABC. You can use @dataclass. I am using default code checker in pycharm (warnings are in comments).

from dataclasses import dataclass@dataclassclass MyType:    my_prop: int@dataclassclass SomeType:    my_prop_out: intdef my_func_ok(my_param: MyType) -> SomeType:    some_type = SomeType(my_prop_out=my_param.my_prop)    return some_typedef my_func_bad(my_param: MyType) -> SomeType:    return my_param              # this is not returning SomeTypemy_type = MyType()               # this is expecting to set my_propmy_type = MyType(my_prop="sss")  # this is expecting to set my_prop with int not strmy_func_ok(my_param=100)         # this is expecting MyType objectmy_func_ok(my_param=MyType(my_prop=10))  # this is correct, no errors

I am adding picture of pycharm code checker warnings:


enter image description here