Skip to content

attempting to compare a cf field with None returns an error #281

@bnlawrence

Description

@bnlawrence

I have some code which has a comparator that looks like

x = None
loop over stuff
    if x == None:
       # create a cf field called x
   use x in each instance of the loop thereafter.

(I actually do this with if (x,y) == (None,None) but I think it's the simpler problem. My error message is this:

Traceback (most recent call last):
  File "/home/users/lawrence/GitRepos/cmip-play/regrid.py", line 71, in <module>
    if (land, sea) ==  (None, None):
  File "/home/users/lawrence/miniconda3v/envs/py39n/lib/python3.9/site-packages/cf/mixin/propertiesdata.py", line 342, in __eq__
    return self._binary_operation(y, "__eq__")
  File "/home/users/lawrence/miniconda3v/envs/py39n/lib/python3.9/site-packages/cf/field.py", line 1918, in _binary_operation
    if other.ndim > 0:
  File "/home/users/lawrence/miniconda3v/envs/py39n/lib/python3.9/site-packages/cf/data/data.py", line 7679, in ndim
    return self._ndim
  File "/home/users/lawrence/miniconda3v/envs/py39n/lib/python3.9/site-packages/cf/data/data.py", line 7256, in _ndim
    return self._custom["_ndim"]
KeyError: '_ndim'

It looks like the eq method needs to start by seeing if the comparator is of the same class before testing attributes.

I would expect this to work. Clearly it's easy to work around, but ...

cf.environment(paths=False)
Platform: Linux-3.10.0-1160.42.2.el7.x86_64-x86_64-with-glibc2.17
HDF5 library: 1.10.6
netcdf library: 4.7.4
udunits2 library: /home/users/lawrence/miniconda3v/envs/py39n/lib/libudunits2.so.0
Python: 3.9.7
netCDF4: 1.5.6
cftime: 1.5.1.1
numpy: 1.21.2
psutil: 5.8.0
scipy: 1.5.3
matplotlib: 3.4.3
ESMF: 8.0.1
cfdm: 1.9.0.1
cfunits: 3.3.4
cfplot: 3.1.17
cf: 3.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions