refactor: enforce BasedPyright strict type checking (#5158)

This commit is contained in:
chengyongru authored and GitHub committed 2026-07-29 21:37:11 +08:00
1 parent e703481755
commit 757ad9c764
166 files changed
+4728 -2621

No files matched your search

+116 -69
View File
@@ -1,19 +1,27 @@
"""Interactive onboarding questionnaire for nanobot."""
# pyright: reportMissingTypeStubs=false, reportUnusedFunction=false
import asyncio
import json
import types
from collections.abc import Callable, Iterable, Sized
from contextlib import suppress
from dataclasses import dataclass
from functools import lru_cache
from typing import Any, Literal, NamedTuple, get_args, get_origin
from typing import Any, Literal, NamedTuple, TypeVar, cast, get_args, get_origin
try:
import questionary
except ModuleNotFoundError: # pragma: no cover - exercised in environments without wizard deps
questionary = None
from loguru import logger
from prompt_toolkit.completion import CompleteEvent, Completer, Completion
from prompt_toolkit.document import Document
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from pydantic import BaseModel
from pydantic.fields import FieldInfo
from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
@@ -29,6 +37,8 @@ from nanobot.config.schema import Config, ModelPresetConfig
console = Console()
_ModelT = TypeVar("_ModelT", bound=BaseModel)
@dataclass
class OnboardResult:
@@ -119,14 +129,14 @@ _CHANNEL_LOGIN_CHOICE = "Login with QR/link"
_CHANNEL_ADVANCED_CHOICE = "Edit advanced settings"
def _get_questionary():
def _get_questionary() -> Any:
"""Return questionary or raise a clear error when wizard deps are unavailable."""
if questionary is None:
raise RuntimeError(
"Interactive onboarding requires the optional 'questionary' dependency. "
"Install project dependencies and rerun with --wizard."
)
return questionary
return cast(Any, questionary)
def _select_with_back(
@@ -147,7 +157,6 @@ def _select_with_back(
import shutil
from prompt_toolkit.application import Application
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys
from prompt_toolkit.layout import Layout
from prompt_toolkit.layout.containers import HSplit, Window
@@ -170,8 +179,8 @@ def _select_with_back(
visible_count = min(len(choices), max(1, terminal_lines - 3))
# Build menu items (uses closure over selected_index)
def get_menu_text():
items = []
def get_menu_text() -> list[tuple[str, str]]:
items: list[tuple[str, str]] = []
start, end = _choice_viewport(selected_index, len(choices), visible_count)
for i in range(start, end):
choice = choices[i]
@@ -182,14 +191,14 @@ def _select_with_back(
return items
# Create layout
menu_control = FormattedTextControl(get_menu_text, show_cursor=False)
menu_control = FormattedTextControl(cast(Any, get_menu_text), show_cursor=False)
menu_window = Window(content=menu_control, height=visible_count, always_hide_cursor=True)
def get_prompt_text():
def get_prompt_text() -> list[tuple[str, str]]:
suffix = f" ({selected_index + 1}/{len(choices)})" if len(choices) > visible_count else ""
return [("class:question", f"{prompt}{suffix}")]
prompt_control = FormattedTextControl(get_prompt_text, show_cursor=False)
prompt_control = FormattedTextControl(cast(Any, get_prompt_text), show_cursor=False)
prompt_window = Window(content=prompt_control, height=1, always_hide_cursor=True)
layout = Layout(HSplit([prompt_window, menu_window]))
@@ -198,34 +207,34 @@ def _select_with_back(
bindings = KeyBindings()
@bindings.add(Keys.Up)
def _up(event):
def _up(event: KeyPressEvent) -> None:
nonlocal selected_index
selected_index = (selected_index - 1) % len(choices)
event.app.invalidate()
@bindings.add(Keys.Down)
def _down(event):
def _down(event: KeyPressEvent) -> None:
nonlocal selected_index
selected_index = (selected_index + 1) % len(choices)
event.app.invalidate()
@bindings.add(Keys.Enter)
def _enter(event):
def _enter(event: KeyPressEvent) -> None:
state["result"] = choices[selected_index]
event.app.exit()
@bindings.add("escape")
def _escape(event):
def _escape(event: KeyPressEvent) -> None:
state["result"] = _BACK_PRESSED
event.app.exit()
@bindings.add(Keys.Left)
def _left(event):
def _left(event: KeyPressEvent) -> None:
state["result"] = _BACK_PRESSED
event.app.exit()
@bindings.add(Keys.ControlC)
def _ctrl_c(event):
def _ctrl_c(event: KeyPressEvent) -> None:
state["result"] = None
event.app.exit()
@@ -235,7 +244,7 @@ def _select_with_back(
"question": f"fg:{_UI_TEXT}",
})
app = Application(layout=layout, key_bindings=bindings, style=style)
app = Application[object](layout=layout, key_bindings=bindings, style=style)
app.ttimeoutlen = 0.05
app.timeoutlen = 0.05
try:
@@ -268,7 +277,7 @@ class FieldTypeInfo(NamedTuple):
inner_type: Any
def _get_field_type_info(field_info) -> FieldTypeInfo:
def _get_field_type_info(field_info: FieldInfo) -> FieldTypeInfo:
"""Extract field type info from Pydantic field."""
annotation = field_info.annotation
if annotation is None:
@@ -285,10 +294,11 @@ def _get_field_type_info(field_info) -> FieldTypeInfo:
args = get_args(annotation)
_simple_types: dict[type, str] = {bool: "bool", int: "int", float: "float"}
origin_name = getattr(origin, "__name__", None)
if origin is list or (hasattr(origin, "__name__") and origin.__name__ == "List"):
if origin is list or origin_name == "List":
return FieldTypeInfo("list", args[0] if args else str)
if origin is dict or (hasattr(origin, "__name__") and origin.__name__ == "Dict"):
if origin is dict or origin_name == "Dict":
return FieldTypeInfo("dict", None)
for py_type, name in _simple_types.items():
if annotation is py_type:
@@ -300,7 +310,7 @@ def _get_field_type_info(field_info) -> FieldTypeInfo:
return FieldTypeInfo("str", None)
def _get_field_display_name(field_key: str, field_info) -> str:
def _get_field_display_name(field_key: str, field_info: FieldInfo | None) -> str:
"""Get display name for a field."""
if field_info and field_info.description:
return field_info.description
@@ -349,22 +359,30 @@ def _format_value(value: Any, rich: bool = True, field_name: str = "") -> str:
masked = _mask_value(value)
return f"[dim]{masked}[/dim]" if rich else masked
if isinstance(value, BaseModel):
parts = []
model_parts: list[str] = []
for fname, _finfo in type(value).model_fields.items():
fval = getattr(value, fname, None)
formatted = _format_value(fval, rich=False, field_name=fname)
if formatted != "[not set]":
parts.append(f"{fname}={formatted}")
return ", ".join(parts) if parts else ("[dim]not set[/dim]" if rich else "[not set]")
model_parts.append(f"{fname}={formatted}")
return (
", ".join(model_parts)
if model_parts
else ("[dim]not set[/dim]" if rich else "[not set]")
)
if isinstance(value, list):
return ", ".join(str(v) for v in value)
return ", ".join(str(v) for v in cast(list[Any], value))
if isinstance(value, dict):
# Handle dicts containing BaseModel instances
parts = []
for k, v in value.items():
mapping_parts: list[str] = []
for k, v in cast(dict[Any, Any], value).items():
formatted = _format_value(v, rich=False, field_name=str(k))
parts.append(f"{k}: {formatted}")
return ", ".join(parts) if parts else ("[dim]not set[/dim]" if rich else "[not set]")
mapping_parts.append(f"{k}: {formatted}")
return (
", ".join(mapping_parts)
if mapping_parts
else ("[dim]not set[/dim]" if rich else "[not set]")
)
return str(value)
@@ -373,13 +391,13 @@ def _format_value_for_input(value: Any, field_type: str) -> str:
if value is None or value == "":
return ""
if field_type == "list" and isinstance(value, list):
return ",".join(str(v) for v in value)
return ",".join(str(v) for v in cast(list[Any], value))
if field_type == "dict" and isinstance(value, dict):
return json.dumps(value)
return str(value)
def _validate_field_constraint(value: Any, field_info) -> str | None:
def _validate_field_constraint(value: Any, field_info: FieldInfo | None) -> str | None:
"""Validate a value against Pydantic Field constraints.
Returns an error message string if validation fails, None if valid.
@@ -388,7 +406,8 @@ def _validate_field_constraint(value: Any, field_info) -> str | None:
if field_info is None or not hasattr(field_info, "metadata"):
return None
for m in field_info.metadata:
for metadata in field_info.metadata:
m = metadata
if hasattr(m, "ge") and isinstance(value, (int, float)):
if value < m.ge:
return f"Value must be >= {m.ge}"
@@ -402,16 +421,16 @@ def _validate_field_constraint(value: Any, field_info) -> str | None:
if value >= m.lt:
return f"Value must be < {m.lt}"
if hasattr(m, "min_length") and hasattr(value, "__len__"):
if len(value) < m.min_length:
if len(cast(Sized, value)) < m.min_length:
return f"Length must be >= {m.min_length}"
if hasattr(m, "max_length") and hasattr(value, "__len__"):
if len(value) > m.max_length:
if len(cast(Sized, value)) > m.max_length:
return f"Length must be <= {m.max_length}"
return None
def _get_constraint_hint(field_info) -> str:
def _get_constraint_hint(field_info: FieldInfo | None) -> str:
"""Derive a human-readable constraint hint from field metadata.
Returns a string like " - 0-10" or " - >= 0" to append to field display names.
@@ -421,7 +440,8 @@ def _get_constraint_hint(field_info) -> str:
ge_val = None
le_val = None
for m in field_info.metadata:
for metadata in field_info.metadata:
m = metadata
if hasattr(m, "ge"):
ge_val = m.ge
if hasattr(m, "le"):
@@ -439,7 +459,11 @@ def _get_constraint_hint(field_info) -> str:
# --- Rich UI Components ---
def _show_config_panel(display_name: str, model: BaseModel, fields: list) -> None:
def _show_config_panel(
display_name: str,
model: BaseModel,
fields: list[tuple[str, FieldInfo]],
) -> None:
"""Display current configuration as a rich table."""
table = Table(show_header=False, box=None, padding=(0, 2))
table.add_column("Field", style=_UI_ACCENT)
@@ -504,20 +528,18 @@ def _input_bool(display_name: str, current: bool | None) -> bool | None:
).ask()
def _input_back_key_bindings():
def _input_back_key_bindings() -> KeyBindings:
"""Return key bindings that make Escape behave like a local back action."""
from prompt_toolkit.key_binding import KeyBindings
bindings = KeyBindings()
@bindings.add("escape")
def _escape(event):
def _escape(event: KeyPressEvent) -> None:
event.app.exit(result=_BACK_PRESSED)
return bindings
def _ask_prompt(prompt):
def _ask_prompt(prompt: Any) -> Any:
"""Ask a questionary prompt with responsive Escape handling."""
app = getattr(prompt, "application", None)
if app is not None:
@@ -528,7 +550,12 @@ def _ask_prompt(prompt):
return prompt.ask()
def _input_text(display_name: str, current: Any, field_type: str, field_info=None) -> Any:
def _input_text(
display_name: str,
current: Any,
field_type: str,
field_info: FieldInfo | None = None,
) -> Any:
"""Get text input and parse based on field type."""
default = _format_value_for_input(current, field_type)
@@ -591,7 +618,10 @@ def _input_secret(display_name: str) -> str | None | object:
def _input_with_existing(
display_name: str, current: Any, field_type: str, field_info=None
display_name: str,
current: Any,
field_type: str,
field_info: FieldInfo | None = None,
) -> Any:
"""Handle input with 'keep existing' option for non-empty values."""
has_existing = current is not None and current != "" and current != {} and current != []
@@ -624,8 +654,6 @@ def _input_model_with_autocomplete(
"""Get model input with autocomplete suggestions.
"""
from prompt_toolkit.completion import Completer, Completion
default = str(current) if current else ""
class DynamicModelCompleter(Completer):
@@ -634,7 +662,12 @@ def _input_model_with_autocomplete(
def __init__(self, provider_name: str):
self.provider = provider_name
def get_completions(self, document, _complete_event):
def get_completions(
self,
document: Document,
complete_event: CompleteEvent,
) -> Iterable[Completion]:
_ = complete_event
text = document.text_before_cursor
suggestions = get_model_suggestions(text, provider=self.provider, limit=50)
for model in suggestions:
@@ -735,7 +768,7 @@ def _handle_model_field(
return
if new_value is not None and new_value != current_value:
setattr(working_model, field_name, new_value)
_try_auto_fill_context_window(working_model, new_value)
_try_auto_fill_context_window(working_model, cast(str, new_value))
def _handle_context_window_field(
@@ -794,7 +827,11 @@ def _handle_fallback_models_field(
"""Handle the 'fallback_models' field with preset-aware list management."""
from nanobot.config.schema import InlineFallbackConfig
items: list[Any] = list(current_value) if isinstance(current_value, list) else []
items: list[Any] = (
list(cast(list[Any], current_value))
if isinstance(current_value, list)
else []
)
preset_names = sorted(_MODEL_PRESET_CACHE)
while True:
@@ -888,11 +925,11 @@ def _is_str_or_none(annotation: Any) -> bool:
def _configure_pydantic_model(
model: BaseModel,
model: _ModelT,
display_name: str,
*,
skip_fields: set[str] | None = None,
) -> BaseModel | None:
) -> _ModelT | None:
"""Configure a Pydantic model interactively.
Returns the updated model when the user selects "Done" or navigates back.
@@ -901,7 +938,7 @@ def _configure_pydantic_model(
skip_fields = skip_fields or set()
working_model = model.model_copy(deep=True)
fields = [
fields: list[tuple[str, FieldInfo]] = [
(name, info)
for name, info in type(working_model).model_fields.items()
if name not in skip_fields
@@ -911,7 +948,7 @@ def _configure_pydantic_model(
return working_model
def get_choices() -> list[str]:
items = []
items: list[str] = []
for fname, finfo in fields:
value = getattr(working_model, fname, None)
display = _get_field_display_name(fname, finfo)
@@ -1057,6 +1094,10 @@ def _sync_preset_cache(config: Config) -> None:
_MODEL_PRESET_CACHE.update(config.model_presets.keys())
def _validate_nonempty_name(text: str) -> bool | str:
return True if text and text.strip() else "Name cannot be empty"
def _configure_model_presets(config: Config) -> None:
"""Configure model presets (CRUD)."""
_sync_preset_cache(config)
@@ -1099,7 +1140,7 @@ def _configure_model_presets(config: Config) -> None:
if answer == "[+] Add new preset":
name_input = _get_questionary().text(
"Preset name:",
validate=lambda t: True if t and t.strip() else "Name cannot be empty",
validate=_validate_nonempty_name,
).ask()
if not name_input:
continue
@@ -1218,7 +1259,7 @@ def _configure_providers(config: Config) -> None:
def get_provider_choices() -> list[str]:
"""Build provider choices with config status indicators."""
choices = []
choices: list[str] = []
for name, display in _get_provider_names().items():
provider = getattr(config.providers, name, None)
if provider and provider.api_key:
@@ -1427,7 +1468,7 @@ _SETTINGS_SECTIONS: dict[str, tuple[str, str, set[str] | None]] = {
"Tools": ("Tools Settings", "Configure web search, shell exec, and other tools", {"mcp_servers"}),
}
_SETTINGS_GETTER = {
_SETTINGS_GETTER: dict[str, Callable[[Config], BaseModel]] = {
"Agent Settings": lambda c: c.agents.defaults,
"Channel Common": lambda c: c.channels,
"API Server": lambda c: c.api,
@@ -1435,7 +1476,7 @@ _SETTINGS_GETTER = {
"Tools": lambda c: c.tools,
}
_SETTINGS_SETTER = {
_SETTINGS_SETTER: dict[str, Callable[[Config, BaseModel], None]] = {
"Agent Settings": lambda c, v: setattr(c.agents, "defaults", v),
"Channel Common": lambda c, v: setattr(c, "channels", v),
"API Server": lambda c, v: setattr(c, "api", v),
@@ -1449,7 +1490,7 @@ def _configure_general_settings(config: Config, section: str) -> None:
meta = _SETTINGS_SECTIONS.get(section)
if not meta:
return
display_name, subtitle, skip = meta
display_name, _subtitle, skip = meta
model = _SETTINGS_GETTER[section](config)
updated = _configure_pydantic_model(model, display_name, skip_fields=skip)
if updated is not None:
@@ -1495,7 +1536,7 @@ def _show_summary(config: Config) -> None:
console.print()
# Providers
provider_rows = []
provider_rows: list[tuple[str, str]] = []
for name, display in _get_provider_names().items():
provider = getattr(config.providers, name, None)
status = (
@@ -1507,12 +1548,12 @@ def _show_summary(config: Config) -> None:
_print_summary_panel(provider_rows, "LLM Providers")
# Channels
channel_rows = []
channel_rows: list[tuple[str, str]] = []
for name, display in _get_channel_names().items():
channel = getattr(config.channels, name, None)
if channel:
enabled = (
channel.get("enabled", False)
cast(dict[str, Any], channel).get("enabled", False)
if isinstance(channel, dict)
else getattr(channel, "enabled", False)
)
@@ -1523,7 +1564,7 @@ def _show_summary(config: Config) -> None:
_print_summary_panel(channel_rows, "Chat Channels")
# Model Presets
preset_rows = []
preset_rows: list[tuple[str, str]] = []
for name, preset in config.model_presets.items():
preset_rows.append((name, f"{preset.model} - ctx {preset.context_window_tokens}"))
_print_summary_panel(preset_rows, "Model Presets")
@@ -1562,7 +1603,7 @@ def _set_primary_quick_start_preset(config: Config, provider_name: str, model: s
def _show_quick_start_progress(active_step: int) -> None:
"""Render a compact step tracker for Quick Start."""
parts = []
parts: list[str] = []
for idx, label in enumerate(_QUICK_START_STEPS, 1):
if idx < active_step:
parts.append(f"[{_UI_SUCCESS}]{idx}. {label}[/]")
@@ -1755,7 +1796,10 @@ def _configure_quick_start_provider(config: Config) -> bool | object:
continue
if api_base_result is None:
return False
api_base, base_was_prompted = api_base_result
api_base, base_was_prompted = cast(
tuple[str, bool],
api_base_result,
)
api_key: str | None = None
if _quick_start_requires_api_key(provider_name, provider_info):
@@ -1778,7 +1822,10 @@ def _configure_quick_start_provider(config: Config) -> bool | object:
continue
if api_base_result is None:
return False
api_base, base_was_prompted = api_base_result
api_base, base_was_prompted = cast(
tuple[str, bool],
api_base_result,
)
provider_config = getattr(config.providers, provider_name, None)
if provider_config is None:
@@ -1792,7 +1839,7 @@ def _configure_quick_start_provider(config: Config) -> bool | object:
)
if model is _BACK_PRESSED:
continue
model = (model or "").strip()
model = cast(str, model or "").strip()
if not model:
console.print("[yellow]! Model ID is required for Quick Start[/yellow]")
return False
@@ -1850,7 +1897,7 @@ def _enable_quick_start_websocket_defaults(config: Config) -> bool:
console.print("[red]No configuration class found for websocket[/red]")
return False
current = getattr(config.channels, "websocket", None) or {}
current: Any = getattr(config.channels, "websocket", None) or {}
model = config_cls.model_validate(current)
if hasattr(model, "enabled"):
setattr(model, "enabled", True)
@@ -1997,7 +2044,7 @@ def _configure_advanced_settings(config: Config) -> None:
if answer is _BACK_PRESSED or answer is None or answer == "<- Back":
break
_advanced_dispatch = {
_advanced_dispatch: dict[str, Callable[[], None]] = {
"[P] LLM Provider": lambda: _configure_providers(config),
"[M] Model Presets": lambda: _configure_model_presets(config),
"[C] Chat Channel": lambda: _configure_channels(config),
@@ -2008,9 +2055,9 @@ def _configure_advanced_settings(config: Config) -> None:
"[T] Tools": lambda: _configure_general_settings(config, "Tools"),
"[V] View Configuration Summary": lambda: _show_summary(config),
}
action_fn = _advanced_dispatch.get(answer)
action_fn = _advanced_dispatch.get(cast(str, answer))
if action_fn:
last_choice = answer
last_choice = cast(str, answer)
action_fn()