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

Replace SingleValue types in data_containers.py with typing.NewType #2101

Open
samtygier-stfc opened this issue Mar 5, 2024 · 0 comments
Open

Comments

@samtygier-stfc
Copy link
Collaborator

Current Behaviour

MI use subtypes of SingleValue (itself a dataclass) to provide distinct types for a range of quantities, in order to allow type checking for some function calls. These are wrappers around a base type. For example a Degree holds a float, but the type checker can prevent a Slope (also holding a float), being passed to a method that takes a Degree. There is also some code for runtime type checking in the SingleValue constructor, but this does not run for subtypes. Degrees("Hello") does not raise a error at runtime.

Desired Behaviour

Since python 3.5 the typing model has offered NewType, this has most of the static benefits with lower runtime overhead. We'd loose the ability to add constructors to do runtime checking, but we were not using this. And we loose the ability override operators, but we were not enforcing anything with this.

We would no longer need to use a.value to unpack the value, because at runtime there is no wrapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant