fix(cli): move custom Field kwargs to json_schema_extra for mypy compatibility (#7362)

Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
This commit is contained in:
John Seong
2026-02-17 18:36:20 -08:00
committed by GitHub
parent d3537dd155
commit cf6a7aa970
11 changed files with 104 additions and 65 deletions

View File

@@ -236,7 +236,7 @@ class PlatformController(BaseController):
if not export:
obbject.show()
elif session.settings.USE_INTERACTIVE_DF and not export:
obbject.charting.table()
obbject.charting.table() # type: ignore[attr-defined]
else:
if isinstance(df.columns, pd.RangeIndex):
df.columns = [str(i) for i in df.columns]

View File

@@ -3,7 +3,7 @@
import argparse
from functools import partial, update_wrapper
from types import MethodType
from typing import Literal, get_origin
from typing import Any, Literal, cast, get_origin
from openbb_cli.config.menu_text import MenuText
from openbb_cli.controllers.base_controller import BaseController
@@ -13,20 +13,26 @@ from openbb_cli.session import Session
session = Session()
def _get_extra(field: Any) -> dict[str, Any]:
"""Safely extract json_schema_extra as a dict."""
extra = field.json_schema_extra
return cast(dict[str, Any], extra) if isinstance(extra, dict) else {}
class SettingsController(BaseController):
"""Settings Controller class."""
_COMMANDS = {
v.json_schema_extra.get("command"): {
"command": (v.json_schema_extra or {}).get("command"),
"group": (v.json_schema_extra or {}).get("group"),
_COMMANDS: dict[str, dict[str, Any]] = {
_get_extra(v).get("command", ""): {
"command": _get_extra(v).get("command"),
"group": _get_extra(v).get("group"),
"description": v.description,
"annotation": v.annotation,
"field_name": k,
}
for k, v in sorted(
session.settings.model_fields.items(),
key=lambda item: (item[1].json_schema_extra or {}).get("command", ""),
key=lambda item: _get_extra(item[1]).get("command", ""),
)
if v.json_schema_extra
}
@@ -39,9 +45,9 @@ class SettingsController(BaseController):
super().__init__(queue)
for cmd, field in self._COMMANDS.items():
group = field.get("group")
if group == SettingGroups.feature_flags:
if group == SettingGroups.feature_flags.value:
self._generate_command(cmd, field, "toggle")
elif group == SettingGroups.preferences:
elif group == SettingGroups.preferences.value:
self._generate_command(cmd, field, "set")
self.update_completer(self.choices_default)
@@ -50,7 +56,7 @@ class SettingsController(BaseController):
mt = MenuText("settings/")
mt.add_info("Feature Flags")
for k, f in self._COMMANDS.items():
if f.get("group") == SettingGroups.feature_flags:
if f.get("group") == SettingGroups.feature_flags.value:
mt.add_setting(
name=k,
status=getattr(session.settings, f["field_name"]),
@@ -59,7 +65,7 @@ class SettingsController(BaseController):
mt.add_raw("\n")
mt.add_info("Preferences")
for k, f in self._COMMANDS.items():
if f.get("group") == SettingGroups.preferences:
if f.get("group") == SettingGroups.preferences.value:
mt.add_cmd(
name=k,
description=f["description"],

View File

@@ -41,106 +41,140 @@ class Settings(BaseModel):
FILE_OVERWRITE: bool = Field(
default=False,
description="whether to overwrite Excel files if they already exists",
command="overwrite",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "overwrite",
"group": SettingGroups.feature_flags.value,
},
)
SHOW_VERSION: bool = Field(
default=True,
description="whether to show the version in the bottom right corner",
command="version",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "version",
"group": SettingGroups.feature_flags.value,
},
)
USE_INTERACTIVE_DF: bool = Field(
default=True,
description="display tables in interactive window",
command="interactive",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "interactive",
"group": SettingGroups.feature_flags.value,
},
)
USE_CLEAR_AFTER_CMD: bool = Field(
default=False,
description="clear console after each command",
command="cls",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "cls",
"group": SettingGroups.feature_flags.value,
},
)
USE_DATETIME: bool = Field(
default=True,
description="whether to show the date and time before the flair",
command="datetime",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "datetime",
"group": SettingGroups.feature_flags.value,
},
)
USE_PROMPT_TOOLKIT: bool = Field(
default=True,
description="enable prompt toolkit (autocomplete and history)",
command="promptkit",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "promptkit",
"group": SettingGroups.feature_flags.value,
},
)
ENABLE_EXIT_AUTO_HELP: bool = Field(
default=True,
description="automatically print help when quitting menu",
command="exithelp",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "exithelp",
"group": SettingGroups.feature_flags.value,
},
)
ENABLE_RICH_PANEL: bool = Field(
default=True,
description="enable colorful rich CLI panel",
command="richpanel",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "richpanel",
"group": SettingGroups.feature_flags.value,
},
)
TOOLBAR_HINT: bool = Field(
default=True,
description="displays usage hints in the bottom toolbar",
command="tbhint",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "tbhint",
"group": SettingGroups.feature_flags.value,
},
)
SHOW_MSG_OBBJECT_REGISTRY: bool = Field(
default=False,
description="show obbject registry message after a new result is added",
command="obbject_msg",
group=SettingGroups.feature_flags,
json_schema_extra={
"command": "obbject_msg",
"group": SettingGroups.feature_flags.value,
},
)
# PREFERENCES
TIMEZONE: Literal[tuple(all_timezones)] = Field( # type: ignore[valid-type]
default="America/New_York",
description="pick timezone",
command="timezone",
group=SettingGroups.preferences,
json_schema_extra={
"command": "timezone",
"group": SettingGroups.preferences.value,
},
)
FLAIR: Literal[tuple(AVAILABLE_FLAIRS)] = Field( # type: ignore[valid-type]
default=":openbb",
description="choose flair icon",
command="flair",
group=SettingGroups.preferences,
json_schema_extra={
"command": "flair",
"group": SettingGroups.preferences.value,
},
)
N_TO_KEEP_OBBJECT_REGISTRY: int = Field(
default=10,
description="define the maximum number of obbjects allowed in the registry",
command="obbject_res",
group=SettingGroups.preferences,
json_schema_extra={
"command": "obbject_res",
"group": SettingGroups.preferences.value,
},
)
N_TO_DISPLAY_OBBJECT_REGISTRY: int = Field(
default=5,
description="define the maximum number of cached results to display on the help menu",
command="obbject_display",
group=SettingGroups.preferences,
json_schema_extra={
"command": "obbject_display",
"group": SettingGroups.preferences.value,
},
)
RICH_STYLE: str = Field(
default="dark",
description="apply a custom rich style to the CLI",
command="console_style",
group=SettingGroups.preferences,
json_schema_extra={
"command": "console_style",
"group": SettingGroups.preferences.value,
},
)
ALLOWED_NUMBER_OF_ROWS: int = Field(
default=20,
description="number of rows to show (when not using interactive tables).",
command="n_rows",
group=SettingGroups.preferences,
json_schema_extra={
"command": "n_rows",
"group": SettingGroups.preferences.value,
},
)
ALLOWED_NUMBER_OF_COLUMNS: int = Field(
default=5,
description="number of columns to show (when not using interactive tables).",
command="n_cols",
group=SettingGroups.preferences,
json_schema_extra={
"command": "n_cols",
"group": SettingGroups.preferences.value,
},
)
model_config = ConfigDict(validate_assignment=True)