From: Tamir Duberstein <tamird@gmail.com>
To: "Kernel.org Tools" <tools@kernel.org>
Cc: Konstantin Ryabitsev <konstantin@linuxfoundation.org>,
Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH b4 4/5] Provide overloads for git_run_command
Date: Fri, 25 Oct 2024 16:16:06 -0400 [thread overview]
Message-ID: <20241025-better-type-annotations-v1-4-9d7a00a8d754@gmail.com> (raw)
In-Reply-To: <20241025-better-type-annotations-v1-0-9d7a00a8d754@gmail.com>
This requires explicit use of kwargs to allow decode to be a required
parameter in the negative case (which is necessary to disambiguate the
overloads).
Callers now know for certain whether they get str or bytes.
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
src/b4/__init__.py | 37 ++++++++++++++++++++++++++-----------
1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index d1f0fc5b56693d7d234bc4aced65166244928728..e71fbe7d8ea8d4ca19228690ce7c186cc91b81ad 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -36,7 +36,7 @@ import requests
from pathlib import Path
from contextlib import contextmanager
-from typing import Optional, Tuple, Set, List, BinaryIO, Union, Sequence, Literal, Iterator, Dict, overload
+from typing import Optional, Tuple, Set, List, BinaryIO, Union, Sequence, Literal, Iterator, Dict, TypeVar, overload
from email import charset
@@ -2697,14 +2697,27 @@ def gpg_run_command(args: List[str], stdin: Optional[bytes] = None) -> Tuple[int
return _run_command(cmdargs, stdin=stdin)
+@overload
+def git_run_command(gitdir: Optional[Union[str, Path]], args: List[str], stdin: Optional[bytes] = ...,
+ *, logstderr: bool = ..., decode: Literal[False],
+ rundir: Optional[str] = ...) -> Tuple[int, bytes]:
+ ...
+
+
+@overload
+def git_run_command(gitdir: Optional[Union[str, Path]], args: List[str], stdin: Optional[bytes] = ...,
+ *, logstderr: bool = ..., decode: Literal[True] = ...,
+ rundir: Optional[str] = ...) -> Tuple[int, str]:
+ ...
+
def git_run_command(gitdir: Optional[Union[str, Path]], args: List[str], stdin: Optional[bytes] = None,
- logstderr: bool = False, decode: bool = True,
+ *, logstderr: bool = False, decode: bool = True,
rundir: Optional[str] = None) -> Tuple[int, Union[str, bytes]]:
cmdargs = ['git', '--no-pager']
if gitdir:
if os.path.exists(os.path.join(gitdir, '.git')):
gitdir = os.path.join(gitdir, '.git')
- cmdargs += ['--git-dir', gitdir]
+ cmdargs += ['--git-dir', str(gitdir)]
# counteract some potential local settings
if args[0] == 'log':
@@ -2714,16 +2727,18 @@ def git_run_command(gitdir: Optional[Union[str, Path]], args: List[str], stdin:
ecode, out, err = _run_command(cmdargs, stdin=stdin, rundir=rundir)
- if decode:
- out = out.decode(errors='replace')
+ U = TypeVar('U', str, bytes)
+
+ def _handle(out: U, err: U) -> Tuple[int, Union[str, bytes]]:
+ if logstderr and len(err.strip()):
+ logger.debug('Stderr: %s', err)
+ out += err
- if logstderr and len(err.strip()):
- if decode:
- err = err.decode(errors='replace')
- logger.debug('Stderr: %s', err)
- out += err
+ return ecode, out
- return ecode, out
+ if decode:
+ return _handle(out.decode(errors='replace'), err.decode(errors='replace'))
+ return _handle(out, err)
def git_credential_fill(gitdir: Optional[str], protocol: str, host: str, username: str) -> Optional[str]:
--
2.47.0
next prev parent reply other threads:[~2024-10-25 20:16 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-25 20:16 [PATCH b4 0/5] Resolve some static typing errors Tamir Duberstein
2024-10-25 20:16 ` [PATCH b4 1/5] Add development dependencies Tamir Duberstein
2024-10-25 20:16 ` [PATCH b4 2/5] Avoid file descriptor leak Tamir Duberstein
2024-10-25 20:16 ` [PATCH b4 3/5] Correctly type annotate generators Tamir Duberstein
2024-10-25 20:16 ` Tamir Duberstein [this message]
2024-10-25 20:16 ` [PATCH b4 5/5] Add missing imports Tamir Duberstein
2025-01-22 15:28 ` [PATCH b4 0/5] Resolve some static typing errors Konstantin Ryabitsev
2025-01-22 15:36 ` Konstantin Ryabitsev
2025-01-23 20:28 ` Tamir Duberstein
2025-02-06 17:53 ` Konstantin Ryabitsev
2025-02-06 18:23 ` Tamir Duberstein
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241025-better-type-annotations-v1-4-9d7a00a8d754@gmail.com \
--to=tamird@gmail.com \
--cc=konstantin@linuxfoundation.org \
--cc=tools@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).