Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typesafe getter and setter #119

Open
jruebsam opened this issue Mar 6, 2020 · 0 comments
Open

Typesafe getter and setter #119

jruebsam opened this issue Mar 6, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@jruebsam
Copy link

jruebsam commented Mar 6, 2020

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

@jruebsam jruebsam added the enhancement New feature or request label Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant