The unit test files below test/unit/python/jw/pkg/lib/ define Cmd and App subclasses with unannotated methods: __init__(), add_arguments(), _run() and close() lack signatures, the made class attribute lacks a type, and the _cleanup() and exit_context() helpers are untyped, so a strict mypy run over the test tree fails on them. Likewise, the ExecApp and version tests carry list formatting that yapf rejects. Annotate the test classes following the library conventions: parent is App | Cmd, the parser is ArgumentParser, and args is Namespace, and mark every overridden method with @override. Guard the imports that are only used for annotations behind TYPE_CHECKING, and add the future annotations import so they stay out of the runtime path. Then reformat the test tree with yapf. That folds the opts list of the ExecApp test and re-indents the rejected operator list of the version test. Signed-off-by: Jan Lindemann <jan@janware.com> Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import sys
|
|
|
|
from typing import TYPE_CHECKING, override
|
|
|
|
from jw.pkg.lib.Cmd import Cmd
|
|
from jw.pkg.lib.ExecApp import ExecApp
|
|
|
|
if TYPE_CHECKING:
|
|
from argparse import Namespace
|
|
|
|
from jw.pkg.lib.App import App
|
|
|
|
# -- A minimal root command so the app can be built without a command tree.
|
|
|
|
class RootCmd(Cmd):
|
|
|
|
def __init__(self, parent: App | Cmd) -> None:
|
|
super().__init__(parent, 'root', 'Root command')
|
|
|
|
@override
|
|
async def _run(self, args: Namespace) -> None:
|
|
pass
|
|
|
|
# -- Counts the close() calls made by App.__aexit__() when the async context
|
|
# manager exits.
|
|
|
|
class RecordingExecApp(ExecApp):
|
|
|
|
closed = 0
|
|
|
|
@override
|
|
def close(self) -> None:
|
|
RecordingExecApp.closed += 1
|
|
super().close()
|
|
|
|
# -- App.__init__ builds the parser from sys.argv, so point it at a clean
|
|
# command line while constructing the app.
|
|
|
|
saved_argv = sys.argv
|
|
sys.argv = ['jw-pkg-test']
|
|
try:
|
|
app = RecordingExecApp(description = 'ExecApp test', root = RootCmd)
|
|
finally:
|
|
sys.argv = saved_argv
|
|
|
|
# -- ExecApp registers the exec-related options on the top-level parser.
|
|
|
|
opts = [o for a in app.parser._actions for o in getattr(a, 'option_strings', ())]
|
|
for opt in ('--interactive', '--verbose', '--target'):
|
|
assert opt in opts, f'{opt} must be registered by ExecApp'
|
|
|
|
# -- Exiting the async context must close the app, through the
|
|
# App.__aexit__() that ExecApp.__aexit__() chains to.
|
|
|
|
async def exit_context() -> None:
|
|
async with app:
|
|
pass
|
|
|
|
asyncio.run(exit_context())
|
|
assert RecordingExecApp.closed == 1, 'exiting the app context must close the app'
|
|
|
|
print('All ExecApp tests passed')
|