Skip to content

Commit 4e5ee56

Browse files
authored
[BugFix] Move showwarning To Outside catch_warnings Context Block (#7058)
* move showwarning outside of catch_warnings context * linters
1 parent 428a4e0 commit 4e5ee56

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed

openbb_platform/core/openbb_core/app/command_runner.py

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -301,35 +301,37 @@ async def _execute_func( # pylint: disable=too-many-positional-arguments
301301

302302
user_settings = execution_context.user_settings
303303
system_settings = execution_context.system_settings
304+
raised_warnings: list = []
305+
custom_headers: Optional[dict[str, Any]] = None
304306

305-
with catch_warnings(record=True) as warning_list:
306-
# If we're on Jupyter we need to pop here because we will lose "chart" after
307-
# ParametersBuilder.build. This needs to be fixed in a way that chart is
308-
# added to the function signature and shared for jupyter and api
309-
# We can check in the router decorator if the given function has a chart
310-
# in the charting extension then we add it there. This way we can remove
311-
# the chart parameter from the commands.py and package_builder, it will be
312-
# added to the function signature in the router decorator
313-
chart = kwargs.pop("chart", False)
314-
315-
kwargs = ParametersBuilder.build(
316-
args=args,
317-
execution_context=execution_context,
318-
func=func,
319-
kwargs=kwargs,
320-
)
307+
try:
308+
with catch_warnings(record=True) as warning_list:
309+
# If we're on Jupyter we need to pop here because we will lose "chart" after
310+
# ParametersBuilder.build. This needs to be fixed in a way that chart is
311+
# added to the function signature and shared for jupyter and api
312+
# We can check in the router decorator if the given function has a chart
313+
# in the charting extension then we add it there. This way we can remove
314+
# the chart parameter from the commands.py and package_builder, it will be
315+
# added to the function signature in the router decorator
316+
chart = kwargs.pop("chart", False)
317+
318+
kwargs = ParametersBuilder.build(
319+
args=args,
320+
execution_context=execution_context,
321+
func=func,
322+
kwargs=kwargs,
323+
)
321324

322-
# If we're on the api we need to remove "chart" here because the parameter is added on
323-
# commands.py and the function signature does not expect "chart"
324-
kwargs.pop("chart", None)
325-
# We also pop custom headers
326-
model_headers = system_settings.api_settings.custom_headers or {}
327-
custom_headers = {
328-
name: kwargs.pop(name.replace("-", "_"), default)
329-
for name, default in model_headers.items() or {}
330-
} or None
325+
# If we're on the api we need to remove "chart" here because the parameter is added on
326+
# commands.py and the function signature does not expect "chart"
327+
kwargs.pop("chart", None)
328+
# We also pop custom headers
329+
model_headers = system_settings.api_settings.custom_headers or {}
330+
custom_headers = {
331+
name: kwargs.pop(name.replace("-", "_"), default)
332+
for name, default in model_headers.items() or {}
333+
} or None
331334

332-
try:
333335
obbject = await cls._command(func, kwargs)
334336
# The output might be from a router command with 'no_validate=True'
335337
# It might be of a different type than OBBject.
@@ -350,32 +352,33 @@ async def _execute_func( # pylint: disable=too-many-positional-arguments
350352
if chart and obbject.results:
351353
cls._chart(obbject, **kwargs)
352354

353-
if warning_list:
355+
raised_warnings = warning_list if warning_list else []
356+
finally:
357+
if raised_warnings:
358+
if isinstance(obbject, OBBject):
359+
obbject.warnings = []
360+
for w in raised_warnings:
354361
if isinstance(obbject, OBBject):
355-
obbject.warnings = []
356-
for w in warning_list:
357-
if isinstance(obbject, OBBject):
358-
obbject.warnings.append(cast_warning(w))
359-
if user_settings.preferences.show_warnings:
360-
showwarning(
361-
message=w.message,
362-
category=w.category,
363-
filename=w.filename,
364-
lineno=w.lineno,
365-
file=w.file,
366-
line=w.line,
367-
)
368-
finally:
369-
ls = LoggingService(system_settings, user_settings)
370-
ls.log(
371-
user_settings=user_settings,
372-
system_settings=system_settings,
373-
route=route,
374-
func=func,
375-
kwargs=kwargs,
376-
exec_info=exc_info(),
377-
custom_headers=custom_headers,
378-
)
362+
obbject.warnings.append(cast_warning(w))
363+
if user_settings.preferences.show_warnings:
364+
showwarning(
365+
message=w.message,
366+
category=w.category,
367+
filename=w.filename,
368+
lineno=w.lineno,
369+
file=w.file,
370+
line=w.line,
371+
)
372+
ls = LoggingService(system_settings, user_settings)
373+
ls.log(
374+
user_settings=user_settings,
375+
system_settings=system_settings,
376+
route=route,
377+
func=func,
378+
kwargs=kwargs,
379+
exec_info=exc_info(),
380+
custom_headers=custom_headers,
381+
)
379382

380383
return obbject
381384

openbb_platform/core/openbb_core/app/model/preferences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Preferences(BaseModel):
2222
validate_default=True,
2323
)
2424
request_timeout: PositiveInt = 60
25-
show_warnings: bool = True
25+
show_warnings: bool = False
2626
table_style: Literal["dark", "light"] = "dark"
2727
user_styles_directory: str = str(Path.home() / "OpenBBUserData" / "styles" / "user")
2828

0 commit comments

Comments
 (0)