Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: get_backend return None if no backend set #12592

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

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Oct 22, 2018

PR Summary

Closes #12362

Allows the following to run if rcParams['backend'] is not already set.

import matplotlib
print('pre-backend', matplotlib.get_backend())
if not matplotlib.get_backend() == 'WXAgg':
    matplotlib.use('WXAgg')
print('backend:', matplotlib.get_backend())

Before

pre-backend MacOSX
testit.py:4: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('WXAgg')
backend: MacOSX

After

pre-backend None
backend: WXAgg

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 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

@jklymak jklymak added this to the v3.0.x milestone Oct 22, 2018
@jklymak jklymak closed this Oct 22, 2018
@jklymak jklymak reopened this Oct 22, 2018
@jklymak
Copy link
Member Author

jklymak commented Oct 22, 2018

Umm, hmmm. Not sure why CI is not running here

@jklymak jklymak closed this Oct 22, 2018
@jklymak jklymak reopened this Oct 22, 2018
@jklymak jklymak requested a review from anntzer October 22, 2018 17:52
@dstansby
Copy link
Member

Github is having issues with hooks I think...

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

See #12362 (comment) for my preferred solution. Returning yet another fake value and adding another parameter to say "nah, I want the real value" from get_backend() seems worse. (Plus, that doesn't help those who read rcParams["backend"] directly.)

@jklymak
Copy link
Member Author

jklymak commented Oct 22, 2018

I’m actually pretty strongly against having rcParams do things or having it store the current state of things. It should be a list of things the user would like to set and defaults and we should read it when we need to but we shouldn’t write to it and we shouldn’t trigger code from it’s getitem. That is really unexpected behaviour.

@anntzer
Copy link
Contributor

anntzer commented Oct 22, 2018

As noted in the linked comment, it doesn't actually "do" things, except for the arbitraryish constraint we have that use() cannot be called once the backend is resolved. Again, that constraint is self-imposed; nothing magical happens upon backend resolution, it's only when the event loop starts (much later) that backend switching actually becomes impossible.

@jklymak
Copy link
Member Author

jklymak commented Oct 22, 2018

I understand and agree that matplotlib.use should be able to be called as often as the user would like before the event loop starts. I'm not quite sure what bogged down with the API for that, as it seems pretty straightforward.

...and I agree and understand that this would fix this issue.

I'm still not comfortable with print(rcParams['backend']) doing anything other than printng the value stored in the dictionary.

@tacaswell
Copy link
Member

I am also 👎 on breaking type-stability of get_backend. If the OP use was if not matplotlib.get_backend().lower() == 'wxagg': it would be broken worse under this change.

@jklymak
Copy link
Member Author

jklymak commented Oct 22, 2018

No, I'm returning a string....

But @anntzer and I discussed another possible fix...

@jklymak jklymak closed this Oct 23, 2018
@jklymak jklymak deleted the fix-return-none-no-backend branch October 23, 2018 23:24
@QuLogic QuLogic modified the milestones: v3.0.x, unassigned Oct 23, 2018
@tacaswell
Copy link
Member

Ah, I did read too fast (and missed "None" vs None) sorry 🐑

@jklymak
Copy link
Member Author

jklymak commented Oct 23, 2018

The title of the PR wasn't very clear ;-) But, I'll try and work on this a bit tonight and make it work, particularly now that the MacOSX stuff is merged (and hopefully works)

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.

In 3.0.0 backend cannot be set if 'get_backend()' is run first
5 participants