Skip to content

Commit 1e229c0

Browse files
fix types
1 parent ba2b81a commit 1e229c0

6 files changed

Lines changed: 27 additions & 54 deletions

File tree

dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ setuptools>56
1919
# Debuggery
2020
icecream>=2.1
2121
# typing
22-
mypy==0.971
22+
mypy==1.10.0
2323
types-PyYAML==6.0.12.4
2424
typing-extensions>=4,<5

invoke/_types.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import (
22
IO,
33
TYPE_CHECKING,
4+
Any,
5+
Callable,
46
Union,
57
Sequence,
68
overload,
@@ -16,18 +18,11 @@
1618
from invoke.watchers import StreamWatcher
1719

1820

19-
def annotate_run_function(func: "_RunFunctionImpl") -> "RunFunction":
21+
def annotate_run_function(func: Callable[..., Any]) -> "RunFunction":
2022
"""Add standard run function annotations to a function."""
2123
return cast(RunFunction, func)
2224

2325

24-
class _RunFunctionImpl(Protocol):
25-
def __call__(
26-
self, command: str, **kwargs: Unpack["RunParams"]
27-
) -> Optional["Result"]:
28-
...
29-
30-
3126
class _BaseRunParams(TypedDict, total=False):
3227
dry: bool
3328
echo: bool

invoke/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def add_task(
266266
name = task.name
267267
# XXX https://github.com/python/mypy/issues/1424
268268
elif hasattr(task.body, "func_name"):
269-
name = task.body.func_name # type: ignore
269+
name = task.body.func_name
270270
elif hasattr(task.body, "__name__"):
271271
name = task.__name__
272272
else:

invoke/context.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ def run(self, command: str, **kwargs: Any) -> Optional[Result]:
108108
# NOTE: broken out of run() to allow for runner class injection in
109109
# Fabric/etc, which needs to juggle multiple runner class types (local and
110110
# remote).
111-
def _run(
112-
self, runner: "Runner", command: str, **kwargs: Any
113-
) -> Optional[Result]:
111+
def _run(self, runner: "Runner", command: str, **kwargs: Any) -> Optional[Result]:
114112
command = self._prefix_commands(command)
115113
return runner.run(command, **kwargs)
116114

@@ -188,9 +186,7 @@ def sudo(self, command: str, **kwargs: Any) -> Optional[Result]:
188186
return self._sudo(runner, command, **kwargs)
189187

190188
# NOTE: this is for runner injection; see NOTE above _run().
191-
def _sudo(
192-
self, runner: "Runner", command: str, **kwargs: Any
193-
) -> Optional[Result]:
189+
def _sudo(self, runner: "Runner", command: str, **kwargs: Any) -> Optional[Result]:
194190
prompt = self.config.sudo.prompt
195191
password = kwargs.pop("password", self.config.sudo.password)
196192
user = kwargs.pop("user", self.config.sudo.user)
@@ -488,9 +484,7 @@ def __init__(self, config: Optional[Config] = None, **kwargs: Any) -> None:
488484
if isinstance(results, dict):
489485
for key, value in results.items():
490486
results[key] = self._normalize(value)
491-
elif isinstance(results, singletons) or hasattr(
492-
results, "__iter__"
493-
):
487+
elif isinstance(results, singletons) or hasattr(results, "__iter__"):
494488
results = self._normalize(results)
495489
# Unknown input value: cry
496490
else:
@@ -551,23 +545,23 @@ def _yield_result(self, attname: str, command: str) -> Result:
551545
# raise_from(NotImplementedError(command), None)
552546
raise NotImplementedError(command)
553547

554-
def run(self, command: str, *args: Any, **kwargs: Any) -> Result:
548+
@annotate_run_function
549+
def run(self, command: str, **kwargs: Any) -> Result:
555550
# TODO: perform more convenience stuff associating args/kwargs with the
556551
# result? E.g. filling in .command, etc? Possibly useful for debugging
557552
# if one hits unexpected-order problems with what they passed in to
558553
# __init__.
559554
return self._yield_result("__run", command)
560555

561-
def sudo(self, command: str, *args: Any, **kwargs: Any) -> Result:
556+
@annotate_run_function
557+
def sudo(self, command: str, **kwargs: Any) -> Result:
562558
# TODO: this completely nukes the top-level behavior of sudo(), which
563559
# could be good or bad, depending. Most of the time I think it's good.
564560
# No need to supply dummy password config, etc.
565561
# TODO: see the TODO from run() re: injecting arg/kwarg values
566562
return self._yield_result("__sudo", command)
567563

568-
def set_result_for(
569-
self, attname: str, command: str, result: Result
570-
) -> None:
564+
def set_result_for(self, attname: str, command: str, result: Result) -> None:
571565
"""
572566
Modify the stored mock results for given ``attname`` (e.g. ``run``).
573567

invoke/runners.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ def _setup(self, command: str, kwargs: Any) -> None:
409409
# Normalize kwargs w/ config; sets self.opts, self.streams
410410
self._unify_kwargs_with_config(kwargs)
411411
# Environment setup
412-
self.env = self.generate_env(
413-
self.opts["env"], self.opts["replace_env"]
414-
)
412+
self.env = self.generate_env(self.opts["env"], self.opts["replace_env"])
415413
# Arrive at final encoding if neither config nor kwargs had one
416414
self.encoding = self.opts["encoding"] or self.default_encoding()
417415
# Echo running command (wants to be early to be included in dry-run)
@@ -546,7 +544,9 @@ def _unify_kwargs_with_config(self, kwargs: Any) -> None:
546544
self._asynchronous = opts["asynchronous"]
547545
self._disowned = opts["disown"]
548546
if self._asynchronous and self._disowned:
549-
err = "Cannot give both 'asynchronous' and 'disown' at the same time!" # noqa
547+
err = (
548+
"Cannot give both 'asynchronous' and 'disown' at the same time!" # noqa
549+
)
550550
raise ValueError(err)
551551
# If hide was True, turn off echoing
552552
if opts["hide"] is True:
@@ -602,9 +602,7 @@ def _collate_result(self, watcher_errors: List[WatcherError]) -> "Result":
602602
# TODO: as noted elsewhere, I kinda hate this. Consider changing
603603
# generate_result()'s API in next major rev so we can tidy up.
604604
result = self.generate_result(
605-
**dict(
606-
self.result_kwargs, stdout=stdout, stderr=stderr, exited=exited
607-
)
605+
**dict(self.result_kwargs, stdout=stdout, stderr=stderr, exited=exited)
608606
)
609607
return result
610608

@@ -755,9 +753,7 @@ def _handle_output(
755753
# Run our specific buffer through the autoresponder framework
756754
self.respond(buffer_)
757755

758-
def handle_stdout(
759-
self, buffer_: List[str], hide: bool, output: IO
760-
) -> None:
756+
def handle_stdout(self, buffer_: List[str], hide: bool, output: IO) -> None:
761757
"""
762758
Read process' stdout, storing into a buffer & printing/parsing.
763759
@@ -774,13 +770,9 @@ def handle_stdout(
774770
775771
.. versionadded:: 1.0
776772
"""
777-
self._handle_output(
778-
buffer_, hide, output, reader=self.read_proc_stdout
779-
)
773+
self._handle_output(buffer_, hide, output, reader=self.read_proc_stdout)
780774

781-
def handle_stderr(
782-
self, buffer_: List[str], hide: bool, output: IO
783-
) -> None:
775+
def handle_stderr(self, buffer_: List[str], hide: bool, output: IO) -> None:
784776
"""
785777
Read process' stderr, storing into a buffer & printing/parsing.
786778
@@ -789,9 +781,7 @@ def handle_stderr(
789781
790782
.. versionadded:: 1.0
791783
"""
792-
self._handle_output(
793-
buffer_, hide, output, reader=self.read_proc_stderr
794-
)
784+
self._handle_output(buffer_, hide, output, reader=self.read_proc_stderr)
795785

796786
def read_our_stdin(self, input_: IO) -> Optional[str]:
797787
"""
@@ -940,9 +930,7 @@ def respond(self, buffer_: List[str]) -> None:
940930
for response in watcher.submit(stream):
941931
self.write_proc_stdin(response)
942932

943-
def generate_env(
944-
self, env: Dict[str, Any], replace_env: bool
945-
) -> Dict[str, Any]:
933+
def generate_env(self, env: Dict[str, Any], replace_env: bool) -> Dict[str, Any]:
946934
"""
947935
Return a suitable environment dict based on user input & behavior.
948936
@@ -1283,9 +1271,7 @@ def _write_proc_stdin(self, data: bytes) -> None:
12831271
elif self.process and self.process.stdin:
12841272
fd = self.process.stdin.fileno()
12851273
else:
1286-
raise SubprocessPipeError(
1287-
"Unable to write to missing subprocess or stdin!"
1288-
)
1274+
raise SubprocessPipeError("Unable to write to missing subprocess or stdin!")
12891275
# Try to write, ignoring broken pipes if encountered (implies child
12901276
# process exited before the process piping stdin to us finished;
12911277
# there's nothing we can do about that!)
@@ -1303,9 +1289,7 @@ def close_proc_stdin(self) -> None:
13031289
elif self.process and self.process.stdin:
13041290
self.process.stdin.close()
13051291
else:
1306-
raise SubprocessPipeError(
1307-
"Unable to close missing subprocess or stdin!"
1308-
)
1292+
raise SubprocessPipeError("Unable to close missing subprocess or stdin!")
13091293

13101294
def start(self, command: str, shell: str, env: Dict[str, Any]) -> None:
13111295
if self.using_pty:

invoke/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def run(self) -> None:
191191
# doesn't appear to be the case, then assume we're being used
192192
# directly and just use super() ourselves.
193193
# XXX https://github.com/python/mypy/issues/1424
194-
if hasattr(self, "_run") and callable(self._run): # type: ignore
194+
if hasattr(self, "_run") and callable(self._run):
195195
# TODO: this could be:
196196
# - io worker with no 'result' (always local)
197197
# - tunnel worker, also with no 'result' (also always local)
@@ -206,7 +206,7 @@ def run(self) -> None:
206206
# and let it continue acting like a normal thread (meh)
207207
# - assume the run/sudo/etc case will use a queue inside its
208208
# worker body, orthogonal to how exception handling works
209-
self._run() # type: ignore
209+
self._run()
210210
else:
211211
super().run()
212212
except BaseException:

0 commit comments

Comments
 (0)