lib: Fix silent assertitons

There are a couple of assert statements in the codebase which can make jw-pkg fail without any detail whatsoever if --backtrace is not specified, fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-25 07:45:14 +02:00
commit 3ac3aff997
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
7 changed files with 19 additions and 12 deletions

View file

@ -59,7 +59,7 @@ class CmdListRepos(Cmd): # export
'-H', 'X-GitHub-Api-Version: 2022-11-28', '-H', 'X-GitHub-Api-Version: 2022-11-28',
] ]
if password is not None: if password is not None:
assert username is not None assert username is not None, f'Assertion failed: username is empty but password isn\'t for "{args.base_url}"'
cmd_input = (f'-u {username}:{password}').encode('utf-8') cmd_input = (f'-u {username}:{password}').encode('utf-8')
curl_args.extend(['-K-']) curl_args.extend(['-K-'])
curl_args.append(f'https://api.github.com/users/{args.from_owner}/repos') curl_args.append(f'https://api.github.com/users/{args.from_owner}/repos')

View file

@ -145,7 +145,9 @@ class App: # export
except Exception as e: except Exception as e:
log(ERR, 'Failed: {}'.format(repr(e) if self.__back_trace else str(e))) log(ERR, 'Failed: {}'.format(repr(e) if self.__back_trace else str(e)))
exit_status = 1 exit_status = 1
if self.__back_trace: # AssertionErrors are programming errors, hence a programmer should
# get a chance to figure it out
if self.__back_trace or isinstance(e, AssertionError):
raise raise
finally: finally:
if pr is not None: if pr is not None:

View file

@ -48,7 +48,7 @@ class Cmd(abc.ABC): # export
if isinstance(parent, App): if isinstance(parent, App):
self.__app = parent self.__app = parent
break break
assert parent != parent.__parent assert parent != parent.__parent, f'Assertion failed: Parent mismatch'
parent = parent.__parent parent = parent.__parent
return self.__app return self.__app

View file

@ -26,8 +26,10 @@ class Distro(abc.ABC):
os_release_str: str|None=None, os_release_str: str|None=None,
default_pkg_filter: PackageFilter|None=None, default_pkg_filter: PackageFilter|None=None,
) -> None: ) -> None:
assert ec is not None if id is None:
assert id is not None raise ValueError(f'Tried to instaniate Distro without id')
if ec is None:
raise ValueError(f'Tried to instaniate Distro "{id}" without execution context')
self.__exec_context = ec self.__exec_context = ec
self.__id: str|None = None self.__id: str|None = None
self.__os_release_str: str|None = os_release_str self.__os_release_str: str|None = os_release_str

View file

@ -149,7 +149,7 @@ class ExecContext(Base):
if interactive is None: if interactive is None:
interactive = sys.stdin.isatty() interactive = sys.stdin.isatty()
self.__cmd_input = None self.__cmd_input = None
assert interactive in [ True, False ] assert interactive in [ True, False ], f'Invalid: interactive = {invalid}'
self.__interactive = interactive self.__interactive = interactive
self.__cmd_input = cmd_input if not isinstance(cmd_input, InputMode) else None self.__cmd_input = cmd_input if not isinstance(cmd_input, InputMode) else None
@ -285,7 +285,7 @@ class ExecContext(Base):
# Note that in the calls to the wrapped method, cmd_input == None can # Note that in the calls to the wrapped method, cmd_input == None can
# be returned by CallContext and is very much allowed # be returned by CallContext and is very much allowed
assert cmd_input is not None assert cmd_input is not None, 'Invalid: cmd_input is None'
ret = Result(None, None, 1) ret = Result(None, None, 1)
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, mod_env=mod_env, wd=wd, with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, mod_env=mod_env, wd=wd,
@ -379,7 +379,7 @@ class ExecContext(Base):
# Note that in the calls to the wrapped method, cmd_input == None can # Note that in the calls to the wrapped method, cmd_input == None can
# be returned by CallContext and is very much allowed # be returned by CallContext and is very much allowed
assert cmd_input is not None assert cmd_input is not None, 'Invalid: cmd_input is None'
ret = Result(None, None, 1) ret = Result(None, None, 1)
with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input, with self.CallContext(self, title=title, cmd=cmd, cmd_input=cmd_input,
@ -578,7 +578,8 @@ class ExecContext(Base):
_raise_stat_error(path, result.stderr, result.status) _raise_stat_error(path, result.stderr, result.status)
async def _chown(self, path: str, owner: str|None, group: str|None) -> None: async def _chown(self, path: str, owner: str|None, group: str|None) -> None:
assert owner is not None or group is not None if owner is None and group is None:
raise ValueError(f'Tried to chown("{path}") without owner and group')
if group is None: if group is None:
ownership = owner ownership = owner
elif owner is None: elif owner is None:

View file

@ -38,7 +38,8 @@ class FileContext(abc.ABC):
self.__in_pipe = in_pipe self.__in_pipe = in_pipe
self.__out_pipe = out_pipe self.__out_pipe = out_pipe
self.__open_count = 0 self.__open_count = 0
assert verbose_default is not None if not verbose_default in [True, False]:
raise ValueError(f'Tried to instantiate FileContext with verbose_default = "{verbose_default}"')
async def __aenter__(self): async def __aenter__(self):
await self.open() await self.open()
@ -87,7 +88,7 @@ class FileContext(abc.ABC):
if self.__open_count == 1: if self.__open_count == 1:
await self._close() await self._close()
self.__open_count -= 1 self.__open_count -= 1
assert self.__open_count >= 0 assert self.__open_count >= 0, f'Closed file context "{self.__uri}" more often than opened'
@classmethod @classmethod
def schema_from_uri(cls, uri: str) -> str: def schema_from_uri(cls, uri: str) -> str:

View file

@ -70,7 +70,8 @@ async def run_curl(args: list[str], parse_json: bool=False, wd=None, throw=None,
return ret, stderr, status return ret, stderr, status
async def run_askpass(askpass_env: list[str], key: AskpassKey, host: str|None=None, ec: ExecContext|None=None): async def run_askpass(askpass_env: list[str], key: AskpassKey, host: str|None=None, ec: ExecContext|None=None):
assert host is None # Currently unsupported if host is not None: # Currently unsupported
raise NotImplementedError(f'Tried to run askpass with host "{host}"')
for var in askpass_env: for var in askpass_env:
exe = os.getenv(var) exe = os.getenv(var)
if exe is None: if exe is None: