Skip to content

[Bug]: backend bug in matplotlib==3.6.1 with python3.11 and PySide6==6.4.0.1 #24332

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
zlmturnout opened this issue Nov 1, 2022 · 1 comment

Comments

@zlmturnout
Copy link

Bug summary

backend bug in matplotlib==3.6.1 with python3.11

same issue happend when embeding in Qt with PySide6==6.4.0.1

error report
int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'

This is caused by the "The new Enum system" within the release of PySide6, Qt for Python 6.4

See
Qt for python release 6.4

PySide is in the interesting position of being both Qt (with its C++ roots and all that entails) but also distinctly Python. Where possible, we strive to align PySide further with Python conventions and use as many Python features as we can, if they make sense.

Quote below:

The new Enum system

One such area that we tackled now affects enums, which was implemented as bound C++ classes, but now it is updated to leverage Python's own enum mechanism.

Although the new Python enums and the old Shiboken-based ones are compatible with each other, but there are a few differences or restrictions:

Python enums cannot be inherited from each other, whereas Shiboken enums can
Python enums do not allow undefined values, Shiboken enums do
Python enums always need exactly one argument, Shiboken enums have a default zero value
Python enums rarely inherit from int, Shiboken enums always do
You can see that some of these differences are between flags. For example, until now, you could either write

flags = Qt.Alignment() 

enum = Qt.AlignmentFlag 

or use the enum shortcuts like these:

Qt.AlignLeft = Qt.AlignmentFlag.AlignLeft 

Qt.AlignTop  = Qt.AlignmentFlag.AlignTop

These shortcuts, and flags no longer exist (officially) from 6.4.0 onwards. Instead, Python has an enum.Flags class, which is a subclass of the enum.Enum class.

If the thought of updating all your code left and right makes you sweat more than an overheated pumpkin spice latte, do not fret, for salvation has a name, and it’s “forgiveness mode”. This transitory mode allows you to continue using the old constructs but translates them internally into the new ones. For example, if you write

flags = Qt.Alignment() 

enum = Qt.AlignLeft 

item.setForeground(QColor(Qt.green)) 

flags_type = QPainter.RenderHints 

flags = QPainter.RenderHints() 

chart_view.setRenderHint(QPainter.Antialiasing) 

you get a construct that mimics the following code, which is the recommended way of using flags and enums:

flags = Qt.AlignmentFlag(0) 

enum = Qt.AlignmentFlag.AlignLeft 

item.setForeground(QColor(Qt.GlobalColor.green)) 

flags_type = QPainter.RenderHint 

flags = QPainter.RenderHint(0) 

chart_view.setRenderHint(QPainter.RenderHint.Antialiasing) 

With this you can at least initially ignore the difference between old and new enums, as long as the new enums are properties of classes.

Code for reproduction

import matplotlib
print(matplotlib.get_backend())

or 

from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qtagg import (
    FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure

Actual outcome

Traceback (most recent call last):
File "", line 1, in
File "C:\Python311\Lib\site-packages\matplotlib_init_.py", line 1182, in get_backend
return rcParams['backend']
~~~~~~~~^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\matplotlib_init_.py", line 677, in getitem
plt.switch_backend(rcsetup.auto_backend_sentinel)
File "C:\Python311\Lib\site-packages\matplotlib\pyplot.py", line 252, in switch_backend
switch_backend(candidate)
File "C:\Python311\Lib\site-packages\matplotlib\pyplot.py", line 265, in switch_backend
backend_mod = importlib.import_module(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\importlib_init
.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1206, in _gcd_import
File "", line 1178, in _find_and_load
File "", line 1149, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 940, in exec_module
File "", line 241, in _call_with_frames_removed
File "C:\Python311\Lib\site-packages\matplotlib\backends\backend_qtagg.py", line 12, in
from .backend_qt import QtCore, QtGui, _BackendQT, FigureCanvasQT
File "C:\Python311\Lib\site-packages\matplotlib\backends\backend_qt.py", line 72, in
_MODIFIER_KEYS = [
^
File "C:\Python311\Lib\site-packages\matplotlib\backends\backend_qt.py", line 73, in
(_to_int(getattr(_enum("QtCore.Qt.KeyboardModifier"), mod)),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'

Expected outcome

QtAgg

Additional information

No response

Operating system

Windows

Matplotlib Version

3.6.1

Matplotlib Backend

error as reported

Python version

3.11

Jupyter version

No response

Installation

pip

@tacaswell
Copy link
Member

Thank you for the report. The patch to work around the breaking change in pyside6 will be released in mpl 3.6.2

Duplicate of #24155 and fixed in #24158

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

No branches or pull requests

2 participants