-
Notifications
You must be signed in to change notification settings - Fork 465
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
Typing in 0.22 #1770
Comments
Now that I have refactored the code, I have a lot of doubts in relation to make Quantity Generic over the magnitude. I think it complicates things, and we get little in return: mainly to flag something as a scalar or array quantity, but I am not sure if it works well. Something to consider, maybe not to change now but for the future, |
For my use case (sustainable finance and climate change), the use case for typing in Pint is to ensure that certain dimensions are present. For example, Emissions should be an amount of GHG gas (typically tons of CO2e). Emissions intensity should be an amount of GHG gas per unit of Production (and there are many units of production--tons of Steel, tons of Cement, square meters of buildings, petajoules, barrels of oil, therms, cubic meters of natural gas, etc, so that's like tons of CO2e per Generic). Monetary quantities should be in some form of currency (which, because of floating rates are easily typed--but not easily converted--at parsing time). |
Hi! I am trying to figure out how to type hint with pint and I haven't found a solution yet. I tried some of the stuff from here but I doesn't seem to work. My use case is as follows: I made some code that has functions that take a value and a unit. Some signatures here as an example: import numpy as np
def func_scalar(x: float, unit: str) -> float:
pass
def func_array(x: np.ndarray[(3, ), int], unit: str) -> np.ndarray[(3, ), int]:
pass
But then I discovered pint and I though "that's amazing! I should just use pint!". I hoped I could do something like this: import numpy as np
import pint
def func_scalar(x: pint.Quantity[float]) -> pint.Quantity[float]:
pass
def func_array(x: pint.Quantity[np.ndarray[(3, ), int]]) -> pint.Quantity[np.ndarray[(3, ), int]]:
pass But this results in the So, my question is: is there currently a way to achieve something like my goal? |
Hi guys. Sharing my solution in case anybody else searching for a solution to this bumps into this thread. The solution I ended up with is to use import pint
import numpy as np
from typing import Annotated, Any
# A function to compute the Sound Pressure Level (SPL) of a pressure time series.
def spl(
pressure: Annotated[pint.Quantity, np.ndarray[(Any, ), float]],
reference = Annotated[pint.Quantity, float]
) -> Annotated[pint.Quantity, np.ndarray[(Any, ), float]]:
return ((np.sqrt(np.nanmean(pressure**2)) / reference)**2).to('decibel') It doesn't give the best autocompletion in PyCharm (which is something I enjoy about type hints) but for now It might be good enough for my purposes. |
@hgrecco: Please add |
Here's my attempt to create a semi-generic Quantity type that can be used to build class structures containing specific Quantity types using Pydantic 2.x:
|
Just as a side note, the solution proposed by @stefano-tronci will only work for Python version >= 3.9 as |
I'm also trying to find with a solution that combines runtime dimensionality validation with static type safety. Sadly none of the solutions shared here or at #1166 typechecks statically (at least with mypy). The most promising direction I've found so far is something like the InstanceOf Pydantic validator that makes Applying this idea to pint, I tried to come up with a |
This issue is to address typing related topics that we should for 0.22. The objective is not to fix every typing aspects but those exposed to the users
The text was updated successfully, but these errors were encountered: