8000 Add PEP 519 support by aragilar · Pull Request #6788 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add PEP 519 support #6788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tests for PEP 519/pathlib, these should fail
  • Loading branch information
aragilar committed Sep 4, 2016
commit 4e46319444e32faf637d8547a6266c7e3a4be0cd
92 changes: 92 additions & 0 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
from numpy.testing import assert_equal
from nose.tools import assert_false, assert_true
from nose.plugins.skip import SkipTest
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation
Expand Down Expand Up @@ -107,6 +108,12 @@ def test_save_animation_smoketest():
for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation, writer, extension

for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation_pep_519, writer, extension

for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation_pathlib, writer, extension


@cleanup
def check_save_animation(writer, extension='mp4'):
Expand Down Expand Up @@ -144,6 +151,91 @@ def animate(i):
pass


@cleanup
def check_save_animation_pep_519(writer, extension='mp4'):
Copy link
Member

Choose a reason for hiding this comment

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

I would avoid referencing to peps in function name. It is unclear what it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about check_save_animation_PathLike?

Copy link
Member

Choose a reason for hiding this comment

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

check_save_animation_path_like?

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

if not animation.writers.is_available(writer):
skip("writer '%s' not available on this system"
% writer)
Copy link
Member

Choose a reason for hiding this comment

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

I am, again, surprised pep8 allows that.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't for me; I don't know why this is passing either.

fig, ax = plt.subplots()
line, = ax.plot([], [])

ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)

def init():
line.set_data([], [])
return line,

def animate(i):
x = np.linspace(0, 10, 100)
y = np.sin(x + i)
line.set_data(x, y)
return line,

# Use NamedTemporaryFile: will be automatically deleted
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
F.close()
Copy link
Contributor

Choose a reason for hiding this comment

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

This works on windows to get a named temp file name...

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
try:
anim.save(FakeFSPathClass(F.name), fps=30, writer=writer, bitrate=500)
except UnicodeDecodeError:
xfail("There can be errors in the numpy import stack, "
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


@cleanup
def check_save_animation_pathlib(writer, extension='mp4'):
Copy link
Member

Choose a reason for hiding this comment

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

This test and the previous one looks like they could be refactoring to loop other Path and fspath compatible object to minimize the code duplication.

try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")

if not animation.writers.is_available(writer):
skip("writer '%s' not available on this system" % writer)
fig, ax = plt.subplots()
line, = ax.plot([], [])

ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)

def init():
line.set_data([], [])
return line,

def animate(i):
x = np.linspace(0, 10, 100)
y = np.sin(x + i)
line.set_data(x, y)
return line,

# Use NamedTemporaryFile: will be automatically deleted
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
F.close()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
try:
anim.save(Path(F.name), fps=30, writer=writer, bitrate=500)
except UnicodeDecodeError:
xfail("There can be errors in the numpy import stack, "
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


@cleanup
def test_no_length_frames():
fig, ax = plt.subplots()
Expand Down
37 changes: 37 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from matplotlib import pyplot as plt
from matplotlib.testing.decorators import (image_comparison, knownfailureif,
cleanup)
from nose.plugins.skip import SkipTest

if 'TRAVIS' not in os.environ:
@image_comparison(baseline_images=['pdf_use14corefonts'],
Expand Down Expand Up @@ -132,3 +133,39 @@ def test_grayscale_alpha():
ax.imshow(dd, interpolation='none', cmap='gray_r')
ax.set_xticks([])
ax.set_yticks([])


@cleanup
def test_pdfpages_accept_pep_519():
Copy link
Member

Choose a reason for hiding this comment

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

Those two tests look like they could be refactored into one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test_pdfpages_accept_pep_519 and test_savefig_accept_pathlib? I'm not sure it's possible to skip part of a test and have that appear in nose/pytest.

Copy link
Member

Choose a reason for hiding this comment

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

mmh… good question.
@Kojoley any idea on that?

from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
with PdfPages(FakeFSPathClass(tmpfile.name)) as pdf:
fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
pdf.savefig(fig)


@cleanup
def test_savefig_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
Copy link
Member

Choose a reason for hiding this comment

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

@Kojoley What is the correct way to handle this now?

Copy link
Member
@Kojoley Kojoley Sep 4, 2016
< F438 /h3>

Choose a reason for hiding this comment

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

It is OK to use raise SkipTest as pytest supports it, but skip (without raise, just function call) (imported from from .testing import skip) is preferable as it does not use nose when you are using pytest.

from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
with PdfPages(Path(tmpfile.name)) as pdf:
fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
pdf.savefig(fig)
33 changes: 33 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
assert_array_almost_equal)
from nose.tools import (assert_equal, assert_not_equal, raises, assert_true,
assert_raises)
from nose.plugins.skip import SkipTest
from matplotlib.testing.decorators import cleanup

import matplotlib.cbook as cbook
import matplotlib.colors as mcolors
Expand Down Expand Up @@ -523,3 +525,34 @@ def test_flatiter():

assert 0 == next(it)
assert 1 == next(it)


@cleanup
def test_to_filehandle_accept_pep_519():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

tmpfile = NamedTemporaryFile(delete=False)
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
cbook.to_filehandle(pep519_path)


@cleanup
def test_to_filehandle_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

tmpfile = NamedTemporaryFile(delete=False)
tmpfile.close()
path = Path(tmpfile.name)
cbook.to_filehandle(path)
107 changes: 107 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from six.moves import xrange

from nose.tools import assert_equal, assert_true
from nose.plugins.skip import SkipTest

from matplotlib import rcParams
from matplotlib.testing.decorators import image_comparison, cleanup
from matplotlib.axes import Axes
Expand Down Expand Up @@ -205,6 +207,111 @@ def test_figaspect():
assert h / w == 1


@cleanup
def test_savefig_accept_pep_519_png():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])

tmpfile = NamedTemporaryFile(suffix='.png')
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_png():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.png')
tmpfile.close()
path = Path(tmpfile.name)
fig.savefig(path)


@cleanup
def test_savefig_accept_pep_519_svg():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.svg')
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_svg():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.svg')
tmpfile.close()
path = Path(tmpfile.name)
fig.savefig(path)


@cleanup
def test_savefig_accept_pep_519_pdf():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_pdf():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
path = Path(tmpfile.name)
fig.savefig(path)

if __name__ == "__main__":
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
38 changes: 36 additions & 2 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os

from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest

import numpy as np

Expand Down Expand Up @@ -752,5 +753,38 @@ def test_imshow_endianess():
ax2.imshow(Z.astype('>f8'), **kwargs)


if __name__ == '__main__':
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
@cleanup
def test_imsave_accept_pep_519():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

a = np.array([[1, 2], [3, 4]])
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
plt.imsave(pep519_path, a)


@cleanup
def test_imsave_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

a = np.array([[1, 2], [3, 4]])
tmpfile = NamedTemporaryFile(suffix='.pdf')
tmpfile.close()
path = Path(tmpfile.name)
plt.imsave(path, a)


if __name__=='__main__':
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
0