Skip to content

Commit 4284a0b

Browse files
authored
Merge pull request #30275 from ZPyrolink/fix/connect_literal
Create events type and update plt.connect and mpl_connect
2 parents 15f2f37 + d2adb8d commit 4284a0b

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

lib/matplotlib/backend_bases.pyi

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ 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+
CapStyleType,
26+
CloseEventType,
27+
ColorType,
28+
DrawEventType,
29+
JoinStyleType,
30+
KeyEventType,
31+
LineStyleType,
32+
MouseEventType,
33+
PickEventType,
34+
ResizeEventType,
35+
)
2536

2637
def register_backend(
2738
format: str, backend: str | type[FigureCanvasBase], description: str | None = ...
@@ -354,37 +365,28 @@ class FigureCanvasBase:
354365
@overload
355366
def mpl_connect(
356367
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-
],
368+
s: MouseEventType,
367369
func: Callable[[MouseEvent], Any],
368370
) -> int: ...
369371

370372
@overload
371373
def mpl_connect(
372374
self,
373-
s: Literal["key_press_event", "key_release_event"],
375+
s: KeyEventType,
374376
func: Callable[[KeyEvent], Any],
375377
) -> int: ...
376378

377379
@overload
378-
def mpl_connect(self, s: Literal["pick_event"], func: Callable[[PickEvent], Any]) -> int: ...
380+
def mpl_connect(self, s: PickEventType, func: Callable[[PickEvent], Any]) -> int: ...
379381

380382
@overload
381-
def mpl_connect(self, s: Literal["resize_event"], func: Callable[[ResizeEvent], Any]) -> int: ...
383+
def mpl_connect(self, s: ResizeEventType, func: Callable[[ResizeEvent], Any]) -> int: ...
382384

383385
@overload
384-
def mpl_connect(self, s: Literal["close_event"], func: Callable[[CloseEvent], Any]) -> int: ...
386+
def mpl_connect(self, s: CloseEventType, func: Callable[[CloseEvent], Any]) -> int: ...
385387

386388
@overload
387-
def mpl_connect(self, s: str, func: Callable[[Event], Any]) -> int: ...
389+
def mpl_connect(self, s: DrawEventType, func: Callable[[DrawEvent], Any]) -> int: ...
388390
def mpl_disconnect(self, cid: int) -> None: ...
389391
def new_timer(
390392
self,

lib/matplotlib/pyplot.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@
100100
import matplotlib.backend_bases
101101
from matplotlib.axis import Tick
102102
from matplotlib.axes._base import _AxesBase
103-
from matplotlib.backend_bases import Event
103+
from matplotlib.backend_bases import (
104+
CloseEvent,
105+
DrawEvent,
106+
KeyEvent,
107+
MouseEvent,
108+
PickEvent,
109+
ResizeEvent,
110+
)
104111
from matplotlib.cm import ScalarMappable
105112
from matplotlib.contour import ContourSet, QuadContourSet
106113
from matplotlib.collections import (
@@ -126,11 +133,17 @@
126133
from matplotlib.quiver import Barbs, Quiver, QuiverKey
127134
from matplotlib.scale import ScaleBase
128135
from matplotlib.typing import (
136+
CloseEventType,
129137
ColorType,
130138
CoordsType,
139+
DrawEventType,
131140
HashableList,
141+
KeyEventType,
132142
LineStyleType,
133143
MarkerType,
144+
MouseEventType,
145+
PickEventType,
146+
ResizeEventType,
134147
)
135148
from matplotlib.widgets import SubplotTool
136149

@@ -1175,8 +1188,32 @@ def get_current_fig_manager() -> FigureManagerBase | None:
11751188
return gcf().canvas.manager
11761189

11771190

1191+
@overload
1192+
def connect(s: MouseEventType, func: Callable[[MouseEvent], Any]) -> int: ...
1193+
1194+
1195+
@overload
1196+
def connect(s: KeyEventType, func: Callable[[KeyEvent], Any]) -> int: ...
1197+
1198+
1199+
@overload
1200+
def connect(s: PickEventType, func: Callable[[PickEvent], Any]) -> int: ...
1201+
1202+
1203+
@overload
1204+
def connect(s: ResizeEventType, func: Callable[[ResizeEvent], Any]) -> int: ...
1205+
1206+
1207+
@overload
1208+
def connect(s: CloseEventType, func: Callable[[CloseEvent], Any]) -> int: ...
1209+
1210+
1211+
@overload
1212+
def connect(s: DrawEventType, func: Callable[[DrawEvent], Any]) -> int: ...
1213+
1214+
11781215
@_copy_docstring_and_deprecators(FigureCanvasBase.mpl_connect)
1179-
def connect(s: str, func: Callable[[Event], Any]) -> int:
1216+
def connect(s, func) -> int:
11801217
return gcf().canvas.mpl_connect(s, func)
11811218

11821219

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy