matplotlib/matplotlib — pull request #3196 by astrofrog (2014-11-26 20:48:51+00:00)
Title: Issue with iterability of axes arguments

I have been trying to use the ``_as_mpl_axes`` method to create a custom projection. I am returning an Axes subclass and a dictionary of arguments to pass to the class. One of the arguments is an item of type ``astropy.wcs.WCS``. In ``figure.py``, the following code:

```
def fixitems(items):
#items may have arrays and lists in them, so convert them
# to tuples for the key
ret = []
for k, v in items:
if iterable(v):
v = tuple(v)
ret.append((k, v))
return tuple(ret)
```

results in an error, specifically when doing ``tuple(v)``. The issue is that ``iterable(wcs)`` returns ``True`` but ``tuple(wcs)`` crashes. As a general issue, ``iter(v)`` working does not guarantee that ``tuple(v)`` will work. One solution is to simply do:

```
if iterable(v):
try:
v = tuple(v)
except:
pass
```

I'm not sure if there is a better way to deal with this kind of issue. Just for the record, ``tuple(wcs)`` gives:

```
ValueError: Cannot downsample a WCS with indexing. Use wcs.sub or wcs.dropaxis if you want to remove axes.
```

To some extent, the issue here is that while ``WCS`` has a ``__getitem__`` method, it is not strictly iterable. So maybe the issue is actually that ``iterable`` should not rely just on ``iter``. Maybe the ``iterable`` function:

```

def iterable(obj):
'return true if *obj* is iterable'
try:
iter(obj)
except TypeError:
return False
return True
```

in ``cbook.py`` should be using:

```
next(iter(wcs))
```

? (which does raise an error in my case)

cc @mdboom, since it involves the WCS class.