1+ from __future__ import annotations
2+
13import asyncio
24import contextvars
35import os
@@ -189,36 +191,33 @@ class Application(Generic[_AppResult]):
189191
190192 def __init__ (
191193 self ,
192- layout : Optional [ Layout ] = None ,
193- style : Optional [ BaseStyle ] = None ,
194+ layout : Layout | None = None ,
195+ style : BaseStyle | None = None ,
194196 include_default_pygments_style : FilterOrBool = True ,
195- style_transformation : Optional [ StyleTransformation ] = None ,
196- key_bindings : Optional [ KeyBindingsBase ] = None ,
197- clipboard : Optional [ Clipboard ] = None ,
197+ style_transformation : StyleTransformation | None = None ,
198+ key_bindings : KeyBindingsBase | None = None ,
199+ clipboard : Clipboard | None = None ,
198200 full_screen : bool = False ,
199- color_depth : Union [
200- ColorDepth , Callable [[], Union [ColorDepth , None ]], None
201- ] = None ,
201+ color_depth : (ColorDepth | Callable [[], ColorDepth | None ] | None ) = None ,
202202 mouse_support : FilterOrBool = False ,
203- enable_page_navigation_bindings : Optional [
204- FilterOrBool
205- ] = None , # Can be None, True or False.
203+ enable_page_navigation_bindings : None
204+ | (FilterOrBool ) = None , # Can be None, True or False.
206205 paste_mode : FilterOrBool = False ,
207206 editing_mode : EditingMode = EditingMode .EMACS ,
208207 erase_when_done : bool = False ,
209208 reverse_vi_search_direction : FilterOrBool = False ,
210- min_redraw_interval : Union [ float , int , None ] = None ,
211- max_render_postpone_time : Union [ float , int , None ] = 0.01 ,
212- refresh_interval : Optional [ float ] = None ,
213- terminal_size_polling_interval : Optional [ float ] = 0.5 ,
209+ min_redraw_interval : float | int | None = None ,
210+ max_render_postpone_time : float | int | None = 0.01 ,
211+ refresh_interval : float | None = None ,
212+ terminal_size_polling_interval : float | None = 0.5 ,
214213 cursor : AnyCursorShapeConfig = None ,
215- on_reset : Optional [ " ApplicationEventHandler[_AppResult]" ] = None ,
216- on_invalidate : Optional [ " ApplicationEventHandler[_AppResult]" ] = None ,
217- before_render : Optional [ " ApplicationEventHandler[_AppResult]" ] = None ,
218- after_render : Optional [ " ApplicationEventHandler[_AppResult]" ] = None ,
214+ on_reset : ApplicationEventHandler [_AppResult ] | None = None ,
215+ on_invalidate : ApplicationEventHandler [_AppResult ] | None = None ,
216+ before_render : ApplicationEventHandler [_AppResult ] | None = None ,
217+ after_render : ApplicationEventHandler [_AppResult ] | None = None ,
219218 # I/O.
220- input : Optional [ Input ] = None ,
221- output : Optional [ Output ] = None ,
219+ input : Input | None = None ,
220+ output : Output | None = None ,
222221 ) -> None :
223222 # If `enable_page_navigation_bindings` is not specified, enable it in
224223 # case of full screen applications only. This can be overridden by the user.
@@ -275,12 +274,12 @@ def __init__(
275274 self .input = input or session .input
276275
277276 # List of 'extra' functions to execute before a Application.run.
278- self .pre_run_callables : List [Callable [[], None ]] = []
277+ self .pre_run_callables : list [Callable [[], None ]] = []
279278
280279 self ._is_running = False
281- self .future : Optional [ Future [_AppResult ]] = None
282- self .loop : Optional [ AbstractEventLoop ] = None
283- self .context : Optional [ contextvars .Context ] = None
280+ self .future : Future [_AppResult ] | None = None
281+ self .loop : AbstractEventLoop | None = None
282+ self .context : contextvars .Context | None = None
284283
285284 #: Quoted insert. This flag is set if we go into quoted insert mode.
286285 self .quoted_insert = False
@@ -325,7 +324,7 @@ def __init__(
325324
326325 # Invalidate flag. When 'True', a repaint has been scheduled.
327326 self ._invalidated = False
328- self ._invalidate_events : List [
327+ self ._invalidate_events : list [
329328 Event [object ]
330329 ] = [] # Collection of 'invalidate' Event objects.
331330 self ._last_redraw_time = 0.0 # Unix timestamp of last redraw. Used when
@@ -337,7 +336,7 @@ def __init__(
337336 # If `run_in_terminal` was called. This will point to a `Future` what will be
338337 # set at the point when the previous run finishes.
339338 self ._running_in_terminal = False
340- self ._running_in_terminal_f : Optional [ Future [None ]] = None
339+ self ._running_in_terminal_f : Future [None ] | None = None
341340
342341 # Trigger initialize callback.
343342 self .reset ()
@@ -425,7 +424,7 @@ def reset(self) -> None:
425424
426425 self .exit_style = ""
427426
428- self ._background_tasks : Set [Task [None ]] = set ()
427+ self ._background_tasks : set [Task [None ]] = set ()
429428
430429 self .renderer .reset ()
431430 self .key_processor .reset ()
@@ -605,7 +604,7 @@ def _on_resize(self) -> None:
605604 self ._request_absolute_cursor_position ()
606605 self ._redraw ()
607606
608- def _pre_run (self , pre_run : Optional [ Callable [[], None ]] = None ) -> None :
607+ def _pre_run (self , pre_run : Callable [[], None ] | None = None ) -> None :
609608 """
610609 Called during `run`.
611610
@@ -625,7 +624,7 @@ def _pre_run(self, pre_run: Optional[Callable[[], None]] = None) -> None:
625624
626625 async def run_async (
627626 self ,
628- pre_run : Optional [ Callable [[], None ]] = None ,
627+ pre_run : Callable [[], None ] | None = None ,
629628 set_exception_handler : bool = True ,
630629 handle_sigint : bool = True ,
631630 slow_callback_duration : float = 0.5 ,
@@ -670,7 +669,7 @@ async def _run_async(f: "asyncio.Future[_AppResult]") -> _AppResult:
670669 # pressed, we start a 'flush' timer for flushing our escape key. But
671670 # when any subsequent input is received, a new timer is started and
672671 # the current timer will be ignored.
673- flush_task : Optional [ asyncio .Task [None ]] = None
672+ flush_task : asyncio .Task [None ] | None = None
674673
675674 # Reset.
676675 # (`self.future` needs to be set when `pre_run` is called.)
@@ -840,7 +839,7 @@ def set_callback_duration(loop: AbstractEventLoop) -> Iterator[None]:
840839 @contextmanager
841840 def create_future (
842841 loop : AbstractEventLoop ,
843- ) -> " Iterator[asyncio.Future[_AppResult]]" :
842+ ) -> Iterator [asyncio .Future [_AppResult ]]:
844843 f = loop .create_future ()
845844 self .future = f # XXX: make sure to set this before calling '_redraw'.
846845
@@ -889,7 +888,7 @@ def create_future(
889888
890889 def run (
891890 self ,
892- pre_run : Optional [ Callable [[], None ]] = None ,
891+ pre_run : Callable [[], None ] | None = None ,
893892 set_exception_handler : bool = True ,
894893 handle_sigint : bool = True ,
895894 in_thread : bool = False ,
@@ -922,7 +921,7 @@ def run(
922921 """
923922 if in_thread :
924923 result : _AppResult
925- exception : Optional [ BaseException ] = None
924+ exception : BaseException | None = None
926925
927926 def run_in_thread () -> None :
928927 nonlocal result , exception
@@ -953,7 +952,7 @@ def run_in_thread() -> None:
953952 )
954953
955954 def _handle_exception (
956- self , loop : AbstractEventLoop , context : Dict [str , Any ]
955+ self , loop : AbstractEventLoop , context : dict [str , Any ]
957956 ) -> None :
958957 """
959958 Handler for event loop exceptions.
@@ -1029,7 +1028,7 @@ def trace_dispatch(
10291028
10301029 def create_background_task (
10311030 self , coroutine : Coroutine [Any , Any , None ]
1032- ) -> " asyncio.Task[None]" :
1031+ ) -> asyncio .Task [None ]:
10331032 """
10341033 Start a background task (coroutine) for the running application. When
10351034 the `Application` terminates, unfinished background tasks will be
@@ -1053,7 +1052,7 @@ def create_background_task(
10531052 task .add_done_callback (self ._on_background_task_done )
10541053 return task
10551054
1056- def _on_background_task_done (self , task : " asyncio.Task[None]" ) -> None :
1055+ def _on_background_task_done (self , task : asyncio .Task [None ]) -> None :
10571056 """
10581057 Called when a background task completes. Remove it from
10591058 `_background_tasks`, and handle exceptions if any.
@@ -1114,7 +1113,7 @@ async def _poll_output_size(self) -> None:
11141113 - If we are not running in the main thread.
11151114 - On Windows.
11161115 """
1117- size : Optional [ Size ] = None
1116+ size : Size | None = None
11181117 interval = self .terminal_size_polling_interval
11191118
11201119 if interval is None :
@@ -1153,14 +1152,14 @@ def exit(self, *, result: _AppResult, style: str = "") -> None:
11531152
11541153 @overload
11551154 def exit (
1156- self , * , exception : Union [ BaseException , Type [BaseException ] ], style : str = ""
1155+ self , * , exception : BaseException | type [BaseException ], style : str = ""
11571156 ) -> None :
11581157 "Exit with exception."
11591158
11601159 def exit (
11611160 self ,
1162- result : Optional [ _AppResult ] = None ,
1163- exception : Optional [ Union [ BaseException , Type [BaseException ]]] = None ,
1161+ result : _AppResult | None = None ,
1162+ exception : BaseException | type [BaseException ] | None = None ,
11641163 style : str = "" ,
11651164 ) -> None :
11661165 """
@@ -1275,7 +1274,7 @@ def run() -> None:
12751274 run_in_terminal (run )
12761275
12771276 def print_text (
1278- self , text : AnyFormattedText , style : Optional [ BaseStyle ] = None
1277+ self , text : AnyFormattedText , style : BaseStyle | None = None
12791278 ) -> None :
12801279 """
12811280 Print a list of (style_str, text) tuples to the output.
@@ -1304,7 +1303,7 @@ def is_done(self) -> bool:
13041303 return self .future .done ()
13051304 return False
13061305
1307- def get_used_style_strings (self ) -> List [str ]:
1306+ def get_used_style_strings (self ) -> list [str ]:
13081307 """
13091308 Return a list of used style strings. This is helpful for debugging, and
13101309 for writing a new `Style`.
@@ -1330,7 +1329,7 @@ class _CombinedRegistry(KeyBindingsBase):
13301329 def __init__ (self , app : Application [_AppResult ]) -> None :
13311330 self .app = app
13321331 self ._cache : SimpleCache [
1333- Tuple [Window , FrozenSet [UIControl ]], KeyBindingsBase
1332+ tuple [Window , frozenset [UIControl ]], KeyBindingsBase
13341333 ] = SimpleCache ()
13351334
13361335 @property
@@ -1340,13 +1339,13 @@ def _version(self) -> Hashable:
13401339 raise NotImplementedError
13411340
13421341 @property
1343- def bindings (self ) -> List [Binding ]:
1342+ def bindings (self ) -> list [Binding ]:
13441343 """Not needed - this object is not going to be wrapped in another
13451344 KeyBindings object."""
13461345 raise NotImplementedError
13471346
13481347 def _create_key_bindings (
1349- self , current_window : Window , other_controls : List [UIControl ]
1348+ self , current_window : Window , other_controls : list [UIControl ]
13501349 ) -> KeyBindingsBase :
13511350 """
13521351 Create a `KeyBindings` object that merges the `KeyBindings` from the
@@ -1409,10 +1408,10 @@ def _key_bindings(self) -> KeyBindingsBase:
14091408 key , lambda : self ._create_key_bindings (current_window , other_controls )
14101409 )
14111410
1412- def get_bindings_for_keys (self , keys : KeysTuple ) -> List [Binding ]:
1411+ def get_bindings_for_keys (self , keys : KeysTuple ) -> list [Binding ]:
14131412 return self ._key_bindings .get_bindings_for_keys (keys )
14141413
1415- def get_bindings_starting_with_keys (self , keys : KeysTuple ) -> List [Binding ]:
1414+ def get_bindings_starting_with_keys (self , keys : KeysTuple ) -> list [Binding ]:
14161415 return self ._key_bindings .get_bindings_starting_with_keys (keys )
14171416
14181417
0 commit comments