From 921abf20cda56e85749368905dd8fac094609541 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 12:49:37 +0000 Subject: [PATCH 01/12] fix: ensure var_kw args come last in signatures --- .../core/openbb_core/api/router/commands.py | 25 +++++++++++++------ .../openbb_core/app/static/package_builder.py | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/openbb_platform/core/openbb_core/api/router/commands.py b/openbb_platform/core/openbb_core/api/router/commands.py index 96334f549fa9..d788f0ea5b1a 100644 --- a/openbb_platform/core/openbb_core/api/router/commands.py +++ b/openbb_platform/core/openbb_core/api/router/commands.py @@ -51,11 +51,14 @@ def build_new_signature(path: str, func: Callable) -> Signature: parameter_list = sig.parameters.values() return_annotation = sig.return_annotation new_parameter_list = [] - - for parameter in parameter_list: + var_kw_start = len(parameter_list) + for pos, parameter in enumerate(parameter_list): if parameter.name == "cc" and parameter.annotation == CommandContext: continue + if parameter.kind == Parameter.VAR_KEYWORD: + var_kw_start = pos + new_parameter_list.append( Parameter( parameter.name, @@ -66,18 +69,21 @@ def build_new_signature(path: str, func: Callable) -> Signature: ) if CHARTING_INSTALLED and path.replace("/", "_")[1:] in Charting.functions(): - new_parameter_list.append( + new_parameter_list.insert( + var_kw_start, Parameter( "chart", kind=Parameter.POSITIONAL_OR_KEYWORD, default=False, annotation=bool, - ) + ), ) + var_kw_start += 1 if custom_headers := SystemService().system_settings.api_settings.custom_headers: for name, default in custom_headers.items(): - new_parameter_list.append( + new_parameter_list.insert( + var_kw_start, Parameter( name.replace("-", "_"), kind=Parameter.POSITIONAL_OR_KEYWORD, @@ -85,11 +91,13 @@ def build_new_signature(path: str, func: Callable) -> Signature: annotation=Annotated[ Optional[str], Header(include_in_schema=False) ], - ) + ), ) + var_kw_start += 1 if Env().API_AUTH: - new_parameter_list.append( + new_parameter_list.insert( + var_kw_start, Parameter( "__authenticated_user_settings", kind=Parameter.POSITIONAL_OR_KEYWORD, @@ -97,8 +105,9 @@ def build_new_signature(path: str, func: Callable) -> Signature: annotation=Annotated[ UserSettings, Depends(AuthService().user_settings_hook) ], - ) + ), ) + var_kw_start += 1 return Signature( parameters=new_parameter_list, diff --git a/openbb_platform/core/openbb_core/app/static/package_builder.py b/openbb_platform/core/openbb_core/app/static/package_builder.py index 793f82731c6c..0047822b2ace 100644 --- a/openbb_platform/core/openbb_core/app/static/package_builder.py +++ b/openbb_platform/core/openbb_core/app/static/package_builder.py @@ -531,7 +531,7 @@ def get_deprecation_message(path: str) -> str: def reorder_params(params: Dict[str, Parameter]) -> "OrderedDict[str, Parameter]": """Reorder the params.""" formatted_keys = list(params.keys()) - for k in ["provider", "extra_params"]: + for k in ["provider", "extra_params", "kwargs"]: if k in formatted_keys: formatted_keys.remove(k) formatted_keys.append(k) From 6657a9e70f1be274744bc55f90add1894d925f47 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 12:54:43 +0000 Subject: [PATCH 02/12] remove package builder change --- openbb_platform/core/openbb_core/app/static/package_builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/static/package_builder.py b/openbb_platform/core/openbb_core/app/static/package_builder.py index 0047822b2ace..e9fffa956a81 100644 --- a/openbb_platform/core/openbb_core/app/static/package_builder.py +++ b/openbb_platform/core/openbb_core/app/static/package_builder.py @@ -531,7 +531,7 @@ def get_deprecation_message(path: str) -> str: def reorder_params(params: Dict[str, Parameter]) -> "OrderedDict[str, Parameter]": """Reorder the params.""" formatted_keys = list(params.keys()) - for k in ["provider", "extra_params", "kwargs"]: + for k in ["provider", "extra_params", "**kwargs"]: if k in formatted_keys: formatted_keys.remove(k) formatted_keys.append(k) From 41befa78e5fc7c5be49eccf10149a8501ffb1d2b Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:56:00 +0000 Subject: [PATCH 03/12] Update package_builder.py --- openbb_platform/core/openbb_core/app/static/package_builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/static/package_builder.py b/openbb_platform/core/openbb_core/app/static/package_builder.py index e9fffa956a81..793f82731c6c 100644 --- a/openbb_platform/core/openbb_core/app/static/package_builder.py +++ b/openbb_platform/core/openbb_core/app/static/package_builder.py @@ -531,7 +531,7 @@ def get_deprecation_message(path: str) -> str: def reorder_params(params: Dict[str, Parameter]) -> "OrderedDict[str, Parameter]": """Reorder the params.""" formatted_keys = list(params.keys()) - for k in ["provider", "extra_params", "**kwargs"]: + for k in ["provider", "extra_params"]: if k in formatted_keys: formatted_keys.remove(k) formatted_keys.append(k) From c932a1b2aa80e4a17451cc7c6f3cb1158d2d531f Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 17:14:17 +0000 Subject: [PATCH 04/12] fix: warn only when ExtraParams --- openbb_platform/core/openbb_core/app/command_runner.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/command_runner.py b/openbb_platform/core/openbb_core/app/command_runner.py index c8d7f43e3776..e27f2142b40d 100644 --- a/openbb_platform/core/openbb_core/app/command_runner.py +++ b/openbb_platform/core/openbb_core/app/command_runner.py @@ -20,7 +20,7 @@ from openbb_core.app.model.obbject import OBBject from openbb_core.app.model.system_settings import SystemSettings from openbb_core.app.model.user_settings import UserSettings -from openbb_core.app.provider_interface import ProviderInterface +from openbb_core.app.provider_interface import ExtraParams, ProviderInterface from openbb_core.app.router import CommandMap from openbb_core.app.service.system_service import SystemService from openbb_core.app.service.user_service import UserService @@ -189,9 +189,11 @@ def _warn_kwargs( annotation = getattr( model.model_fields.get("extra_params", None), "annotation", None ) - if annotation: + if is_dataclass(annotation) and any( + t is ExtraParams for t in getattr(annotation, "__bases__", []) + ): # When there is no annotation there is nothing to warn - valid = asdict(annotation()) if is_dataclass(annotation) else {} # type: ignore + valid = asdict(annotation()) # type: ignore provider = provider_choices.get("provider", None) for p in extra_params: if field := valid.get(p): From 8c1a67fc3229626cd6c3f5f70de7443669f0e81e Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 18:43:25 +0000 Subject: [PATCH 05/12] track VAR_KEYWORD --- .../openbb_core/app/static/package_builder.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/static/package_builder.py b/openbb_platform/core/openbb_core/app/static/package_builder.py index 793f82731c6c..d3b9bc9af688 100644 --- a/openbb_platform/core/openbb_core/app/static/package_builder.py +++ b/openbb_platform/core/openbb_core/app/static/package_builder.py @@ -528,10 +528,12 @@ def get_deprecation_message(path: str) -> str: return getattr(PathHandler.build_route_map()[path], "summary", "") @staticmethod - def reorder_params(params: Dict[str, Parameter]) -> "OrderedDict[str, Parameter]": - """Reorder the params.""" + def reorder_params( + params: Dict[str, Parameter], var_kw: List[str] + ) -> "OrderedDict[str, Parameter]": + """Reorder the params and make sure VAR_KEYWORD come after 'provider.""" formatted_keys = list(params.keys()) - for k in ["provider", "extra_params"]: + for k in ["provider"] + var_kw: if k in formatted_keys: formatted_keys.remove(k) formatted_keys.append(k) @@ -563,14 +565,11 @@ def format_params( ) formatted: Dict[str, Parameter] = {} - + var_kw = [] for name, param in parameter_map.items(): - if name == "extra_params": + if name in ["extra_params"]: formatted[name] = Parameter(name="kwargs", kind=Parameter.VAR_KEYWORD) - elif name == "kwargs": - formatted["**" + name] = Parameter( - name="kwargs", kind=Parameter.VAR_KEYWORD, annotation=Any - ) + var_kw.append(name) elif name == "provider_choices": fields = param.annotation.__args__[0].__dataclass_fields__ field = fields["provider"] @@ -624,12 +623,14 @@ def format_params( formatted[name] = Parameter( name=name, - kind=Parameter.POSITIONAL_OR_KEYWORD, + kind=param.kind, annotation=updated_type, default=param.default, ) + if param.kind == Parameter.VAR_KEYWORD: + var_kw.append(name) - return MethodDefinition.reorder_params(params=formatted) + return MethodDefinition.reorder_params(params=formatted, var_kw=var_kw) @staticmethod def add_field_custom_annotations( From 1e0b4e895b353e034161fba23099c4317ac991d6 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 18:44:24 +0000 Subject: [PATCH 06/12] minor fix --- openbb_platform/core/openbb_core/app/static/package_builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/static/package_builder.py b/openbb_platform/core/openbb_core/app/static/package_builder.py index d3b9bc9af688..6918abe5bb34 100644 --- a/openbb_platform/core/openbb_core/app/static/package_builder.py +++ b/openbb_platform/core/openbb_core/app/static/package_builder.py @@ -567,7 +567,7 @@ def format_params( formatted: Dict[str, Parameter] = {} var_kw = [] for name, param in parameter_map.items(): - if name in ["extra_params"]: + if name == "extra_params": formatted[name] = Parameter(name="kwargs", kind=Parameter.VAR_KEYWORD) var_kw.append(name) elif name == "provider_choices": From 248b1b377cf66d1fea6352d771f4a23977d2588c Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 19:00:16 +0000 Subject: [PATCH 07/12] type Any if no type provided --- openbb_platform/core/openbb_core/app/command_runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/command_runner.py b/openbb_platform/core/openbb_core/app/command_runner.py index e27f2142b40d..ab1114856491 100644 --- a/openbb_platform/core/openbb_core/app/command_runner.py +++ b/openbb_platform/core/openbb_core/app/command_runner.py @@ -229,7 +229,7 @@ def validate_kwargs( sig = signature(func) fields = { n: ( - p.annotation, + p.annotation if p.annotation is not Parameter.empty else Any, ... if p.default is Parameter.empty else p.default, ) for n, p in sig.parameters.items() From d8f5e5e116b0cf561eddf5a15897d256b775c2e7 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 19:01:47 +0000 Subject: [PATCH 08/12] minor fix --- openbb_platform/core/openbb_core/app/command_runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/command_runner.py b/openbb_platform/core/openbb_core/app/command_runner.py index ab1114856491..83056ecf7e16 100644 --- a/openbb_platform/core/openbb_core/app/command_runner.py +++ b/openbb_platform/core/openbb_core/app/command_runner.py @@ -229,7 +229,7 @@ def validate_kwargs( sig = signature(func) fields = { n: ( - p.annotation if p.annotation is not Parameter.empty else Any, + Any if p.annotation is Parameter.empty else p.annotation, ... if p.default is Parameter.empty else p.default, ) for n, p in sig.parameters.items() From 182698fe154e0d02507caeb9fdd7332b0dc36b26 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 20:01:20 +0000 Subject: [PATCH 09/12] fix test --- .../core/tests/app/test_command_runner.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/openbb_platform/core/tests/app/test_command_runner.py b/openbb_platform/core/tests/app/test_command_runner.py index 3447d66407bf..b8a80bb7a807 100644 --- a/openbb_platform/core/tests/app/test_command_runner.py +++ b/openbb_platform/core/tests/app/test_command_runner.py @@ -3,6 +3,7 @@ from typing import Dict, List from unittest.mock import Mock, patch +from openbb_core.app.provider_interface import ExtraParams import pytest from fastapi import Query from fastapi.params import Query as QueryParam @@ -228,45 +229,57 @@ def test_parameters_builder_validate_kwargs(mock_func): @pytest.mark.parametrize( - "provider_choices, extra_params, expect", + "provider_choices, extra_params, base, expect", [ ( {"provider": "provider1"}, {"exists_in_2": ...}, + ExtraParams, OpenBBWarning, ), ( {"provider": "inexistent_provider"}, {"exists_in_both": ...}, + ExtraParams, OpenBBWarning, ), ( {}, {"inexistent_field": ...}, + ExtraParams, OpenBBWarning, ), + ( + {}, + {"inexistent_field": ...}, + object, + None, + ), ( {"provider": "provider2"}, {"exists_in_2": ...}, + ExtraParams, None, ), ( {"provider": "provider2"}, {"exists_in_both": ...}, + ExtraParams, None, ), ( {}, {"exists_in_both": ...}, + ExtraParams, None, ), ], ) -def test_parameters_builder__warn_kwargs(provider_choices, extra_params, expect): +def test_parameters_builder__warn_kwargs(provider_choices, extra_params, base, expect): """Test _warn_kwargs.""" @dataclass - class SomeModel: + class SomeModel(base): """SomeModel""" exists_in_2: QueryParam = Query(..., title="provider2") From 7ff02dd536304a1d7d3f70d20c61c8b725c20a5f Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 20:30:51 +0000 Subject: [PATCH 10/12] fix: _as_dict --- openbb_platform/core/openbb_core/app/command_runner.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openbb_platform/core/openbb_core/app/command_runner.py b/openbb_platform/core/openbb_core/app/command_runner.py index cbd356a0b550..f870c32c62ff 100644 --- a/openbb_platform/core/openbb_core/app/command_runner.py +++ b/openbb_platform/core/openbb_core/app/command_runner.py @@ -218,7 +218,12 @@ def _warn_kwargs( @staticmethod def _as_dict(obj: Any) -> Dict[str, Any]: """Safely convert an object to a dict.""" - return asdict(obj) if is_dataclass(obj) else dict(obj) + try: + if isinstance(obj, dict): + return obj + return asdict(obj) if is_dataclass(obj) else dict(obj) + except Exception: + return {} @staticmethod def validate_kwargs( From fc6274027e8797a9a49fde405fe33cbfe0b3b8b4 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 22 Mar 2024 20:33:58 +0000 Subject: [PATCH 11/12] ruff --- openbb_platform/core/tests/app/test_command_runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbb_platform/core/tests/app/test_command_runner.py b/openbb_platform/core/tests/app/test_command_runner.py index b8a80bb7a807..cd751e9c592d 100644 --- a/openbb_platform/core/tests/app/test_command_runner.py +++ b/openbb_platform/core/tests/app/test_command_runner.py @@ -3,7 +3,6 @@ from typing import Dict, List from unittest.mock import Mock, patch -from openbb_core.app.provider_interface import ExtraParams import pytest from fastapi import Query from fastapi.params import Query as QueryParam @@ -17,6 +16,7 @@ from openbb_core.app.model.command_context import CommandContext from openbb_core.app.model.system_settings import SystemSettings from openbb_core.app.model.user_settings import UserSettings +from openbb_core.app.provider_interface import ExtraParams from openbb_core.app.router import CommandMap from pydantic import BaseModel, ConfigDict From c60c58ad1304bf64480c7bb9cf521c8dc8944770 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:50:01 -0700 Subject: [PATCH 12/12] set kwargs with default state in Fast API --- .../core/openbb_core/api/router/commands.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/openbb_platform/core/openbb_core/api/router/commands.py b/openbb_platform/core/openbb_core/api/router/commands.py index 0052ab65e2ce..a19722633fad 100644 --- a/openbb_platform/core/openbb_core/api/router/commands.py +++ b/openbb_platform/core/openbb_core/api/router/commands.py @@ -53,18 +53,25 @@ def build_new_signature(path: str, func: Callable) -> Signature: new_parameter_list = [] var_kw_start = len(parameter_list) for pos, parameter in enumerate(parameter_list): + var_kind = None + var_name = None + var_annotation = None if parameter.name == "cc" and parameter.annotation == CommandContext: continue if parameter.kind == Parameter.VAR_KEYWORD: var_kw_start = pos + var_kind = Parameter.POSITIONAL_OR_KEYWORD + var_default = None + var_name = parameter.name + var_annotation = Optional[Dict[str, Any]] new_parameter_list.append( Parameter( - parameter.name, - kind=parameter.kind, - default=parameter.default, - annotation=parameter.annotation, + name=var_name if var_name else parameter.name, + kind=var_kind if var_kind else parameter.kind, + default=parameter.default if not var_kind else var_default, + annotation=var_annotation if var_annotation else parameter.annotation, ) )