summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormontezdesousa <79287829+montezdesousa@users.noreply.github.com>2024-02-07 19:18:21 +0000
committerGitHub <noreply@github.com>2024-02-07 19:18:21 +0000
commitaa1fab6f2882f5368e782c23e1ae16509ab060fe (patch)
tree3d5c0c8f8a06a3c6217522ed259baabdf8d17666
parent158d6e9d15836d201c6a9b4c185cef7795eb9bba (diff)
display warnings based on preference (#6051)
-rw-r--r--openbb_platform/core/openbb_core/app/command_runner.py34
-rw-r--r--openbb_platform/core/openbb_core/app/model/preferences.py1
-rw-r--r--website/content/platform/usage/index.md3
3 files changed, 25 insertions, 13 deletions
diff --git a/openbb_platform/core/openbb_core/app/command_runner.py b/openbb_platform/core/openbb_core/app/command_runner.py
index 171ede70316..ba6215a26cb 100644
--- a/openbb_platform/core/openbb_core/app/command_runner.py
+++ b/openbb_platform/core/openbb_core/app/command_runner.py
@@ -1,13 +1,12 @@
"""Command runner module."""
import warnings
-from contextlib import nullcontext
from copy import deepcopy
from datetime import datetime
from inspect import Parameter, signature
from sys import exc_info
from time import perf_counter_ns
-from typing import Any, Callable, ContextManager, Dict, List, Optional, Tuple, Union
+from typing import Any, Callable, Dict, List, Optional, Tuple
from pydantic import ConfigDict, create_model
@@ -240,23 +239,33 @@ class StaticCommandRunner:
"""Static Command Runner."""
@classmethod
- async def _command(cls, func: Callable, kwargs: Dict[str, Any]) -> OBBject:
+ async def _command(
+ cls,
+ func: Callable,
+ kwargs: Dict[str, Any],
+ show_warnings: bool = True,
+ ) -> OBBject:
"""Run a command and return the output."""
- context_manager: Union[warnings.catch_warnings, ContextManager[None]] = (
- warnings.catch_warnings(record=True)
- if not Env().DEBUG_MODE
- else nullcontext()
- )
- with context_manager as warning_list:
+ with warnings.catch_warnings(record=True) as warning_list:
obbject = await maybe_coroutine(func, **kwargs)
-
obbject.provider = getattr(
kwargs.get("provider_choices", None), "provider", None
)
- if warning_list:
- obbject.warnings = list(map(cast_warning, warning_list))
+ if warning_list:
+ obbject.warnings = []
+ for w in warning_list:
+ obbject.warnings.append(cast_warning(w))
+ if show_warnings:
+ warnings.showwarning(
+ message=w.message,
+ category=w.category,
+ filename=w.filename,
+ lineno=w.lineno,
+ file=w.file,
+ line=w.line,
+ )
return obbject
@@ -328,6 +337,7 @@ class StaticCommandRunner:
obbject = await cls._command(
func=func,
kwargs=kwargs,
+ show_warnings=user_settings.preferences.show_warnings,
)
if chart and obbject.results:
diff --git a/openbb_platform/core/openbb_core/app/model/preferences.py b/openbb_platform/core/openbb_core/app/model/preferences.py
index 0fb2a555500..19a24a2bfc1 100644
--- a/openbb_platform/core/openbb_core/app/model/preferences.py
+++ b/openbb_platform/core/openbb_core/app/model/preferences.py
@@ -30,6 +30,7 @@ class Preferences(BaseModel):
output_type: Literal["OBBject", "dataframe", "polars", "numpy", "dict", "chart"] = (
Field(default="OBBject", description="Python default output type.")
)
+ show_warnings: bool = True
model_config = ConfigDict(validate_assignment=True)
diff --git a/website/content/platform/usage/index.md b/website/content/platform/usage/index.md
index 13ce82322ae..59dac5d146f 100644
--- a/website/content/platform/usage/index.md
+++ b/website/content/platform/usage/index.md
@@ -146,7 +146,8 @@ HTTP_PROXY="http://10.10.10.10:8000"
| request_timeout | 15 | Any positive integer. | Specifies the timeout duration for HTTP requests. |
| metadata | True | [True, False] | Enables or disables the collection of metadata which provides information about operations including arguments duration route and timestamp. Disabling this feature may improve performance in cases where contextual information is not needed or when the additional computation time and storage space are a concern. |
| field_order | False | [True, False] | Enables or disables the information on the field order defined with Pydantic models. Useful for frontend as the API returns JSON which is an unordered collection. |
-| output_type | OBBject | ["OBBject", "dataframe", "numpy", "dict", "chart", "polars"] | Specifies the type of data the application will output when a command or endpoint is accessed. Note that choosing data formats only available in Python such as `dataframe` | `numpy` or `polars` will render the application's API non-functional. |
+| output_type | OBBject | ["OBBject", "dataframe", "numpy", "dict", "chart", "polars"] | Specifies the type of data the application will output when a command or endpoint is accessed. Note that choosing data formats only available in Python such as `dataframe`, `numpy` or `polars` will render the application's API non-functional. |
+| show_warnings | True | [True, False] | Enables or disables the display of warnings. |
User settings can be set from the Python interface directly.