Skip to content

Support PathLike inputs. #10231

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

Merged
merged 1 commit into from
Jan 21, 2018
Merged

Support PathLike inputs. #10231

merged 1 commit into from
Jan 21, 2018

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jan 11, 2018

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
Copy link
Member

@efiring efiring left a comment

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
Copy link
Contributor Author

anntzer commented Jan 11, 2018

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
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

fixed

if (format == 'png'
or (format is None
and isinstance(fname, six.string_types)
and fname.lower().endswith('.png'))):
Copy link
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
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
Copy link

skotaro commented Jul 27, 2018

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
Copy link
Contributor Author

anntzer commented Jul 27, 2018

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