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

Type hints? #778

Closed
likewei92 opened this issue Mar 19, 2019 · 6 comments
Closed

Type hints? #778

likewei92 opened this issue Mar 19, 2019 · 6 comments

Comments

@likewei92
Copy link

Is it possible to expose certain classes like Quantity such that they can be used with type hints (https://docs.python.org/3/library/typing.html)?

For example like this:

def my_function(l: Quantity[ureg.meter]) -> Quantity[ureg.second]
@hgrecco
Copy link
Owner

hgrecco commented Mar 20, 2019

It is an interesting proposal. But this would require to create another class for type hinting analog to how List is to list

@MatthewWilkes
Copy link

I would love type hinting support in pint, but this is indeed tricky. I've been thinking about this for the last hour or so and it just keeps getting harder.

The fact that there are lots of different units that are equivalent dimensionality but this isn't known until runtime is the killer for me.

The function

def height_of_liquid_in_tube(cross_sectional_area: Quantity[ureg.centimeter**2], volume: Quantity[ureg.litre]) -> ureg.centimeter:
    return (volume / cross_sectional_area).to(ureg.centimeter)

works equally well if given:

height_of_liquid_in_tube(3 * ureg.microacre, 2*ureg.sys.imperial.gill)

so Quantity[ureg.litre] would need to be a reference to Quantity[Dimensionality[Length, Length, Length]] or similar. I don't see a way of making it possible to do this generically, as this information involves reading the definitions files.

My instinct is that the only way to do this would be to maintain a separate set of pyi files that approximates pint without much of the depth, hard coding the dimensionality of certain units that are in common usage, but that would cause problems for anyone using a registry backed by definitions files that have any units that differ in dimensionality or contain any units not in the core set.

Even then, I'm not sure e'd be able to handle building dimensionality on the fly, maybe for the simple units like Dimensionality[Length] * Dimensionality[Length] = Dimensionality[Length, Length], but I can't see how we'd differentiate Dimensionality[Length, Time] * Dimensionality[Length] between Dimensionality[Length, Time, Length] and Dimensionality[Length, Length, Time].

I'd be curious to know if people think how important it is for various things to happen automatically, but given the difficulties I can't imagine that this would support official typing anytime soon.

@mike-stack
Copy link

I'm using type hinting and would love to have it. Even a generic Quantity would give me the ability to remove the --ignore-missing-imports flag.

@huonw
Copy link

huonw commented Dec 17, 2020

One possibility here might be a mypy plugin: https://mypy.readthedocs.io/en/stable/extending_mypy.html#configuring-mypy-to-use-plugins . Hypothetically, a plugin might be able to work with UnitsContainers/dicts with real code, and thus do the required computations behind the scenes for accurate equalities. I don't know if this approach would be able to extend to generic functions that don't have concrete units, like (hypothetically):

A = TypeVar("A")
B = TypeVar("B")
def mul_any(a: Quantity[A], b: Quantity[B]) -> Quantity[A * B]:
    return a * b

For instance, other mypy plugins:

(edit: ah, I see #1166 discusses a syntax too.)

@claudiofinizio
Copy link

Hello,
I am writing a python package for designing rural water supplies and I made extensive use of both pint and mypy. I would therefore be glad to contribute exposing Quantity to mypy.

My objective would be to write something like:

def my_function(l: Quantity[ureg.meter]) -> Quantity[ureg.second]

as was suggested by @likewei92 and I read with interest the other posts too.

For reference, a page about my work is at ruralwater and please feel free to contact me.

@hgrecco
Copy link
Owner

hgrecco commented Dec 28, 2020

This is something on the roadmap @claudiofinizio it would be great if you can contribute. I am closing this to move the discussion to #1166

@hgrecco hgrecco closed this as completed Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants