Skip to content

deprecate GraphicsObject::parentChanged method#2420

Merged
j9ac9k merged 1 commit intopyqtgraph:masterfrom
pijyoi:rename-parent-changed
Sep 27, 2022
Merged

deprecate GraphicsObject::parentChanged method#2420
j9ac9k merged 1 commit intopyqtgraph:masterfrom
pijyoi:rename-parent-changed

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Sep 18, 2022

QGraphicsObject has a signal named parentChanged.
Although Qt bindings "permit" a method name to be the same as a Qt signal name, it may not be a good idea to make use of this "feature".
This "feature" has been broken before: #2132
It also relies on the inheritance order of the parent classes of GraphicsObject.

This PR renames the method name while emitting a deprecation warning if it detects that the old name is being used by user sub-classes.

One way to test out this PR is to modify examples/customGraphicsItem.py.
By adding the following method:

def parentChanged(self):
    super().parentChanged()

We can verify that running this example with python -Wdefault emits a deprecation warning.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Sep 18, 2022

The detection only works for the simplest case where the method is at the last level in an inheritance hierarchy.

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Sep 19, 2022

Based on a search of the repo, #1991 led me to https://github.com/sem-geologist/HussariX/blob/master/lib/ui/CustomPGWidgets.py#L98 which has a class that subclasses pg.ScaleBar.
The current detection technique would fail there.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 19, 2022

@sem-geologist has been active in the issue tracker here before; hopefully this ping will get their attention

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Sep 19, 2022

The detection would think that CustomScaleBar doesn't use the deprecated method parentChanged, not knowing whether any parent class does override.

It would then call self.parentChanged_() which would correctly invoke ScaleBar::parentChanged_().

It works in this case only because the parent (pyqtgraph) class was updated to use parentChanged_().

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 23, 2022

Hi @pijyoi

Thanks for the PR!. The diff looks good to me, but I'm a little hung up on the name, parentChanged_. The name does achieve the primary objective, which is to differentiate the method from the signal name, but the past-tense wording is in-line with the name of a signal. An alternative would be changeParent, but I'm not set on that. I'm open to other suggestions.

@pijyoi pijyoi force-pushed the rename-parent-changed branch from 1212d30 to f6487c2 Compare September 23, 2022 04:03
@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Sep 23, 2022

A contrived example where the current detection technique fails.

import pyqtgraph as pg 
from pyqtgraph.Qt import QtCore

class CustomParent(pg.GraphicsObject):
    def __init__(self):
        pg.GraphicsObject.__init__(self)
        self.text = "OLD PARENT"

    def boundingRect(self):
        return QtCore.QRectF(0, 0, 100, 100)

    def paint(self, p, *args):
        p.setPen(pg.mkPen('w'))
        p.drawText(0, 50, self.text)

    def parentChanged(self):
        self.text = "NEW PARENT"

class CustomChild(CustomParent):
    def __init__(self):
        CustomParent.__init__(self)
        self.text = "CHILD"

pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
custom = CustomChild()
pw.addItem(custom)
pg.exec()

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 23, 2022

Maybe I have the wrong attitude about this, but I'm not so concerned about catching every usage of this method... this method is buried fairly deep in the library, and I wouldn't imagine that many end-users overwriting it...

Anyway, I'm fine w/ the current code diff.

QGraphicsObject has a signal named parentChanged
@pijyoi pijyoi force-pushed the rename-parent-changed branch from f6487c2 to 9b8add7 Compare September 24, 2022 00:14
@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented Sep 24, 2022

I am generally unfamiliar with all the viewbox machinery. The docstring for parentChanged() says "It should generally be extended, not overridden.. And yet the only use of parentChanged() within the library (ScaleBar) doesn't call the parent method.

Maybe merge this "breaking" change after 0.13.0?

@pijyoi pijyoi marked this pull request as ready for review September 24, 2022 00:22
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Sep 27, 2022

Since we just released 0.13.0, let's merge this one! Thanks for your work here @pijyoi

@j9ac9k j9ac9k merged commit 1f9475e into pyqtgraph:master Sep 27, 2022
@pijyoi pijyoi deleted the rename-parent-changed branch September 27, 2022 23:02
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.

2 participants