Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

rcParams.keys() is not Python 3 compatible #1721

Merged
merged 1 commit into from

3 participants

@tukss

When I run rcParams.keys() in Python 3 I get the following exception:

AttributeError: 'dict_keys' object has no attribute 'sort'

This is due to the fact that the keys method does not return a list but a dict_keys object, which has no sort method.

To retain the behavior from Python 2 I suggest the following patch:

--- a/lib/matplotlib/__init__.py
+++ b/lib/matplotlib/__init__.py
@@ -733,7 +733,7 @@ See rcParams.keys() for a list of valid parameters.' % (key,))
         """
         Return sorted list of keys.
         """
-        k = dict.keys(self)
+        k = list(dict.keys(self))
         k.sort()
         return k

This works fine in Python 2 and 3.

For completeness, I used Python 3.2.3 and matplotlib 1.2.0 from Debian unstable/experimental.

Thanks for the great work.

@mdboom
Owner

The transformation you quote should already have been performed by 2to3. Are you perhaps importing matplotlib from the source tree, or from a 2.x install?

@tukss

I agree that 2to3 should do this but it doesn't. You can test that easily by running 2to3 on __init__.py.
The reason is probably that we directly call the method of the dict class instead of the keys method of a dict instance. In the latter case 2to3 would add list() as expected.

So this seems to be a deficiency in 2to3 rather than matplotlib. I can report this to the Python bug tracker but I am not sure there will be a fix for that.

For the time being I guess we need a manual fix. What is your opinion?

@dmcdougall
Collaborator

There are no Travis failures indicating errors of this kind, so I am curious as to why you get this error. How does the error arise? Do you get it right after import matplotlib?

@dmcdougall
Collaborator

Addendum: AFAIK, Travis runs 2to3 on the whole source tree.

@tukss

Generally, matplotlib in Python 3 works fine. I got this error while trying to modify rcParams from ipython (If you just type rcParams to see all parameters, you get the error). It's clearly a minor issue that can easily go unnoticed. I guess none of the tests try the keys() method of rcParams.

@dmcdougall
Collaborator

Has your ipython been built with python 3?

@tukss

Yes, it has. This issue does not have anything to do with ipython, I just happened to notice it there first.

Steps to reproduce this with plain python3:

>>> import matplotlib
>>> matplotlib.rcParams.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 726, in keys
    k.sort()
AttributeError: 'dict_keys' object has no attribute 'sort'
@mdboom
Owner

Interesting -- I see this now -- 2to3 is not covering this as I expected.

2to3 does do this:

 x = {}
-for k in x.keys():
+for k in list(x.keys()):
     pass

But leaves this alone (perhaps because there is an argument to keys here).

class Foo(dict):
    def keys(self):
        k = dict.keys(self)
        k.sort()
        return k

I'll go ahead and attach a commit with the change you recommended.

@mdboom mdboom merged commit 02832c9 into matplotlib:v1.2.x

1 check passed

Details default The Travis build passed
@juliantaylor juliantaylor referenced this pull request in ipython/ipython
Closed

f.__globals__ causes an error in Python 3.3 #3027

@mdboom mdboom deleted the mdboom:issue_1721 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 30, 2013
  1. @mdboom
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/matplotlib/__init__.py
View
2  lib/matplotlib/__init__.py
@@ -733,7 +733,7 @@ def keys(self):
"""
Return sorted list of keys.
"""
- k = dict.keys(self)
+ k = list(dict.keys(self))
k.sort()
return k
Something went wrong with that request. Please try again.