11from __future__ import annotations
22
3- import anyio
43import collections .abc as cabc
54import enum
65import errno
109import typing as t
1110from collections import abc
1211from collections import Counter
13- from contextlib import asynccontextmanager
1412from contextlib import AbstractContextManager
15- from contextlib import contextmanager
13+ from contextlib import asynccontextmanager
1614from contextlib import AsyncExitStack
15+ from contextlib import contextmanager
1716from functools import update_wrapper
1817from gettext import gettext as _
1918from gettext import ngettext
2019from inspect import iscoroutine
2120from itertools import repeat
2221from types import TracebackType
2322
23+ import anyio
24+
2425from . import types
2526from ._utils import FLAG_NEEDS_VALUE
2627from ._utils import UNSET
4849from .utils import PacifyFlushWrapper
4950
5051if t .TYPE_CHECKING :
52+ from collections .abc import Awaitable
53+
5154 from .shell_completion import CompletionItem
5255
5356F = t .TypeVar ("F" , bound = "t.Callable[..., t.Any]" )
@@ -455,7 +458,7 @@ def protected_args(self) -> list[str]:
455458 )
456459 return self ._protected_args
457460
458- async def to_info_dict (self ) -> t . Dict [str , t .Any ]:
461+ async def to_info_dict (self ) -> dict [str , t .Any ]:
459462 """Gather information that could be useful for a tool generating
460463 user-facing documentation. This traverses the entire CLI
461464 structure.
@@ -490,7 +493,9 @@ async def __aexit__(
490493 self ._depth -= 1
491494 exit_result : bool | None = None
492495 if self ._depth == 0 :
493- exit_result = await self ._aclose_with_exception_info (exc_type , exc_value , tb )
496+ exit_result = await self ._aclose_with_exception_info (
497+ exc_type , exc_value , tb
498+ )
494499 pop_context ()
495500 return exit_result
496501
@@ -603,7 +608,9 @@ def cli(ctx):
603608 """
604609 return self ._exit_stack .enter_context (context_manager )
605610
606- def with_async_resource (self , context_manager : t .AsyncContextManager [V ]) -> "t.Awaitable[V]" :
611+ def with_async_resource (
612+ self , context_manager : t .AsyncContextManager [V ]
613+ ) -> t .Awaitable [V ]:
607614 """Register a resource as if it were used in an ``async with``
608615 statement. The resource will be cleaned up when the context is
609616 popped.
@@ -642,11 +649,13 @@ def call_on_close(self, f: t.Callable[..., t.Any]) -> t.Callable[..., t.Any]:
642649
643650 :param f: The function to execute on teardown.
644651 """
652+
645653 async def _f ():
646654 res = f ()
647655 if iscoroutine (res ):
648656 res = await res
649657 return res
658+
650659 return self ._exit_stack .push_async_callback (_f )
651660
652661 async def aclose (self ) -> None :
@@ -669,7 +678,9 @@ async def _aclose_with_exception_info(
669678
670679 :return: Whatever ``exit_stack.__exit__()`` returns.
671680 """
672- exit_result = await t .cast (t .AsyncContextManager [t .Any ], self ._exit_stack ).__aexit__ (exc_type , exc_value , tb )
681+ exit_result = await t .cast (
682+ t .AsyncContextManager [t .Any ], self ._exit_stack
683+ ).__aexit__ (exc_type , exc_value , tb )
673684 # In case the context is reused, create a new exit stack.
674685 self ._exit_stack = AsyncExitStack ()
675686
@@ -796,14 +807,24 @@ def _make_sub_context(self, command: Command) -> Context:
796807
797808 @t .overload
798809 async def invoke (
799- self , callback : t .Callable [..., V | Awaitable [V ]], / , * args : t .Any , ** kwargs : t .Any
810+ self ,
811+ callback : t .Callable [..., V | Awaitable [V ]],
812+ / ,
813+ * args : t .Any ,
814+ ** kwargs : t .Any ,
800815 ) -> V : ...
801816
802817 @t .overload
803- async def invoke (self , callback : Command , / , * args : t .Any , ** kwargs : t .Any ) -> t .Any : ...
818+ async def invoke (
819+ self , callback : Command , / , * args : t .Any , ** kwargs : t .Any
820+ ) -> t .Any : ...
804821
805822 async def invoke (
806- self , callback : Command | t .Callable [..., V | Awaitable [V ]], / , * args : t .Any , ** kwargs : t .Any
823+ self ,
824+ callback : Command | t .Callable [..., V | Awaitable [V ]],
825+ / ,
826+ * args : t .Any ,
827+ ** kwargs : t .Any ,
807828 ) -> t .Any | V :
808829 """Invokes a command callback in exactly the way it expects. There
809830 are two ways to invoke this method:
@@ -1497,7 +1518,13 @@ async def _main_shell_completion(
14971518 rv = await shell_complete (self , ctx_args , prog_name , complete_var , instruction )
14981519 sys .exit (rv )
14991520
1500- def __call__ (self , * args : t .Any , _anyio_backend : t .Optional [str ] = None , _anyio_backend_options : t .Dict [str , t .Any ] = {}, ** kwargs : t .Any ) -> t .Any :
1521+ def __call__ (
1522+ self ,
1523+ * args : t .Any ,
1524+ _anyio_backend : str | None = None ,
1525+ _anyio_backend_options : dict [str , t .Any ] | None = None ,
1526+ ** kwargs : t .Any ,
1527+ ) -> t .Any :
15011528 """Calling the command runs it in a new :mod:`anyio` event loop.
15021529
15031530 If you are already inside an async event loop, call
@@ -1511,14 +1538,19 @@ def __call__(self, *args: t.Any, _anyio_backend: t.Optional[str] = None, _anyio_
15111538
15121539 """
15131540 main = self .main
1514- opts :t . Dict [str , t .Any ] = {}
1541+ opts : dict [str , t .Any ] = {}
15151542 if _anyio_backend :
15161543 opts ["backend" ] = _anyio_backend
15171544 if _anyio_backend_options :
15181545 opts ["backend_options" ] = _anyio_backend_options
15191546 return anyio .run (self ._main , main , args , kwargs , ** opts )
15201547
1521- async def _main (self , main :t .Callable [..., t .Awaitable [V ]], args : t .List [t .Any ], kwargs : t .Dict [str , t .Any ]) -> V :
1548+ async def _main (
1549+ self ,
1550+ main : t .Callable [..., t .Awaitable [V ]],
1551+ args : list [t .Any ],
1552+ kwargs : dict [str , t .Any ],
1553+ ) -> V :
15221554 return await main (* args , ** kwargs )
15231555
15241556
0 commit comments