Skip to content

Create events type and update plt.connect and mpl_connect #30275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions lib/matplotlib/backend_bases.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ from matplotlib.transforms import Bbox, BboxBase, Transform, TransformedPath
from collections.abc import Callable, Iterable, Sequence
from typing import Any, IO, Literal, NamedTuple, TypeVar, overload
from numpy.typing import ArrayLike
from .typing import ColorType, LineStyleType, CapStyleType, JoinStyleType
from .typing import (
CapStyleType,
CloseEventType,
ColorType,
DrawEventType,
JoinStyleType,
KeyEventType,
LineStyleType,
MouseEventType,
PickEventType,
ResizeEventType,
)

def register_backend(
format: str, backend: str | type[FigureCanvasBase], description: str | None = ...
Expand Down Expand Up @@ -354,37 +365,28 @@ class FigureCanvasBase:
@overload
def mpl_connect(
self,
s: Literal[
"button_press_event",
"motion_notify_event",
"scroll_event",
"figure_enter_event",
"figure_leave_event",
"axes_enter_event",
"axes_leave_event",
"button_release_event",
],
s: MouseEventType,
func: Callable[[MouseEvent], Any],
) -> int: ...

@overload
def mpl_connect(
self,
s: Literal["key_press_event", "key_release_event"],
s: KeyEventType,
func: Callable[[KeyEvent], Any],
) -> int: ...

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

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

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

@overload
def mpl_connect(self, s: str, func: Callable[[Event], Any]) -> int: ...
def mpl_connect(self, s: DrawEventType, func: Callable[[DrawEvent], Any]) -> int: ...
def mpl_disconnect(self, cid: int) -> None: ...
def new_timer(
self,
Expand Down
41 changes: 39 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@
import matplotlib.backend_bases
from matplotlib.axis import Tick
from matplotlib.axes._base import _AxesBase
from matplotlib.backend_bases import Event
from matplotlib.backend_bases import (
CloseEvent,
DrawEvent,
KeyEvent,
MouseEvent,
PickEvent,
ResizeEvent,
)
from matplotlib.cm import ScalarMappable
from matplotlib.contour import ContourSet, QuadContourSet
from matplotlib.collections import (
Expand All @@ -126,11 +133,17 @@
from matplotlib.quiver import Barbs, Quiver, QuiverKey
from matplotlib.scale import ScaleBase
from matplotlib.typing import (
CloseEventType,
ColorType,
CoordsType,
DrawEventType,
HashableList,
KeyEventType,
LineStyleType,
MarkerType,
MouseEventType,
PickEventType,
ResizeEventType,
)
from matplotlib.widgets import SubplotTool

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


@overload
def connect(s: MouseEventType, func: Callable[[MouseEvent], Any]) -> int: ...


@overload
def connect(s: KeyEventType, func: Callable[[KeyEvent], Any]) -> int: ...


@overload
def connect(s: PickEventType, func: Callable[[PickEvent], Any]) -> int: ...


@overload
def connect(s: ResizeEventType, func: Callable[[ResizeEvent], Any]) -> int: ...


@overload
def connect(s: CloseEventType, func: Callable[[CloseEvent], Any]) -> int: ...


@overload
def connect(s: DrawEventType, func: Callable[[DrawEvent], Any]) -> int: ...


@_copy_docstring_and_deprecators(FigureCanvasBase.mpl_connect)
def connect(s: str, func: Callable[[Event], Any]) -> int:
def connect(s, func) -> int:
return gcf().canvas.mpl_connect(s, func)


Expand Down
30 changes: 30 additions & 0 deletions lib/matplotlib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,33 @@
_HT = TypeVar("_HT", bound=Hashable)
HashableList: TypeAlias = list[_HT | "HashableList[_HT]"]
"""A nested list of Hashable values."""

MouseEventType: TypeAlias = Literal[
"button_press_event",
"button_release_event",
"motion_notify_event",
"scroll_event",
"figure_enter_event",
"figure_leave_event",
"axes_enter_event",
"axes_leave_event",
]

KeyEventType: TypeAlias = Literal[
"key_press_event",
"key_release_event"
]

DrawEventType: TypeAlias = Literal["draw_event"]
PickEventType: TypeAlias = Literal["pick_event"]
ResizeEventType: TypeAlias = Literal["resize_event"]
CloseEventType: TypeAlias = Literal["close_event"]

EventType: TypeAlias = Literal[
MouseEventType,
KeyEventType,
DrawEventType,
PickEventType,
ResizeEventType,
CloseEventType,
]
Loading