Repository navigation
[Typing] Python type stubs for common functions of quantity and dimensionality - #206
Conversation
|
Hi Peter, many thanks for this. Running |
|
Hello Andrew, The other errors are stem mostly from type conflicts (12 errors), which may occur because I simply inferred the wrong types when defining the type-stubs. 4 errors are because of incompatible imports, which may require a separate issue / pull request to fix, because they require some code refactoring to fix. 2 errors are because imported names are redefined (also requires refactoring). A huge problem when annotating this library stems from inheritance (e.g. from the built-in quantities/quantity.pyi:69: error: Signature of "__sub__" incompatible with supertype "ndarray"
quantities/quantity.pyi:69: note: Superclass:
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[bool_]], _NestedSequence[_SupportsArray[dtype[bool_]]], bool, _NestedSequence[bool]]) -> NoReturn
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, unsignedinteger[Any]]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, unsignedinteger[Any]]]]], bool, _NestedSequence[bool]]) -> ndarray[Any, dtype[unsignedinteger[Any]]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, integer[Any]]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any]]]]], bool, int, _NestedSequence[Union[bool, int]]]) -> ndarray[Any, dtype[signedinteger[Any]]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, integer[Any], floating[Any]]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any], floating[Any]]]]], bool, int, float, _NestedSequence[Union[bool, int, float]]]) -> ndarray[Any, dtype[floating[Any]]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]]], bool, int, float, complex, _NestedSequence[Union[bool, int, float, complex]]]) -> ndarray[Any, dtype[complexfloating[Any, Any]]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, number[Any]]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, number[Any]]]]], bool, int, float, complex, _NestedSequence[Union[bool, int, float, complex]]]) -> ndarray[Any, dtype[number[Any]]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, integer[Any], timedelta64]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any], timedelta64]]]], bool, int, _NestedSequence[Union[bool, int]]]) -> ndarray[Any, dtype[timedelta64]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[Union[bool_, integer[Any], timedelta64]]], _NestedSequence[_SupportsArray[dtype[Union[bool_, integer[Any], timedelta64]]]], bool, int, _NestedSequence[Union[bool, int]]]) -> ndarray[Any, dtype[datetime64]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[datetime64]], _NestedSequence[_SupportsArray[dtype[datetime64]]]]) -> ndarray[Any, dtype[timedelta64]]
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Any) -> Any
quantities/quantity.pyi:69: note: @overload
quantities/quantity.pyi:69: note: def __sub__(self, Union[_SupportsArray[dtype[object_]], _NestedSequence[_SupportsArray[dtype[object_]]]]) -> Any
quantities/quantity.pyi:69: note: Subclass:
quantities/quantity.pyi:69: note: def __sub__(self, Any) -> QuantitySo we should discuss how we want to deal with these issues. The fastest method would be to suppress this kind of errors by adding |
|
Many thanks for this follow-up, Peter. Based on the aphorism "perfect is the enemy of good", I would support using |
|
Ok, so I didn't have time to work on this in the past few months, but I am back. I annotated some stuff to with the ignore comments and generated pyi-stubs for all units (since there were some variables which produced weird errors where mypy was unable to defer their type). The remaining (and new errors) go now much more into the depth of how the module is structured. quantities/units/volume.pyi:21: error: Name "milliliter" already defined on line 8 [no-redef]
quantities/units/pressure.pyi:38: error: Name "barad" already defined on line 37 [no-redef]
quantities/units/__init__.py:75: error: Incompatible import of "pt" (imported name has type "UnitQuantity", local name has type "UnitLength") [assignment]
quantities/typing/quantities.pyi:8: error: Type argument "Union[float, int]" of "dtype" must be a subtype of "generic" [type-var]
quantities/__init__.py:283: error: Incompatible import of "dimensionless" (imported name has type Module, local name has type "Dimensionless") [assignment]
quantities/__init__.py:287: error: Incompatible import of "dimensionless" (imported name has type Module, local name has type "Dimensionless") [assignment]
quantities/constants/mathematical.py:15: error: Argument "aliases" to "UnitConstant" has incompatible type "str"; expected "List[str]" [arg-type]
quantities/tests/test_methods.py:188: error: Name "test_nanargmax" already defined on line 163 [no-redef]Short breakdown:
One cause may be, that mypy doesn't pick up on the removal of imported constants at the end of the files where units are defined. To investigate this, I tried to get rid of the direct imports by importing the modules and using the constants only as a reference (i.e. instead of Long story short: I don't think you can do this with relative imports and have a consistent import style at the same time. Example from # file quantities/units/power.py
from . import time # (ok) imports the module quantities.units.time
from . import force # (nok) imports the constant quantities.units.acceleration.force as it is exported in units/__init__.py
import time # (nok) imports the module time from stdlib
import force # (ok) imports the module quantities.units.forceAs far as I am aware, the only way to do this consistently would be to use absolute imports. I don't know if you want to do this, but I think if we do it for the units, we should also do it for everything else, so there is a consistent style across the project. |
|
Many thanks for all your work on this @codingchipmunk |
Hello everyone,
I wrote some stub files containing type hints for some of the functions. My goals for this PR were:
Quantityobject, which in turn IS another numpy array by inheritance.Cheers,
Peter