Skip to content

Commit f3e2966

Browse files
committed
Create events type and update plt.connect and mpl_connect
1 parent b6777b1 commit f3e2966

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

lib/matplotlib/backend_bases.pyi

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ from matplotlib.transforms import Bbox, BboxBase, Transform, TransformedPath
2121
from collections.abc import Callable, Iterable, Sequence
2222
from typing import Any, IO, Literal, NamedTuple, TypeVar, overload
2323
from numpy.typing import ArrayLike
24-
from .typing import ColorType, LineStyleType, CapStyleType, JoinStyleType
24+
from .typing import (
25+
ColorType,
26+
LineStyleType,
27+
CapStyleType,
28+
JoinStyleType,
29+
MouseEventType,
30+
KeyEventType,
31+
DrawEventType,
32+
PickEventType,
33+
ResizeEventType,
34+
CloseEventType,
35+
EventType
36+
)
2537

2638
def register_backend(
2739
format: str, backend: str | type[FigureCanvasBase], description: str | None = ...
@@ -354,37 +366,31 @@ class FigureCanvasBase:
354366
@overload
355367
def mpl_connect(
356368
self,
357-
s: Literal[
358-
"button_press_event",
359-
"motion_notify_event",
360-
"scroll_event",
361-
"figure_enter_event",
362-
"figure_leave_event",
363-
"axes_enter_event",
364-
"axes_leave_event",
365-
"button_release_event",
366-
],
369+
s: MouseEventType,
367370
func: Callable[[MouseEvent], Any],
368371
) -> int: ...
369372

370373
@overload
371374
def mpl_connect(
372375
self,
373-
s: Literal["key_press_event", "key_release_event"],
376+
s: KeyEventType,
374377
func: Callable[[KeyEvent], Any],
375378
) -> int: ...
376379

377380
@overload
378-
def mpl_connect(self, s: Literal["pick_event"], func: Callable[[PickEvent], Any]) -> int: ...
381+
def mpl_connect(self, s: PickEventType, func: Callable[[PickEvent], Any]) -> int: ...
382+
383+
@overload
384+
def mpl_connect(self, s: ResizeEventType, func: Callable[[ResizeEvent], Any]) -> int: ...
379385

380386
@overload
381-
def mpl_connect(self, s: Literal["resize_event"], func: Callable[[ResizeEvent], Any]) -> int: ...
387+
def mpl_connect(self, s: CloseEventType, func: Callable[[CloseEvent], Any]) -> int: ...
382388

383389
@overload
384-
def mpl_connect(self, s: Literal["close_event"], func: Callable[[CloseEvent], Any]) -> int: ...
390+
def mpl_connect(self, s: DrawEventType, func: Callable[[DrawEvent], Any]) -> int: ...
385391

386392
@overload
387-
def mpl_connect(self, s: str, func: Callable[[Event], Any]) -> int: ...
393+
def mpl_connect(self, s: EventType, func: Callable[[Event], Any]) -> int: ...
388394
def mpl_disconnect(self, cid: int) -> None: ...
389395
def new_timer(
390396
self,

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
HashableList,
132132
LineStyleType,
133133
MarkerType,
134+
EventType
134135
)
135136
from matplotlib.widgets import SubplotTool
136137

@@ -1176,7 +1177,7 @@ def get_current_fig_manager() -> FigureManagerBase | None:
11761177

11771178

11781179
@_copy_docstring_and_deprecators(FigureCanvasBase.mpl_connect)
1179-
def connect(s: str, func: Callable[[Event], Any]) -> int:
1180+
def connect(s: EventType, func: Callable[[Event], Any]) -> int:
11801181
return gcf().canvas.mpl_connect(s, func)
11811182

11821183

lib/matplotlib/typing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,33 @@
107107
_HT = TypeVar("_HT", bound=Hashable)
108108
HashableList: TypeAlias = list[_HT | "HashableList[_HT]"]
109109
"""A nested list of Hashable values."""
110+
111+
MouseEventType: TypeAlias = Literal[
112+
"button_press_event",
113+
"button_release_event",
114+
"motion_notify_event",
115+
"scroll_event",
116+
"figure_enter_event",
117+
"figure_leave_event",
118+
"axes_enter_event",
119+
"axes_leave_event",
120+
]
121+
122+
KeyEventType: TypeAlias = Literal[
123+
"key_press_event",
124+
"key_release_event"
125+
]
126+
127+
DrawEventType: TypeAlias = Literal["draw_event"]
128+
PickEventType: TypeAlias = Literal["pick_event"]
129+
ResizeEventType: TypeAlias = Literal["resize_event"]
130+
CloseEventType: TypeAlias = Literal["close_event"]
131+
132+
EventType: TypeAlias = Literal[
133+
MouseEventType,
134+
KeyEventType,
135+
DrawEventType,
136+
PickEventType,
137+
ResizeEventType,
138+
CloseEventType,
139+
]

0 commit comments

Comments
 (0)