iris.coords.Coord() gives me a TypeError

Created on 28 Jan 2021  路  8Comments  路  Source: SciTools/iris

馃摪 Custom Issue


Hey guys, me again :grin: Silly question - I have this snippet:

import iris
print(iris.__version__)
import numpy as np

lat_coord = iris.coords.Coord(
    np.linspace(-90.0, 90.0, 100), var_name='lat',
    standard_name='latitude', long_name='latitude', units='degrees')

and in iris=3.0.1 gives me this malarkey:

/home/valeriu/miniconda3/envs/iris31/lib/python3.9/site-packages/iris/config.py:139: UserWarning: Ignoring config item 'Resources':'test_data_dir' (section:option) as '/home/h05/itwl/projects/git/iris-test-data/test_data' is not a valid directory path.
  warnings.warn(msg.format(section, option, c_path))
3.0.1
Traceback (most recent call last):
  File "/home/valeriu/ESMValTool/testcoord.py", line 5, in <module>
    lat_coord = iris.coords.Coord(
TypeError: Can't instantiate abstract class Coord with abstract method __init__

and in iris=2.4.0 it runs a-ok, with no warnings. First off, I don't understand the warning, it seems to be pointing to a location that's got nothing to do with me :grin: Second, why is it not constructing my coordinate? Looking at the latest documentation it seems support should be as previous version? Cheers muchly in advance, as usual, for great support :beer:

Good First Issue Issue Documentation

All 8 comments

@valeriupredoi This is now the correct behavour.

The iris.coords.Coord is a base class and has now been made abstract i.e., you cannot create an instance of it, because you shouldn't.

Prior to iris 3.0.0 that wasn't the case. Essentially up to iris 2.4.0 we were allowing you to do the wrong thing. We've disallowed that now, for very good reasons.

You can only create a iris.coords.DimCoord or a iris.coords.AuxCoord. That's is.

@valeriupredoi Does this help?

So either do:

lat_coord = iris.coords.DimCoord(
    np.linspace(-90.0, 90.0, 100), var_name='lat',
    standard_name='latitude', long_name='latitude', units='degrees')

or:

lat_coord = iris.coords.AuxCoord(
    np.linspace(-90.0, 90.0, 100), var_name='lat',
    standard_name='latitude', long_name='latitude', units='degrees')

terrific! Cheers muchly @bjlittle - I would encourage you to either remove it from the latest API documentation or mention this explicitily there, I looked for this and the first point of entry was the API documentation :+1:

@valeriupredoi Will do - anything to stop confusion :+1:

I'll keep this issue open until it's followed up with a suitable documentation change to clarify the abstract base class nature of iris.coords.Coord since iris 3.0.0

great! this is a pretty well-used function and changing its behaviour should have a few bright neon lights attached to it - at least for people like me that always forget the API and consult the manual oftentimes :grin:

@valeriupredoi This issue has been closed by #3971.

The iris.coords.Coord doc-string has been updated to provide clarification that it is an abstract base class, and it is not possible to create an instance of it 馃憤

cheers @bjlittle :beer:

Was this page helpful?
0 / 5 - 0 ratings