Skip to content

Support PathLike inputs. - #10231

Merged
tacaswell merged 1 commit into
matplotlib:masterfrom
anntzer:pathlike
Jan 21, 2018
Merged

tacaswell merged 1 commit into
matplotlib:masterfrom
anntzer:pathlike

Conversation

@anntzer

@anntzer anntzer commented Jan 11, 2018 •

Copy link
Copy Markdown
Contributor

PR Summary

My take on PathLike support.
Alternate for #8481; marked as release critical only in the sense that one of the two PRs should go in...

Also xref #10048.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 11, 2018
@anntzer anntzer added this to the v2.2 milestone Jan 11, 2018
@anntzer
anntzer force-pushed the pathlike branch 2 times, most recently from 76a5e1c to 89a130f Compare January 11, 2018 06:02

@efiring efiring left a comment

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.

miscellaneous comment: if this is intended to support python 2.7 then I suggest changing "file" to "_file" or some such, since "file" is a builtin in python 2. It doesn't feel right to hijack its name.

@anntzer

anntzer commented Jan 11, 2018

Copy link
Copy Markdown
Contributor Author

changed file -> fh.

@anntzer
anntzer force-pushed the pathlike branch 7 times, most recently from b5e9b69 to aa4ca11 Compare January 12, 2018 22:34

The following functions now support `~.PathLike` inputs on Python 3.6+:
On Python 3.6+, `~matplotlib.pyplot.savefig`, `~matplotlib.pyplot.imsave`,
`~matplotlib.pyplot.imread`, and animation writers now accept `~os.PathLike`\s

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.

The end of this sentence is a repeat of the beginning.

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.

fixed

PathLike support
````````````````

The following functions now support `~.PathLike` inputs on Python 3.6+:

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.

Not sure you should use ~ since this is an external reference and os. is pretty short anyway.

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.

fixed

Comment thread lib/matplotlib/image.py
if (format == 'png'
or (format is None
and isinstance(fname, six.string_types)
and fname.lower().endswith('.png'))):

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.

This precedence is something changed in another PR, right?

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.

Ah, #10048, I see you linked it.

@tacaswell
tacaswell merged commit af1197d into matplotlib:master Jan 21, 2018
@anntzer
anntzer deleted the pathlike branch January 21, 2018 21:12
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
@skotaro

skotaro commented Jul 27, 2018

Copy link
Copy Markdown

Hi, I'm using 2.2.0 installed with Anaconda on Jupyter notebook.

I'm not sure whether this PR has been already implemented in my version or not, but I came across a related issue to this.
PosixPath works for png format in fig.savefig() but not for pdf and other vector formats (eps, ps, svg).
Please try a MWE below.
Strange thing is that savefig() creates a file at a right location but it's broken and cannot be opened. Quick Look (a preview feature of macOS) gives a small thumbnail while it usually shows a preview of the original size. The broken file has same file size as a png file. It might be related to backend but I'm not familiar with it.

matrix = np.random.rand(10, 10)
plt.imshow(matrix)

figdir = Path('fig_matplotlib/')
filepath = Path('fig_matplotlib/PosixPath_full.pdf')

# Both cases create a broken pdf file (or eps, ps, svg)
plt.savefig(figdir/'PosixPath.pdf')
plt.savefig(filepath)

# But no problems with PNG format 
filepath = Path('fig_matplotlib/PosixPath_full.png')
plt.savefig(figdir/'PosixPath.png')
plt.savefig(filepath)

@anntzer

anntzer commented Jul 27, 2018

Copy link
Copy Markdown
Contributor Author

fixed in 2.2.1 by #10754.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants