Reputation: 1
I am adding type annotations to some code that handles events in a Tkinter GUI using a matplotlib canvas. PyCharm is giving a type check warning:
"Expected type '(Event) -> Any' got '(event: MouseEvent) -> None' instead"
Code looks like this:
class ViewWindow(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.draw()
self.canvas.get_tk_widget().pack(fill='both', expand=True)
self.handler = EventHandler(self)
class EventHandler:
def __init__(self, parent):
self.parent = parent
self.parent.canvas.mpl_connect('button_press_event', self.on_press)
def on_press(self, event: MouseEvent) -> None:
# do some stuff that uses the MouseEvent attributes
print(event)
PyCharm is taking issue with the self.on_press method having a "MouseEvent" type event when it wants an "Event" type in the function signature. "MouseEvent" is a child class of "Event" and everything works as it should. If I comply and change the event annotation to "Event" it throws up warnings that some of the attributes of MouseEvent I am using do not exist.
Can someone help me understand why PyCharm is complaining? I can make the warning go away by using "# noinspection PyTypeChecker" before it. Is there a right way to annotate this?
I tried changing the return type to "Any", this made no difference. I tried changing the event type to "Event" this go rid of the warning but introduced several others where I was using attributes that "MouseEvent" has that "Event" does not.
Upvotes: 0
Views: 35