-
-
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
mypy does not understand output of binary operations #7780
Comments
That's interesting. But not sure if this solves the problem, I have not tested it yet! In a mini test example this worked for me: class A:
i: int
def __init__(self, i: int) -> None:
self.i = i
def __add__(self, other: A | int) -> A:
if isinstance(other, A):
return A(self.i + other.i)
if isinstance(other, int):
return A(self.i + other)
return NotImplemented
def __radd__(self, other: A | int) -> A:
return self + other
def __str__(self):
return f"A({self.i})"
class B:
ii: dict[str, int]
def __init__(self, ii: dict[str, int]) -> None:
self.ii = ii
def __add__(self, other: A | B | int) -> B:
if isinstance(other, B):
return B(
{k: i + other.ii.get(k, 0) for k, i in self.ii.items()}
| {k: o for k, o in other.ii.items() if k not in self.ii}
)
if isinstance(other, A):
return B({k: i + other.i for k, i in self.ii.items()})
if isinstance(other, int):
return B({k: i + other for k, i in self.ii.items()})
return NotImplemented
def __radd__(self, other: A | B | int) -> B:
return self + other
def __str__(self):
return f"B({self.ii})"
a = A(1)
b = B({"a": 5})
print(a + 1)
print(1 + a)
print(a + a)
print(b + 5)
print(5 + b)
print(b + b)
print(a + b)
print(b + a)
reveal_type(a + 1)
reveal_type(1 + a)
reveal_type(a + a)
reveal_type(b + 5)
reveal_type(5 + b)
reveal_type(b + b)
reveal_type(a + b)
reveal_type(b + a) Note how |
I have entered quite the rabbit hole here. Somehow the operators used in xarray are not doing what the python standard dictates: import xarray as xr
xr.DataArray([1, 2, 3]) + {"asd"} raises:
but it should be "TypeError: unsupported operand type(s) for +: 'xarray.core.DataArray' and 'set'" It seems that we have done this only for xarray objects but not all other types. Back to static typing: |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Related: [1, 2 ,3] == DataArray([1, 2, 3], dims=["t"]) will be inferred to bool. Same for anything basically, including np.ndarray. Caused by python/typeshed#8217 / https://discuss.python.org/t/make-type-hints-for-eq-of-primitives-less-strict/34240 |
Is there anything we can do here or should we close? |
Closing as upstream issue |
What happened?
When doing operations on numpy arrays and xarray variables mypy does not understand that the output is always a xarray variable regardless of the order. See example.
What did you expect to happen?
mypy to pass for the example code.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
Seen in #7741
Environment
xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.9.16 (main, Mar 8 2023, 10:39:24) [MSC v.1916 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
libhdf5: 1.10.6
libnetcdf: None
xarray: 2023.4.2
pandas: 2.0.0
numpy: 1.23.5
scipy: 1.10.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.4.0
distributed: 2023.4.0
matplotlib: 3.5.3
cartopy: None
seaborn: 0.12.2
numbagg: None
fsspec: 2023.4.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 67.7.1
pip: 23.1.1
conda: 23.3.1
pytest: 7.3.1
mypy: 1.2.0
IPython: 8.12.0
sphinx: 6.1.3
The text was updated successfully, but these errors were encountered: