Skip to content

Type checking fails for multiplication #4054

@jenssss

Description

@jenssss

MCVE Code Sample

I save the following in a file called "foo.py"

import xarray as xr

def mul(x: float, y: xr.Dataset):
    return x*y

and run

mypy foo.py

Expected Output

This should give

Success: no issues found in 1 source file

Problem Description

Instead I get the output

foo.py:4: error: Unsupported operand types for * ("float" and "Dataset")

It gives a similar error for addition.
If I reverse the order of multiplication it works as expected, namely

import xarray as xr

def mul(x: float, y: xr.Dataset):
    return y*x

gives

Success: no issues found in 1 source file

But it would be nice if it also worked when the float is first, so I don't have to change a lot existing code to get typechecking to work.

Versions

I tested on version 0.15.1 and on the current master

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.2 (default, May 7 2020, 20:00:49)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-29-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None

xarray: 0.15.2.dev61+gbd84186a
pandas: 1.0.3
numpy: 1.18.4
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 46.2.0.post20200511
pip: 20.0.2
conda: None
pytest: None
IPython: None
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions