-
Notifications
You must be signed in to change notification settings - Fork 476
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
Type checking of UnitRegistry instance calls are potentially broken, or return values are mistyped. #1642
Comments
Let's first tackle the following questions:
The answer to (1) has always been to parse the string as a math expression replacing names by their unit respective unit objects. This has not changed since Pint was started (except for the fact that in the beginning Unit and Quantity were just one object) The answer to (2) has been call So indeed is type hinting of these functions what needs to be fixed. I guess they should return something like: Regarding the |
The distinction is that from typing import TypeVar
from pint import Quantity
T = TypeVar("T")
def ratio(top: Quantity[T], bottom: Quantity[T]) -> T:
return (top / bottom).m_as("") It now results in (This is linked to our using Decimal, of course. We try to ensure that floats don't sneak into our calculations in the wrong places.) |
Weird.
I fully support this. I am willing to make changes to bring this back! |
Changes in Quantity mean that basic methods on Quantity instances returned by calls to a UnitRegistry now result in typechecking errors in mypy. In our code, it breaks check and m_as. This does not occur if
ureg.Quantity
is called instead.This may be caused by calls to UnitRegistry directly potentially not being Quantity instances (they can simply be numbers), and so the type checking here might be an improvement, but it also appears that those calls are documented as returning a Quantity ("Parse a mathematical expression including units and return a quantity object."), so there appears to be either a problem of function or of documentation here, and it appears that the type hinting within pint may be incorrectly stating that the calls return a Quantity.
This appears to be caused by c082656, and so is present in 0.19.3 and later.
A minimal example:
Results in mypy error:
error: Quantity? has no attribute "check"
forq.check
andq4.check
, and no error forq2.check
.q4.check
also fails, because q4 is anint
.(Note that the Quantity to PlainQuantity change also breaks quite a bit of typechecking downstream, for anyone who was using a TypeVar with the quantity. However, this is fixable by replacing Quantity with PlainQuantity.)
The text was updated successfully, but these errors were encountered: