Skip to content

Typesafe getter and setter #119

@jruebsam

Description

@jruebsam

Currently setting the attributes of a derived class is not typesafe e.g.

@dataclass
 class Position(TypedJsonMixin):
    x : int 
    y : int 

p = Position(3, 3)
p.x = 1.2

is still possible. Does it make sense to overwrite the setatt method of the class
currently im doing this manually like this :

def __settattr__(self, name, value):
    if isinstance(value, type(getattr(self, name))):
        super().__setattr__(self, name, value)
    else:
        print(f'Type is not matching with that of {name} which is {type(getattr(self, name))}')

im not sure if this has some drawbacks at some point
maybe it would be an option

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions