Skip to content

fix plt.show doesn't warn if a non-GUI backend #10685

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 146 commits into from
Apr 1, 2018

Conversation

zhoubecky
Copy link
Contributor

PR Summary

fix plt.show doesn't warn if a non-GUI backend is being used #7908
will add command and unit tests later

anntzer and others added 5 commits October 25, 2017 02:01
We already have runtime tests for LaTeX and associated dependencies; the
check in setup.py is not used for anything.
Previously, changes to the text.latex.unicode or text.latex.preview
rcParams (which do change the autogenerated preamble) would not
invalidate the tex cache.
1) math.ceil() returns an int on Py3.
2) Window.SetInitialSize and Window.IsShownOnScreen are documented in
   the wx API; it seems overkill to try to workaround their possible
   absence (that code was present from the very beginning of the git
   history).
3) the macros attribute is unused since 7499f7c (2004).
Fix some doc links

Put back AGG OO example

Remove old example
@jklymak jklymak changed the title fix #7908 fix plt.show doesn't warn if a non-GUI backend Mar 5, 2018
@jklymak
Copy link
Member

jklymak commented Mar 5, 2018

Thanks for the PR! I just editied the title to be more descriptive and to exclude the issue number (though definitely include the issue # in the PR summary!)

import warnings
warnings.warn(
"matplotlib is currently using a non-GUI backend, "
"so cannot show the figure")
Copy link
Member

Choose a reason for hiding this comment

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

Might be nice to say which backend is being used

Copy link
Member

Choose a reason for hiding this comment

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

Also, what's the policy now for warnings versus logging warnings?

Copy link
Member

@timhoffm timhoffm Mar 5, 2018

Choose a reason for hiding this comment

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

According to the logging howto

warnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning

logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted

The former is the case here. One should not try to call show (or expect that it does something) on a non-GUI backend. So, warnings.warn is correct.

@@ -175,20 +175,28 @@ def draw_if_interactive(cls):
cls.trigger_manager_draw(manager)

@classmethod
def show(cls, block=None):
def show(cls, block=None, warn=True):
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need the warn parameter. Client applications can use the filter mechanism of the warnings module, if they really want to suppress the warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll remove this parameter. Thanks.

@tacaswell tacaswell added this to the v3.0 milestone Mar 5, 2018
@zhoubecky
Copy link
Contributor Author

Hi @jklymak , I have removed the warning parameter and added a test. Is there anything else I should do to make this better?


def test_NonGui_warning():
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Copy link
Member

Choose a reason for hiding this comment

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

Duplicate import.

matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('myfig')
Copy link
Member

Choose a reason for hiding this comment

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

Not needed?

@@ -179,3 +179,22 @@ def psfont(*args, **kwargs):
ax.text(0.5, 0.5, 'hello')
with tempfile.TemporaryFile() as tmpfile, pytest.raises(ValueError):
fig.savefig(tmpfile, format='svg')


def test_NonGui_warning():
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused; why does this test belong in the SVG backend test file? (The name should also not be CamelCase.)

import warnings
warnings.warn(
"matplotlib is currently using a non-GUI backend, "
"so cannot show the figure")
Copy link
Member

Choose a reason for hiding this comment

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

It looks like you missed @WeatherGod's suggestion:

Might be nice to say which backend is being used



def test_NonGui_warning():
matplotlib.use('Agg')
Copy link
Member

Choose a reason for hiding this comment

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

To force an alternate backend, you can mark the function with a decorator: @pytest.mark.backend('Agg')

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

modulo the docstring typo

warnings.warn(
"matplotlib is currently using a non-GUI backend, "
"so cannot show the figure")
('matplotlib is currently using %s, which is a ' +
Copy link
Member

Choose a reason for hiding this comment

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

Can you fix the docstring typo while you are working on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, I don't find the typo. Which line is it?

Copy link
Member

Choose a reason for hiding this comment

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

"warm" should be "warn"... Unless my screen is blurry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes... I think so. It is warn. :)

Copy link
Member

Choose a reason for hiding this comment

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

Line 434/435

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh I see it. Will fix. Thank you

jklymak and others added 23 commits March 29, 2018 09:18
Invalidate texmanager cache when any text.latex.* rc changes.
Move some examples out of examples/api
Remove the 'hold' kwarg from codebase
The int version of the buffer size was not updated when the buffer was
resized. It's there to prevent a signed/unsigned comparison warning, but
it's simpler just to cast the other side of the comparison. There's no
problem with the signed-to-unsigned cast since we already know that the
result is positive due to the previous check.

Fixes matplotlib#10889.
FIX: axes limits reverting to automatic when sharing
Fix overflow when resizing path-to-string buffer.
Note that plt.spectral already raises an exception as of 2.2 anyways
(the underlying colormap has already been removed).
API: Do not suggest a unique filename in save dialogue
DOC: Update definition of area in scatter examples
…b#10914)

API: set aspect in Axes.pie to be 'equal'

* Changed pie charts default shape to circle (instead of oval), and added tests

* Updated comments for default circular pie chart

* fix pep8

* removed unneccessary test case and updated docstring

* added what's new entry and edited docstring for pie

* fix pep8

* fixed typo
Use function signatures in boilerplate.py.
Some more removals of deprecated APIs.
Replace "matplotlibrc" by "rcParams" in the docs where applicable.
…arkers

FIX: Postscript allow empty markers
Declare global variables that are created elsewhere
@jkseppan
Copy link
Member

jkseppan commented Apr 1, 2018

I merged master into this branch to address the merge conflict, and now Github is showing this as a really huge patch (+3,415 −5,454). But on the command line this still shows up as the same change as before. I wonder if Github is confused or if I am mistaken...

@dstansby
Copy link
Member

dstansby commented Apr 1, 2018

Rebasing on to master should fix things I think.

@jkseppan jkseppan merged commit a2a145f into matplotlib:master Apr 1, 2018
jkseppan added a commit that referenced this pull request Apr 1, 2018
fix plt.show doesn't warn if a non-GUI backend
@jkseppan
Copy link
Member

jkseppan commented Apr 1, 2018

I think the Github diff machinery just has some limits for how complicated commit graphs it can handle. There were some old PRs (at least one from October 2017) merged recently, so if you look at git log --graph the history is a bit messy. The diffs look fine in the CLI.

@zhoubecky Thank you for your contribution!

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.