Skip to content

Commit 3b35984

Browse files
committed
chore: Add Protocol typing for callback
1 parent 9827748 commit 3b35984

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

libvcs/cmd/core.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import subprocess
77
import sys
8-
from typing import Callable, Optional, Union
8+
from typing import Optional, Protocol, Union
99

1010
from .. import exc
1111
from ..types import StrOrBytesPath
@@ -127,13 +127,21 @@ def process(self, msg, kwargs):
127127
return msg, kwargs
128128

129129

130+
class ProgressCallbackProtocol(Protocol):
131+
"""Callback to report subprocess communication."""
132+
133+
def __call__(self, output: Union[str, bytes], timestamp: datetime.datetime):
134+
"""Callback signature for subprocess communication."""
135+
...
136+
137+
130138
def run(
131139
cmd: Union[str, list[str]],
132140
shell: bool = False,
133141
cwd: Optional[StrOrBytesPath] = None,
134142
log_in_real_time: bool = True,
135143
check_returncode: bool = True,
136-
callback: Union[Callable[[str, datetime.datetime], None], None] = None,
144+
callback: Optional[ProgressCallbackProtocol] = None,
137145
):
138146
"""Run 'cmd' in a shell and return the combined contents of stdout and
139147
stderr (Blocking). Throws an exception if the command exits non-zero.
@@ -161,7 +169,7 @@ def run(
161169
Indicate whether a `libvcs.exc.CommandError` should be raised if return code is
162170
different from 0.
163171
164-
callback : callable
172+
callback : ProgressCallbackProtocol
165173
callback to return output as a command executes, accepts a function signature
166174
of `(output, timestamp)`. Example usage::
167175

0 commit comments

Comments
 (0)