-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update signature for _arrayfunction.__array__ #9237
Conversation
Attempting to fix these:
|
I'm struggling to understand what mypy complains about here.
Here's a simplified example: def test_mypy_typing_fail() -> None:
from typing import Generic, TypeVar
from xarray.namedarray._typing import _Dims
_ST = TypeVar("_ST", bound=Any)
_DT = TypeVar("_DT", bound=Any)
class TestArray(Generic[_ST, _DT]):
__slots__ = ("_data", "_dims", "_attrs")
_data: duckarray[_ST, _DT]
_dims: _Dims
_attrs: dict[Any, Any] | None
def __init__(
self,
dims: _Dims,
data: duckarray[_ST, _DT],
attrs: dict[Any, Any] = {},
):
self._data = data
self._dims = dims
self._attrs = attrs
data: np.ndarray[Any, np.dtype[np.int64]] = np.array([1, 2, 3], dtype=np.int64)
# ta: TestArray[Any, np.dtype[np.int64]] # mypy fails if commented out, why?
ta = TestArray(("time",), data)
# mypy:
# error: Need type annotation for "ta" [var-annotated]
# error: Argument 2 to "TestArray" has incompatible type "ndarray[Any, dtype[signedinteger[_64Bit]]]"; expected "_arrayfunction[Never, Never] | _arrayapi[Never, Never]" [arg-type]
# pyright - pass:
# reveal_type(ta) # information: Type of "ta" is "TestArray[_ShapeType_co@_arrayapi, dtype[signedinteger[_64Bit]]]" |
Thats weird... I cannot reproduce a minimal example that does not use Also
is not very helpful... |
Interesting, maybe due to the Union? This simpler example does the same at least: int_and_float = Union[int, float]
reveal_type(int_and_float) # note: Revealed type is "typing._SpecialForm" Compare pyright: int_and_float = Union[int, float]
reveal_type(int_and_float) # information: Type of "int_and_float" is "type[int] | type[float]" |
An interesting note. The duckarray protocol has to use all the typevars to be able to properly inherit values without explicit definition. Since numpy doesn't do that yet with _ShapeType I haven't pushed it either. This example works but commenting out shape from the Protocol will start forcing explicit definitions: from typing import Any, Generic, Protocol, TypeVar
_ST = TypeVar("_ST", bound=Any)
_ST_co = TypeVar("_ST_co", bound=Any, covariant=True)
_DT = TypeVar("_DT", bound=Any)
_DT_co = TypeVar("_DT_co", bound=Any, covariant=True)
class ArrayProtocol(Protocol[_ST_co, _DT_co]):
@property
def dtype(self) -> _DT_co: ...
@property
def shape(self) -> _ST_co: ... # Commenting out shape will start to require explicit definition
class Array(Generic[_ST, _DT]):
def __init__(self, shape: _ST, dtype: _DT) -> None:
self._shape = shape
self._dtype = dtype
@property
def shape(self) -> _ST:
return self._shape
@property
def dtype(self) -> _DT:
return self._dtype
def count(self, value: Any, /) -> int:
return 2
def test_pass_typevars(a: ArrayProtocol[_ST, _DT]) -> ArrayProtocol[_ST, _DT]:
return a
arr = Array(shape=(3,), dtype=int)
reveal_type(arr) # information: Type of "arr" is "Array[tuple[Literal[3]], type[int]]"
array = test_pass_typevars(a=arr)
reveal_type(array) # information: Type of "array" is "ArrayProtocol[tuple[Literal[3]], type[int]]" |
for more information, see https://pre-commit.ci
That is expected, because the shape TypeVar is unbound then. Maybe you could add a definition/overload of |
Wow, what a journey. Turns out it was Debug method was to turn off methods in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for figuring this out!
Some verbosity of mypy on why it decides stuff would be useful ...
* main: add backend intro and how-to diagram (#9175) Fix copybutton for multi line examples in double digit ipython cells (#9264) Update signature for _arrayfunction.__array__ (#9237) Add encode_cf_datetime benchmark (#9262) groupby, resample: Deprecate some positional args (#9236) Delete ``base`` and ``loffset`` parameters to resample (#9233) Update dropna docstring (#9257) Grouper, Resampler as public api (#8840) Fix mypy on main (#9252) fix fallback isdtype method (#9250) Enable pandas type checking (#9213) Per-variable specification of boolean parameters in open_dataset (#9218) test push Added a space to the documentation (#9247) Fix typing for test_plot.py (#9234)
* main: (54 commits) Adding `open_datatree` backend-specific keyword arguments (#9199) [pre-commit.ci] pre-commit autoupdate (#9202) Restore ability to specify _FillValue as Python native integers (#9258) add backend intro and how-to diagram (#9175) Fix copybutton for multi line examples in double digit ipython cells (#9264) Update signature for _arrayfunction.__array__ (#9237) Add encode_cf_datetime benchmark (#9262) groupby, resample: Deprecate some positional args (#9236) Delete ``base`` and ``loffset`` parameters to resample (#9233) Update dropna docstring (#9257) Grouper, Resampler as public api (#8840) Fix mypy on main (#9252) fix fallback isdtype method (#9250) Enable pandas type checking (#9213) Per-variable specification of boolean parameters in open_dataset (#9218) test push Added a space to the documentation (#9247) Fix typing for test_plot.py (#9234) Allow mypy to run in vscode (#9239) Revert "Test main push" ...
…monotonic-variable * main: (995 commits) Adding `open_datatree` backend-specific keyword arguments (pydata#9199) [pre-commit.ci] pre-commit autoupdate (pydata#9202) Restore ability to specify _FillValue as Python native integers (pydata#9258) add backend intro and how-to diagram (pydata#9175) Fix copybutton for multi line examples in double digit ipython cells (pydata#9264) Update signature for _arrayfunction.__array__ (pydata#9237) Add encode_cf_datetime benchmark (pydata#9262) groupby, resample: Deprecate some positional args (pydata#9236) Delete ``base`` and ``loffset`` parameters to resample (pydata#9233) Update dropna docstring (pydata#9257) Grouper, Resampler as public api (pydata#8840) Fix mypy on main (pydata#9252) fix fallback isdtype method (pydata#9250) Enable pandas type checking (pydata#9213) Per-variable specification of boolean parameters in open_dataset (pydata#9218) test push Added a space to the documentation (pydata#9247) Fix typing for test_plot.py (pydata#9234) Allow mypy to run in vscode (pydata#9239) Revert "Test main push" ...
I believe numpy updated the signature of
__array__
in numpy2.It currently looks like this now: https://github.com/numpy/numpy/blob/de1ca2676e0f1db5a1b95e1d05e86ca87321cac2/numpy/__init__.pyi#L1471-L1478
xref #9231, #9252