1
+ from __future__ import annotations
2
+
1
3
import asyncio
2
4
import contextvars
3
5
import os
@@ -189,36 +191,33 @@ class Application(Generic[_AppResult]):
189
191
190
192
def __init__ (
191
193
self ,
192
- layout : Optional [ Layout ] = None ,
193
- style : Optional [ BaseStyle ] = None ,
194
+ layout : Layout | None = None ,
195
+ style : BaseStyle | None = None ,
194
196
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 ,
198
200
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 ,
202
202
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.
206
205
paste_mode : FilterOrBool = False ,
207
206
editing_mode : EditingMode = EditingMode .EMACS ,
208
207
erase_when_done : bool = False ,
209
208
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 ,
214
213
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 ,
219
218
# I/O.
220
- input : Optional [ Input ] = None ,
221
- output : Optional [ Output ] = None ,
219
+ input : Input | None = None ,
220
+ output : Output | None = None ,
222
221
) -> None :
223
222
# If `enable_page_navigation_bindings` is not specified, enable it in
224
223
# case of full screen applications only. This can be overridden by the user.
@@ -275,12 +274,12 @@ def __init__(
275
274
self .input = input or session .input
276
275
277
276
# 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 ]] = []
279
278
280
279
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
284
283
285
284
#: Quoted insert. This flag is set if we go into quoted insert mode.
286
285
self .quoted_insert = False
@@ -325,7 +324,7 @@ def __init__(
325
324
326
325
# Invalidate flag. When 'True', a repaint has been scheduled.
327
326
self ._invalidated = False
328
- self ._invalidate_events : List [
327
+ self ._invalidate_events : list [
329
328
Event [object ]
330
329
] = [] # Collection of 'invalidate' Event objects.
331
330
self ._last_redraw_time = 0.0 # Unix timestamp of last redraw. Used when
@@ -337,7 +336,7 @@ def __init__(
337
336
# If `run_in_terminal` was called. This will point to a `Future` what will be
338
337
# set at the point when the previous run finishes.
339
338
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
341
340
342
341
# Trigger initialize callback.
343
342
self .reset ()
@@ -425,7 +424,7 @@ def reset(self) -> None:
425
424
426
425
self .exit_style = ""
427
426
428
- self ._background_tasks : Set [Task [None ]] = set ()
427
+ self ._background_tasks : set [Task [None ]] = set ()
429
428
430
429
self .renderer .reset ()
431
430
self .key_processor .reset ()
@@ -605,7 +604,7 @@ def _on_resize(self) -> None:
605
604
self ._request_absolute_cursor_position ()
606
605
self ._redraw ()
607
606
608
- def _pre_run (self , pre_run : Optional [ Callable [[], None ]] = None ) -> None :
607
+ def _pre_run (self , pre_run : Callable [[], None ] | None = None ) -> None :
609
608
"""
610
609
Called during `run`.
611
610
@@ -625,7 +624,7 @@ def _pre_run(self, pre_run: Optional[Callable[[], None]] = None) -> None:
625
624
626
625
async def run_async (
627
626
self ,
628
- pre_run : Optional [ Callable [[], None ]] = None ,
627
+ pre_run : Callable [[], None ] | None = None ,
629
628
set_exception_handler : bool = True ,
630
629
handle_sigint : bool = True ,
631
630
slow_callback_duration : float = 0.5 ,
@@ -670,7 +669,7 @@ async def _run_async(f: "asyncio.Future[_AppResult]") -> _AppResult:
670
669
# pressed, we start a 'flush' timer for flushing our escape key. But
671
670
# when any subsequent input is received, a new timer is started and
672
671
# the current timer will be ignored.
673
- flush_task : Optional [ asyncio .Task [None ]] = None
672
+ flush_task : asyncio .Task [None ] | None = None
674
673
675
674
# Reset.
676
675
# (`self.future` needs to be set when `pre_run` is called.)
@@ -840,7 +839,7 @@ def set_callback_duration(loop: AbstractEventLoop) -> Iterator[None]:
840
839
@contextmanager
841
840
def create_future (
842
841
loop : AbstractEventLoop ,
843
- ) -> " Iterator[asyncio.Future[_AppResult]]" :
842
+ ) -> Iterator [asyncio .Future [_AppResult ]]:
844
843
f = loop .create_future ()
845
844
self .future = f # XXX: make sure to set this before calling '_redraw'.
846
845
@@ -889,7 +888,7 @@ def create_future(
889
888
890
889
def run (
891
890
self ,
892
- pre_run : Optional [ Callable [[], None ]] = None ,
891
+ pre_run : Callable [[], None ] | None = None ,
893
892
set_exception_handler : bool = True ,
894
893
handle_sigint : bool = True ,
895
894
in_thread : bool = False ,
@@ -922,7 +921,7 @@ def run(
922
921
"""
923
922
if in_thread :
924
923
result : _AppResult
925
- exception : Optional [ BaseException ] = None
924
+ exception : BaseException | None = None
926
925
927
926
def run_in_thread () -> None :
928
927
nonlocal result , exception
@@ -953,7 +952,7 @@ def run_in_thread() -> None:
953
952
)
954
953
955
954
def _handle_exception (
956
- self , loop : AbstractEventLoop , context : Dict [str , Any ]
955
+ self , loop : AbstractEventLoop , context : dict [str , Any ]
957
956
) -> None :
958
957
"""
959
958
Handler for event loop exceptions.
@@ -1029,7 +1028,7 @@ def trace_dispatch(
1029
1028
1030
1029
def create_background_task (
1031
1030
self , coroutine : Coroutine [Any , Any , None ]
1032
- ) -> " asyncio.Task[None]" :
1031
+ ) -> asyncio .Task [None ]:
1033
1032
"""
1034
1033
Start a background task (coroutine) for the running application. When
1035
1034
the `Application` terminates, unfinished background tasks will be
@@ -1053,7 +1052,7 @@ def create_background_task(
1053
1052
task .add_done_callback (self ._on_background_task_done )
1054
1053
return task
1055
1054
1056
- def _on_background_task_done (self , task : " asyncio.Task[None]" ) -> None :
1055
+ def _on_background_task_done (self , task : asyncio .Task [None ]) -> None :
1057
1056
"""
1058
1057
Called when a background task completes. Remove it from
1059
1058
`_background_tasks`, and handle exceptions if any.
@@ -1114,7 +1113,7 @@ async def _poll_output_size(self) -> None:
1114
1113
- If we are not running in the main thread.
1115
1114
- On Windows.
1116
1115
"""
1117
- size : Optional [ Size ] = None
1116
+ size : Size | None = None
1118
1117
interval = self .terminal_size_polling_interval
1119
1118
1120
1119
if interval is None :
@@ -1153,14 +1152,14 @@ def exit(self, *, result: _AppResult, style: str = "") -> None:
1153
1152
1154
1153
@overload
1155
1154
def exit (
1156
- self , * , exception : Union [ BaseException , Type [BaseException ] ], style : str = ""
1155
+ self , * , exception : BaseException | type [BaseException ], style : str = ""
1157
1156
) -> None :
1158
1157
"Exit with exception."
1159
1158
1160
1159
def exit (
1161
1160
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 ,
1164
1163
style : str = "" ,
1165
1164
) -> None :
1166
1165
"""
@@ -1275,7 +1274,7 @@ def run() -> None:
1275
1274
run_in_terminal (run )
1276
1275
1277
1276
def print_text (
1278
- self , text : AnyFormattedText , style : Optional [ BaseStyle ] = None
1277
+ self , text : AnyFormattedText , style : BaseStyle | None = None
1279
1278
) -> None :
1280
1279
"""
1281
1280
Print a list of (style_str, text) tuples to the output.
@@ -1304,7 +1303,7 @@ def is_done(self) -> bool:
1304
1303
return self .future .done ()
1305
1304
return False
1306
1305
1307
- def get_used_style_strings (self ) -> List [str ]:
1306
+ def get_used_style_strings (self ) -> list [str ]:
1308
1307
"""
1309
1308
Return a list of used style strings. This is helpful for debugging, and
1310
1309
for writing a new `Style`.
@@ -1330,7 +1329,7 @@ class _CombinedRegistry(KeyBindingsBase):
1330
1329
def __init__ (self , app : Application [_AppResult ]) -> None :
1331
1330
self .app = app
1332
1331
self ._cache : SimpleCache [
1333
- Tuple [Window , FrozenSet [UIControl ]], KeyBindingsBase
1332
+ tuple [Window , frozenset [UIControl ]], KeyBindingsBase
1334
1333
] = SimpleCache ()
1335
1334
1336
1335
@property
@@ -1340,13 +1339,13 @@ def _version(self) -> Hashable:
1340
1339
raise NotImplementedError
1341
1340
1342
1341
@property
1343
- def bindings (self ) -> List [Binding ]:
1342
+ def bindings (self ) -> list [Binding ]:
1344
1343
"""Not needed - this object is not going to be wrapped in another
1345
1344
KeyBindings object."""
1346
1345
raise NotImplementedError
1347
1346
1348
1347
def _create_key_bindings (
1349
- self , current_window : Window , other_controls : List [UIControl ]
1348
+ self , current_window : Window , other_controls : list [UIControl ]
1350
1349
) -> KeyBindingsBase :
1351
1350
"""
1352
1351
Create a `KeyBindings` object that merges the `KeyBindings` from the
@@ -1409,10 +1408,10 @@ def _key_bindings(self) -> KeyBindingsBase:
1409
1408
key , lambda : self ._create_key_bindings (current_window , other_controls )
1410
1409
)
1411
1410
1412
- def get_bindings_for_keys (self , keys : KeysTuple ) -> List [Binding ]:
1411
+ def get_bindings_for_keys (self , keys : KeysTuple ) -> list [Binding ]:
1413
1412
return self ._key_bindings .get_bindings_for_keys (keys )
1414
1413
1415
- def get_bindings_starting_with_keys (self , keys : KeysTuple ) -> List [Binding ]:
1414
+ def get_bindings_starting_with_keys (self , keys : KeysTuple ) -> list [Binding ]:
1416
1415
return self ._key_bindings .get_bindings_starting_with_keys (keys )
1417
1416
1418
1417
0 commit comments