Skip to content

Commit 60d9b51

Browse files
Add proper typing to cli directory (All-Hands-AI#8374)
Co-authored-by: openhands <[email protected]>
1 parent 5ad11e7 commit 60d9b51

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

openhands/cli/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ def on_event(event: Event) -> None:
273273

274274
async def main(loop: asyncio.AbstractEventLoop) -> None:
275275
"""Runs the agent in CLI mode."""
276-
277276
args = parse_arguments()
278277

279278
logger.setLevel(logging.WARNING)

openhands/cli/tui.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from prompt_toolkit.formatted_text import HTML, FormattedText, StyleAndTextTuples
1616
from prompt_toolkit.input import create_input
1717
from prompt_toolkit.key_binding import KeyBindings
18+
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
1819
from prompt_toolkit.keys import Keys
1920
from prompt_toolkit.layout.containers import HSplit, Window
2021
from prompt_toolkit.layout.controls import FormattedTextControl
@@ -70,15 +71,15 @@
7071

7172

7273
class UsageMetrics:
73-
def __init__(self):
74+
def __init__(self) -> None:
7475
self.metrics: Metrics = Metrics()
7576
self.session_init_time: float = time.time()
7677

7778

7879
class CustomDiffLexer(Lexer):
7980
"""Custom lexer for the specific diff format."""
8081

81-
def lex_document(self, document) -> StyleAndTextTuples:
82+
def lex_document(self, document: Document) -> StyleAndTextTuples:
8283
lines = document.lines
8384

8485
def get_line(lineno: int) -> StyleAndTextTuples:
@@ -427,7 +428,7 @@ def display_agent_state_change_message(agent_state: str) -> None:
427428
class CommandCompleter(Completer):
428429
"""Custom completer for commands."""
429430

430-
def __init__(self, agent_state: str):
431+
def __init__(self, agent_state: str) -> None:
431432
super().__init__()
432433
self.agent_state = agent_state
433434

@@ -450,7 +451,7 @@ def get_completions(
450451
)
451452

452453

453-
def create_prompt_session() -> PromptSession:
454+
def create_prompt_session() -> PromptSession[str]:
454455
return PromptSession(style=DEFAULT_STYLE)
455456

456457

@@ -465,7 +466,7 @@ async def read_prompt_input(agent_state: str, multiline: bool = False) -> str:
465466
kb = KeyBindings()
466467

467468
@kb.add('c-d')
468-
def _(event) -> None:
469+
def _(event: KeyPressEvent) -> None:
469470
event.current_buffer.validate_and_handle()
470471

471472
with patch_stdout():
@@ -538,11 +539,10 @@ def keys_ready() -> None:
538539
def cli_confirm(
539540
question: str = 'Are you sure?', choices: list[str] | None = None
540541
) -> int:
541-
"""
542-
Display a confirmation prompt with the given question and choices.
542+
"""Display a confirmation prompt with the given question and choices.
543+
543544
Returns the index of the selected choice.
544545
"""
545-
546546
if choices is None:
547547
choices = ['Yes', 'No']
548548
selected = [0] # Using list to allow modification in closure
@@ -561,15 +561,15 @@ def get_choice_text() -> list:
561561
kb = KeyBindings()
562562

563563
@kb.add('up')
564-
def _(event) -> None:
564+
def _(event: KeyPressEvent) -> None:
565565
selected[0] = (selected[0] - 1) % len(choices)
566566

567567
@kb.add('down')
568-
def _(event) -> None:
568+
def _(event: KeyPressEvent) -> None:
569569
selected[0] = (selected[0] + 1) % len(choices)
570570

571571
@kb.add('enter')
572-
def _(event) -> None:
572+
def _(event: KeyPressEvent) -> None:
573573
event.app.exit(result=selected[0])
574574

575575
style = Style.from_dict({'selected': COLOR_GOLD, 'unselected': ''})
@@ -601,7 +601,7 @@ def kb_cancel() -> KeyBindings:
601601
bindings = KeyBindings()
602602

603603
@bindings.add('escape')
604-
def _(event) -> None:
604+
def _(event: KeyPressEvent) -> None:
605605
event.app.exit(exception=UserCancelledError, style='class:aborting')
606606

607607
return bindings

openhands/cli/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def __getitem__(self, key: str) -> str:
7878

7979

8080
def extract_model_and_provider(model: str) -> ModelInfo:
81-
"""
82-
Extract provider and model information from a model identifier.
81+
"""Extract provider and model information from a model identifier.
8382
8483
Args:
8584
model: The model identifier string
@@ -114,8 +113,7 @@ def extract_model_and_provider(model: str) -> ModelInfo:
114113
def organize_models_and_providers(
115114
models: list[str],
116115
) -> dict[str, 'ProviderInfo']:
117-
"""
118-
Organize a list of model identifiers by provider.
116+
"""Organize a list of model identifiers by provider.
119117
120118
Args:
121119
models: List of model identifiers
@@ -188,7 +186,7 @@ def __getitem__(self, key: str) -> str | list[str]:
188186
return self.models
189187
raise KeyError(f'ProviderInfo has no key {key}')
190188

191-
def get(self, key: str, default=None) -> str | list[str] | None:
189+
def get(self, key: str, default: None = None) -> str | list[str] | None:
192190
"""Dictionary-like get method with default value."""
193191
try:
194192
return self[key]

0 commit comments

Comments
 (0)