Skip to content

Commit 1ee81e0

Browse files
committed
Restore pre-commit to working order
1 parent 47baa7b commit 1ee81e0

14 files changed

Lines changed: 184 additions & 108 deletions

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ else
1010
@echo "Please use 'python setup.py'."
1111
@exit 1
1212
endif
13-

src/asyncclick/core.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import anyio
43
import collections.abc as cabc
54
import enum
65
import errno
@@ -10,17 +9,19 @@
109
import typing as t
1110
from collections import abc
1211
from collections import Counter
13-
from contextlib import asynccontextmanager
1412
from contextlib import AbstractContextManager
15-
from contextlib import contextmanager
13+
from contextlib import asynccontextmanager
1614
from contextlib import AsyncExitStack
15+
from contextlib import contextmanager
1716
from functools import update_wrapper
1817
from gettext import gettext as _
1918
from gettext import ngettext
2019
from inspect import iscoroutine
2120
from itertools import repeat
2221
from types import TracebackType
2322

23+
import anyio
24+
2425
from . import types
2526
from ._utils import FLAG_NEEDS_VALUE
2627
from ._utils import UNSET
@@ -48,6 +49,8 @@
4849
from .utils import PacifyFlushWrapper
4950

5051
if t.TYPE_CHECKING:
52+
from collections.abc import Awaitable
53+
5154
from .shell_completion import CompletionItem
5255

5356
F = 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

src/asyncclick/decorators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def new_func(ctx, *args, **kwargs):
7373
remembered on the context if it's not there yet.
7474
"""
7575

76-
def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]:
76+
def decorator(
77+
f: t.Callable[te.Concatenate[T, P], R],
78+
) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]:
7779
def new_func(*args: P.args, **kwargs: P.kwargs) -> R:
7880
ctx = get_current_context()
7981

@@ -112,7 +114,9 @@ def pass_meta_key(
112114
.. versionadded:: 8.0
113115
"""
114116

115-
def decorator(f: t.Callable[te.Concatenate[T, P], R]) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]:
117+
def decorator(
118+
f: t.Callable[te.Concatenate[T, P], R],
119+
) -> t.Callable[P, t.Coroutine[t.Any, t.Any, R]]:
116120
def new_func(*args: P.args, **kwargs: P.kwargs) -> t.Coroutine[t.Any, t.Any, R]:
117121
ctx = get_current_context()
118122
obj = ctx.meta[key]

src/asyncclick/shell_completion.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ def get_completion_args(self) -> tuple[list[str], str]:
268268
"""
269269
raise NotImplementedError
270270

271-
async def get_completions(self, args: list[str], incomplete: str) -> list[CompletionItem]:
271+
async def get_completions(
272+
self, args: list[str], incomplete: str
273+
) -> list[CompletionItem]:
272274
"""Determine the context and last complete command or parameter
273275
from the complete args. Call that object's ``shell_complete``
274276
method to get the completions for the incomplete value.

src/asyncclick/termui.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import anyio
43
import collections.abc as cabc
54
import inspect
65
import io
@@ -11,6 +10,8 @@
1110
from gettext import gettext as _
1211
from inspect import iscoroutine
1312

13+
import anyio
14+
1415
from ._compat import isatty
1516
from ._compat import strip_ansi
1617
from .exceptions import Abort
@@ -23,6 +24,8 @@
2324
from .utils import LazyFile
2425

2526
if t.TYPE_CHECKING:
27+
from collections.abc import Awaitable
28+
2629
from ._termui_impl import ProgressBar
2730

2831
V = t.TypeVar("V")
@@ -165,9 +168,11 @@ def prompt_func(text: str) -> str:
165168
raise Abort() from None
166169

167170
if blocking:
171+
168172
async def run_prompt_func(text: str) -> str:
169173
return prompt_func(text)
170174
else:
175+
171176
def run_prompt_func(text: str) -> Awaitable[str]:
172177
return anyio.to_thread.run_sync(prompt_func, text)
173178

src/asyncclick/testing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from .core import Command
2323

24+
2425
class EchoingStdin:
2526
def __init__(self, input: t.BinaryIO, output: t.BinaryIO) -> None:
2627
self._input = input
@@ -503,7 +504,9 @@ async def invoke(
503504
prog_name = self.get_default_prog_name(cli)
504505

505506
try:
506-
return_value = await cli.main(args=args or (), prog_name=prog_name, **extra)
507+
return_value = await cli.main(
508+
args=args or (), prog_name=prog_name, **extra
509+
)
507510
except SystemExit as e:
508511
exc_info = sys.exc_info()
509512
e_code = t.cast("int | t.Any | None", e.code)

tests/conftest.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
import pytest
2-
import anyio
3-
from functools import partial
41
from threading import Thread
52

3+
import anyio
4+
import pytest
5+
66
from asyncclick.testing import CliRunner
77

8+
89
class SyncCliRunner(CliRunner):
9-
def invoke(self,*a,_sync=False,**k):
10+
def invoke(self, *a, _sync=False, **k):
1011
fn = super().invoke
1112
if _sync:
12-
return fn(*a,**k)
13+
return fn(*a, **k)
1314

1415
# anyio now protects against nested calls, so we use a thread
1516
result = None
17+
1618
def f():
17-
nonlocal result,fn
19+
nonlocal result, fn
20+
1821
async def r():
19-
return await fn(*a,**k)
20-
result = anyio.run(r) ## , backend="trio")
21-
t=Thread(target=f, name="TEST")
22+
return await fn(*a, **k)
23+
24+
result = anyio.run(r) ## , backend="trio")
25+
26+
t = Thread(target=f, name="TEST")
2227
t.start()
2328
t.join()
2429
return result
2530

31+
2632
@pytest.fixture(scope="function")
2733
def runner(request):
2834
return SyncCliRunner()
2935

36+
3037
@pytest.fixture(scope="function")
3138
def arunner(request):
3239
return CliRunner()

tests/test_arguments.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
PY2 = False # churn
1010

11+
1112
@pytest.mark.anyio
1213
async def test_nargs_star(arunner):
1314
@click.command()
@@ -16,8 +17,8 @@ async def test_nargs_star(arunner):
1617
def copy(src, dst):
1718
click.echo(f"src={'|'.join(src)}")
1819
click.echo(f"dst={dst}")
19-
sys.stdin=sys.__stdin__
20-
sys.stdout=sys.__stdout__
20+
sys.stdin = sys.__stdin__
21+
sys.stdout = sys.__stdout__
2122

2223
result = await arunner.invoke(copy, ["foo.txt", "bar.txt", "dir"])
2324
if result.exception:

tests/test_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from contextlib import asynccontextmanager
1+
import logging
22
from contextlib import AbstractContextManager
3+
from contextlib import asynccontextmanager
34
from contextlib import contextmanager
4-
import logging
55
from types import TracebackType
66

77
import pytest

tests/test_custom_classes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import asyncclick as click
2-
31
import pytest
42

3+
import asyncclick as click
4+
55

66
@pytest.mark.anyio
77
async def test_command_context_class():

0 commit comments

Comments
 (0)