8000 Add pep 519 by tacaswell · Pull Request #8481 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add pep 519 #8481

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 3 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 authored and tacaswell committed Apr 14, 2017
commit e96cab44c2ba58e5f977424ee3478ded5ef2eed9
92 changes: 77 additions & 15 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,7 @@ def isAvailable(self):
]


# Smoke test for saving animations. In the future, we should probably
# design more sophisticated tests which compare resulting frames a-la
# matplotlib.testing.image_comparison
@pytest.mark.parametrize('writer, extension', WRITER_OUTPUT)
def test_save_animation_smoketest(tmpdir, writer, extension):
try:
# for ImageMagick the rcparams must be patched to account for
# 'convert' being a built in MS tool, not the imagemagick
# tool.
writer._init_from_registry()
except AttributeError:
pass
if not animation.writers.is_available(writer):
pytest.skip("writer '%s' not available on this system" % writer)
def _inner_animation(writer):
fig, ax = plt.subplots()
line, = ax.plot([], [])

Expand All @@ -162,11 +149,30 @@ def animate(i):
y = np.sin(x + i)
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
return anim, dpi, codec


# Smoke test for saving animations. In the future, we should probably
# design more sophisticated tests which compare resulting frames a-la
# matplotlib.testing.image_comparison
@pytest.mark.parametrize('writer, extension', WRITER_OUTPUT)
def test_save_animation_smoketest(tmpdir, writer, extension):
try:
# for ImageMagick the rcparams must be patched to account for
# 'convert' being a built in MS tool, not the imagemagick
# tool.
writer._init_from_registry()
except AttributeError:
pass
if not animation.writers.is_available(writer):
pytest.skip("writer '%s' not available on this system" % writer)

anim, dpi, codec = _inner_animation(writer)

# Use temporary directory for the file-based writers, which produce a file
# per frame with known names.
with tmpdir.as_cwd():
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
try:
anim.save('movie.' + extension, fps=30, writer=writer, bitrate=500,
dpi=dpi, codec=codec)
Expand All @@ -175,6 +181,62 @@ def animate(i):
"see issues #1891 and #2679")


@pytest.mark.parametrize('writer, extension', WRITER_OUTPUT)
def test_save_animation_pep_519(writer, extension='mp4'):
class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path
if not animation.writers.is_available(writer):
pytest.skip("writer '%s' not available on this system" % writer)

# Use NamedTemporaryFile: will be automatically deleted
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
F.close()
anim, dpi, codec = _inner_animation(writer)

try:
anim.save(FakeFSPathClass(F.name), fps=30, writer=writer, bitrate=500,
dpi=dpi, codec=codec)
except UnicodeDecodeError:
pytest.xfail("There can be errors in the numpy import stack, "
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


@pytest.mark.parametrize('writer, extension', WRITER_OUTPUT)
def test_save_animation_pathlib(writer, extension='mp4'):
try:
from pathlib import Path
except ImportError:
raise pytest.skip("pathlib not installed")
if not animation.writers.is_available(writer):
pytest.skip("writer '%s' not available on this system" % writer)

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

anim, dpi, codec = _inner_animation(writer)
try:
anim.save(Path(F.name), fps=30, writer=writer, bitrate=500,
dpi=dpi, codec=codec)
except UnicodeDecodeError:
pytest.xfail("There can be errors in the numpy import stack, "
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


def test_no_length_frames():
fig, ax = plt.subplots()
line, = ax.plot([], [])
8000 Expand Down
34 changes: 34 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,37 @@ def psfont(*args, **kwargs):
ax.text(0.5, 0.5, 'hello')
with tempfile.TemporaryFile() as tmpfile, pytest.raises(ValueError):
fig.savefig(tmpfile, format='pdf')


def test_pdfpages_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(suffix='.pdf')
tmpfile.close()
with PdfPages(FakeFSPathClass(tmpfile.name)) as pdf:
fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
pdf.savefig(fig)


def test_savefig_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise pytest.skip("pathlib not installed")
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)
29 changes: 29 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,32 @@ def test_bounded(self, string, bounded):
func_parser = cbook._StringFuncParser(string)
b = func_parser.is_bounded_0_1
assert_array_equal(b, bounded)


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)


def test_to_filehandle_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise pytest.skip("pathlib not installed")
from tempfile import NamedTemporaryFile

tmpfile = NamedTemporaryFile(delete=False)
tmpfile.close()
path = Path(tmpfile.name)
cbook.to_filehandle(path)
36 changes: 36 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,39 @@ def test_autofmt_xdate(which):
if which in ('both', 'minor'):
for label in fig.axes[0].get_xticklabels(True, 'minor'):
assert int(label.get_rotation()) == angle


@pytest.mark.parametrize('ext', ['png', 'svg', 'pdf'])
def test_savefig_accept_pep_519(ext):
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='.{}'.format(ext))
tmpfile.close()
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@pytest.mark.parametrize('ext', ['png', 'svg', 'pdf'])
def test_savefig_accept_pathlib(ext):
try:
from pathlib import Path
except ImportError:
raise pytest.skipd("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
tmpfile = NamedTemporaryFile(suffix='.{}'.format(ext))
tmpfile.close()
path = Path(tmpfile.name)
fig.savefig(path)
31 changes: 31 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,34 @@ def test_empty_imshow():
def test_imshow_float128():
fig, ax = plt.subplots()
ax.imshow(np.zeros((3, 3), dtype=np.longdouble))


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)


def test_imsave_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise pytest.skip("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)
0