1313 This API is experimental in IPython 2.0, and may be revised in future versions.
1414"""
1515
16+ from __future__ import annotations
17+
18+ from typing import TYPE_CHECKING , Any , Callable , Iterable
19+
20+ if TYPE_CHECKING :
21+ from IPython .core .interactiveshell import InteractiveShell
22+
1623
1724class EventManager :
1825 """Manage a collection of events and a sequence of callbacks for each.
@@ -25,7 +32,12 @@ class EventManager:
2532 This API is experimental in IPython 2.0, and may be revised in future versions.
2633 """
2734
28- def __init__ (self , shell , available_events , print_on_error = True ):
35+ def __init__ (
36+ self ,
37+ shell : InteractiveShell ,
38+ available_events : Iterable [str ],
39+ print_on_error : bool = True ,
40+ ) -> None :
2941 """Initialise the :class:`CallbackManager`.
3042
3143 Parameters
@@ -38,10 +50,12 @@ def __init__(self, shell, available_events, print_on_error=True):
3850 A boolean flag to set whether the EventManager will print a warning which a event errors.
3951 """
4052 self .shell = shell
41- self .callbacks = {n :[] for n in available_events }
53+ self .callbacks : dict [str , list [Callable [..., Any ]]] = {
54+ n : [] for n in available_events
55+ }
4256 self .print_on_error = print_on_error
4357
44- def register (self , event , function ) :
58+ def register (self , event : str , function : Callable [..., Any ]) -> None :
4559 """Register a new event callback.
4660
4761 Parameters
@@ -64,14 +78,14 @@ def register(self, event, function):
6478 if function not in self .callbacks [event ]:
6579 self .callbacks [event ].append (function )
6680
67- def unregister (self , event , function ) :
81+ def unregister (self , event : str , function : Callable [..., Any ]) -> None :
6882 """Remove a callback from the given event."""
6983 if function in self .callbacks [event ]:
7084 return self .callbacks [event ].remove (function )
7185
7286 raise ValueError ('Function {!r} is not registered as a {} callback' .format (function , event ))
7387
74- def trigger (self , event , * args , ** kwargs ) :
88+ def trigger (self , event : str , * args : Any , ** kwargs : Any ) -> None :
7589 """Call callbacks for ``event``.
7690
7791 Any additional arguments are passed to all callbacks registered for this
@@ -90,9 +104,9 @@ def trigger(self, event, *args, **kwargs):
90104 self .shell .showtraceback ()
91105
92106# event_name -> prototype mapping
93- available_events = {}
107+ available_events : dict [ str , Callable [..., Any ]] = {}
94108
95- def _define_event (callback_function ) :
109+ def _define_event (callback_function : Callable [..., Any ]) -> Callable [..., Any ] :
96110 available_events [callback_function .__name__ ] = callback_function
97111 return callback_function
98112
@@ -104,7 +118,7 @@ def _define_event(callback_function):
104118# ------------------------------------------------------------------------------
105119
106120@_define_event
107- def pre_execute ():
121+ def pre_execute () -> None :
108122 """Fires before code is executed in response to user/frontend action.
109123
110124 This includes comm and widget messages and silent execution, as well as user
@@ -113,7 +127,7 @@ def pre_execute():
113127 pass
114128
115129@_define_event
116- def pre_run_cell (info ) :
130+ def pre_run_cell (info : Any ) -> None :
117131 """Fires before user-entered code runs.
118132
119133 Parameters
@@ -124,7 +138,7 @@ def pre_run_cell(info):
124138 pass
125139
126140@_define_event
127- def post_execute ():
141+ def post_execute () -> None :
128142 """Fires after code is executed in response to user/frontend action.
129143
130144 This includes comm and widget messages and silent execution, as well as user
@@ -133,7 +147,7 @@ def post_execute():
133147 pass
134148
135149@_define_event
136- def post_run_cell (result ) :
150+ def post_run_cell (result : Any ) -> None :
137151 """Fires after user-entered code runs.
138152
139153 Parameters
@@ -144,7 +158,7 @@ def post_run_cell(result):
144158 pass
145159
146160@_define_event
147- def shell_initialized (ip ) :
161+ def shell_initialized (ip : InteractiveShell ) -> None :
148162 """Fires after initialisation of :class:`~IPython.core.interactiveshell.InteractiveShell`.
149163
150164 This is before extensions and startup scripts are loaded, so it can only be
0 commit comments