X Tutup
Skip to content

MAINT: mappingview check for Python 3.4#8166

Merged
dstansby merged 1 commit intomatplotlib:masterfrom
matthew-brett:master
Mar 1, 2017
Merged

MAINT: mappingview check for Python 3.4#8166
dstansby merged 1 commit intomatplotlib:masterfrom
matthew-brett:master

Conversation

@matthew-brett
Copy link
Copy Markdown
Contributor

Python 3.4 does not autoamatically import module abc into
collections, causing an error when checking for
collections.abc.MappingView. Do more explicit import of MappingView
to work round this difference.

See: https://travis-ci.org/MacPython/matplotlib-wheels/jobs/205713297#L582 for
error on Python 3.4.

try:
from collections.abc import MappingView
except ImportError:
MappingView = None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the except block only applies to python 2.7, is that right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.7 has mappingview (https://docs.python.org/2.7/library/collections.html#collections.MappingView) so I don't know when it can ever be triggered.

(My guess is that below, the test on whether we're running Py3 should just be removed -- we may as well sanitize the case where someone passes dic.itervalues() on Py2 too (well I always thought the whole idea was misguided but let's at least be consistent across Python versions.))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So - collections.MappingView is present in all Python versions we support, but collections.abc.MappingView is present only for the Python 3 versions:

Changed in version 3.3: Moved Collections Abstract Base Classes to the collections.abc module. For backwards compatibility, they continue to be visible in this module as well.

https://docs.python.org/3.5/library/collections.html#module-collections

We could use collections.MappingView directly, they haven't been deprecated, I guess.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for using collections.MappingView directly then as I think the if MappingView = None is clunky

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@phobson phobson changed the title MAINT: mappingview check for Python 3.4 [MRG+1] MAINT: mappingview check for Python 3.4 Mar 1, 2017
@matthew-brett
Copy link
Copy Markdown
Contributor Author

matthew-brett commented Mar 1, 2017 via email

@phobson
Copy link
Copy Markdown
Member

phobson commented Mar 1, 2017

@matthew-brett totally. I just wanted to make sure I understood it properly since I don't have a py2.7 installation handy.

try:
from collections.abc import MappingView
except ImportError:
MappingView = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.7 has mappingview (https://docs.python.org/2.7/library/collections.html#collections.MappingView) so I don't know when it can ever be triggered.

(My guess is that below, the test on whether we're running Py3 should just be removed -- we may as well sanitize the case where someone passes dic.itervalues() on Py2 too (well I always thought the whole idea was misguided but let's at least be consistent across Python versions.))

@anntzer
Copy link
Copy Markdown
Contributor

anntzer commented Mar 1, 2017

Side question: Codecov says this line is covered, so how was that never caught before in the CI? (Unfortunately Travis doesn't want to load for me right now.)

@matthew-brett
Copy link
Copy Markdown
Contributor Author

I'm just guessing, but I suspect that some versions of Python 3.4 do an automatic import of abc in collections, and some do not. Here's an example that does not, on macOS:

$ python3.4
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> collections.abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'abc'

That's just a guess, because the behavior above is so for all the Python3.4s I could find.

Python 3.4 does not automatically import module `abc` into
`collections`, causing an error when checking for
`collections.abc.MappingView`.  Use `collections.MappingView` to work
round this difference.

At some point Python may deprecate `collections.MappingView` in favor of
`collections.abc.MappingView` but we can fix that when it arises.
@anntzer
Copy link
Copy Markdown
Contributor

anntzer commented Mar 1, 2017

LGTM (conditional on tests passing).

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Mar 1, 2017
@dstansby dstansby changed the title [MRG+1] MAINT: mappingview check for Python 3.4 MAINT: mappingview check for Python 3.4 Mar 1, 2017
@dstansby dstansby merged commit c2f675d into matplotlib:master Mar 1, 2017
@dstansby
Copy link
Copy Markdown
Member

dstansby commented Mar 1, 2017

It looks like sanitize_sequence isn't present on 2.0.x, so no need for a backport.

@dstansby dstansby modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Mar 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

X Tutup