Modify MatplotlibWidget to accept QWidget super constructor parameters.#2366
Modify MatplotlibWidget to accept QWidget super constructor parameters.#2366j9ac9k merged 10 commits intopyqtgraph:masterfrom Dolphindalt:master
Conversation
|
The code snippet I provided in #2364 is just a draft, there are several points to fix:
|
|
Sorry, I got ahead of myself. I added back the doc string and the other missing fields. The syntax of the constructors is great but unfamiliar, so I added some unit tests to make sure the constructors are behaving as expected i.e. a constructor with only the parent or figsize and dpi are recognized as defaults to be compatible with pyuic5 and the matplotlib syntax that the original author emulated. |
|
Hi @Dolphindalt Thanks for the PR, appreciate you adding some extra tests! We may need to remove the entry in CI test failure: Read the docs failure: We can put that line in if spec := importlib.util.find_spec('matplotlib') is not None:
from .widgets.MatplotlibWidget import * |
| mplw = pg.MatplotlibWidget(figsize, dpi) | ||
|
|
||
| assert np.allclose(mplw.getFigure().get_size_inches(), figsize) | ||
| assert mplw.getFigure().dpi == dpi No newline at end of file |
There was a problem hiding this comment.
please insert newline at the end of the file 👍🏻
|
This PR also introduces more typing annotations than we have in the library so far. This is fine; we want type annotations! Initially, I was going to suggest fully defining the type annotations for each of the overloads, but as we have no mechanism to test/check those annotations, probably best to leave it as is. |
…conda env files for running tests.
|
Could you add |
|
@pijyoi beat me to it with the |
|
Tests look good, once the unused import is removed, this is ready for merging. Thanks for the PR @Dolphindalt |
|
Thank you reviewers for being so patient with me despite my many mistakes. Learned a lot from doing this PR. Hopefully I can do more in the future with less mistakes. |
|
There is one more problem that the argument size has been changed to figsize, thanks to early discussion in #2364, I think it is not a good option to do this. |
Don't beat yourself up about it (or give it a second thought), some of my PRs get loads of rounds of updates/changes requested; happens to everyone. I periodically refer to PR 1318 (intentionally not hot-linking it) as it shows how many things I goofed on during creation/editing. And there were still issues after it was merged.
oh right! please verify the argument names are appropriate as well (I don't use matplotlib much so I'm not much of an authority here)
@haiiliin I'm confused by this sentence, are you suggesting this PR isn't a good idea, or that merging in the current state? |
I mean changing size to figsize is not a good option to do, sorry i didn't make it clear. |
|
@haiiliin got it, thanks for reviewing. It wouldn't need just the overloaded signature to have |
I think so. |
|
The change of # Matplotlib Figure constructor first arguments
class matplotlib.figure.Figure(figsize=None, dpi=None, ...)# MatplotlibWidget.py line 46
self.fig = Figure(figsize, dpi=dpi)It may not be appropriate to make this change because it will break existing code that is using the |
|
Is it appropriate to support both |
|
Passing keyword arguments after the parent argument does not work properly. class QWidget:
pass
class X:
def __init__(self, *args, **kwargs):
if (
(args and not isinstance(args[0], QWidget))
or
(kwargs and "figsize" in kwargs or "dpi" in kwargs)
):
print("AAA")
figsize = args[0] if len(args) > 0 else kwargs.get("figsize", (5.0, 4.0))
dpi = args[1] if len(args) > 1 else kwargs.get("dpi", 100)
parent = args[2] if len(args) > 2 else kwargs.get("parent", None)
else:
print("BBB")
parent = args[0] if len(args) > 0 else kwargs.get("parent", None)
figsize = kwargs.get("figsize", (5.0, 4.0))
dpi = kwargs.get("dpi", 100)
print(f'{figsize=}, {dpi=}')
# neither of the following work correctly, both enter AAA branch
X(QWidget(), figsize=(1, 1))
X(QWidget(), dpi=200) |
Will add unit tests to make sure these cases are behaving as expected + modifications to the constructor itself. |
I think if you pass the parent as the first positional argument, you are supposed to call the method in Qt style without matplotlib arguments. @typing.overload
def __init__(self, figsize=(5.0, 4.0), dpi=100, parent=None):
pass
@typing.overload
def __init__(self, parent=None):
pass |
…d code and magic numbers.
This behavior is supported along with providing named arguments in any order. This is typical Python behavior I think. |
Right, but then I think you have to modify the overloaded signatures, because calling it this way does not match any of the overloaded signatures. |
I tried to do this in the new commit. Please let me know if it is not what you have in mind. |
|
I mean calling it in this way.
Perhaps we should define an overloaded method like this: @typing.overload
def __init__(self, parent=None, size=(5.0, 4.0), dpi=100):
passBut anyway, I am not sure about making it this complicated is necessary. |
@typing.overload
def __init__(self, figsize=(5.0, 4.0), dpi=100, parent=None):
pass
@typing.overload
def __init__(self, parent=None, figsize=(5.0, 4.0), dpi=100):
passSo something like this? Both styles are supported this way and all parameters are present in each constructor, so there will only be two constructors. |
|
Right, I think it is quite a good solution now. |
|
@Dolphindalt Thank you so much for the PR, @haiiliin thank you so much for your continued comments/reviews, this LGTM, merging! |
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…s. (pyqtgraph#2366) * Modify MatplotlibWidget to accept QWidget super constructor parameters. Co-authored-by: Dalton Caron <dcaron@mtech.edu>
…arameters. (pyqtgraph#2366)" This reverts commit 5d3941e.
…arameters. (pyqtgraph#2366)" This reverts commit 5d3941e.
This pull request adds the parent and flags super constructor parameters of QWidget to MatplotlibWidget such that the parent parameter will be the first default parameter of the MatplotlibWidget constructor. The purpose of this is to enable users to pass the parent and flags parameters to the super QWidget class and to force the parent argument to be the first parameter of the MatplotlibWidget constructor for code generated by tools such as pyuic5.
See #2364 for additional discussion.