Skip to content

Improve the typing of pint wrapped method#147

Merged
alihamdan merged 2 commits intodevelopfrom
typing
Nov 17, 2023
Merged

Improve the typing of pint wrapped method#147
alihamdan merged 2 commits intodevelopfrom
typing

Conversation

@alihamdan
Copy link
Member

This PR improves the user facing types of parameters of methods wrapped with pint wrappers. Inside the methods, the types of parameters that accept either a quantity or a unitless value become incorrect but users of these methods now get the correct types. This means we accept a little bit of wrong types in our internal code for the benefit of our users. Until the Python type system have a "Map" type support, this is our best option.

This PR improves the user facing types of parameters of methods
wrapped with pint wrappers. Inside the methods, the types of parameters
that accept either a quantity or a unitless value become incorrect but users
of these methods now get the correct types. This means we accept a little
bit of wrong types in our internal code for the benefit of our users. Until
the Python type system have a "Map" type support, this is our best option.
@alihamdan alihamdan added the enhancement New feature or request label Nov 16, 2023
@alihamdan alihamdan self-assigned this Nov 16, 2023
@alihamdan
Copy link
Member Author

Example:

before

@ureg.wraps("V", ("V",), strict=False)
def f(v: float) -> Q_[float]:
    do_something(v)  # OK: v here is float, typed as float.
    ...

# user code
f(230.5)  # OK
f(Q_(20, "kV"))  # WRONG, v accepts float only

after

@ureg.wraps("V", ("V",), strict=False)
def f(v: float | Q_[float]) -> Q_[float]:
    do_something(v)  # WRONG: v here is float, typed as float | Q_[float].
    ...

# user code
f(230.5)  # OK
f(Q_(20, "kV"))  # OK

@alihamdan
Copy link
Member Author

Also numpy 2.0 removes np.float_ and np.complex_ https://numpy.org/devdocs/release/2.0.0-notes.html so I replaced them before they start emitting deprecation warnings

@alihamdan alihamdan merged commit c30202a into develop Nov 17, 2023
@alihamdan alihamdan deleted the typing branch November 17, 2023 08:43
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

Successfully merging this pull request may close these issues.

2 participants