-
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
pint typing #1259
pint typing #1259
Conversation
f388c7a
to
b6cad1d
Compare
b08333a
to
abc29be
Compare
PR is almost ready. from pint import UnitRegistry
import numpy as np
ureg = UnitRegistry()
float_quantity = ureg.Quantity(150.0, "m**2")
int_quantity = ureg.Quantity(150, "m**2")
array_quantity = ureg.Quantity(np.array([1, 2, 3, 4]), "m**2")
list_quantity = ureg.Quantity((1, 2, 3, 4), "m**2")
str_quantity = ureg.Quantity("50*2 m")
sequence = [ureg.Quantity(2, "m"), ureg.Quantity(3, "m")]
array_quantity_2 = ureg.Quantity.from_list(sequence)
reveal_type(float_quantity)
reveal_type(int_quantity)
reveal_type(float_quantity.m)
reveal_type(float_quantity.to("km**2"))
reveal_type(array_quantity)
reveal_type(array_quantity_2)
reveal_type(str_quantity)
reveal_type(list_quantity) With mypy :
|
If some people can test this in their IDE & with mypy. I'd be grateful. I had trouble working with dynamically generated classes (like quantity is), it seems to work with another TypeVar in the overloaded For further steps I'll file an issue where we can discuss further steps.
@hgrecco as I said if type hints is becoming a burden in the code, we can extract them to stub files (*.pyi) if needed |
Cool. What do you think about adding |
right, but that's not what |
I agree with @keewis , we can drop support for python 3.6 according to NEP 29. According to pypistats.org, python 3.6 represents roughly 10% of the download. We can pin an issue announcing last compatible version & that version 0.18 will drop python 3.6 support. See if this raises any flags. |
46120b9
to
e4711ea
Compare
3ad490a
to
727fc87
Compare
I'm fixing CI with #1356, after that I think we can merge this. Type hints will be experimental, I made typing module private. |
- Quantity as Generic class - Add overloaded signature for __new__ Quantity - Add typing module as private - Add py.typed for PEP561 supports - Add overloaded signature for __new__ Quantity - Quantity as Generic class - Add type hints throughout the project - Add py.typed in package data in setup.cfg - Add type hints for decorators - Add type hints for public API of registry.py - Add type hints for units.py
@hgrecco It is ready for review |
This is awesome. Excelent job. My only question is the purpose of this if TYPE_CHECKING:
from .quantity import Quantity
from .registry import UnitRegistry
from .util import UnitsContainer in files like |
It is used to avoid circular imports. |
I get tge circular imports, and I get that these imports are only used when typechecking. I was wondering if there was no other place to put it (i.e. a typing imports file which make somethings available). In any case, I really like this and I will be happy to merge it |
Hi, sorry for what might appear as a very dumb question, but I couldn't find any doc on how to use this. |
Following the discussion started here #1166
pre-commit run --all-files
with no errorsThis PR introduces Type annotation support for Quantity.
Quantity is a Generic (https://docs.python.org/3/library/typing.html#user-defined-generic-types).
Magnitude type changes based on input type when constructing Quantity.
This will ease type checking for libraries & application using types and developers who wish to statically type check their code.
If readability is becoming an issue we can export type annotations to stub files.
After the merge of this PR we should comply to PEP561.
We can also introduce Annotated Quantity types with dimensionality & unit.
After running mypy, we will be able to track some bugs since we have a lot of type checking with
isinstance
, switches etc.