use singleton for provider interface: same functionality (#5372)

* use singleton for provider interface: same functionality

* ruff
This commit is contained in:
montezdesousa
2023-08-24 18:09:19 +01:00
committed by GitHub
parent 3f413b1731
commit 484f738e24
11 changed files with 22 additions and 34 deletions

View File

@@ -18,7 +18,7 @@ from openbb_core.app.model.command_context import CommandContext
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 get_provider_interface
from openbb_core.app.provider_interface import 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
@@ -125,7 +125,7 @@ class ParametersBuilder:
)
kwargs["provider_choices"] = {"provider": provider}
else:
available_providers = get_provider_interface().available_providers
available_providers = ProviderInterface().available_providers
kwargs["provider_choices"] = {
"provider": available_providers[0]
if available_providers

View File

@@ -2,7 +2,7 @@ from typing import Dict, List, Optional, Tuple
from pydantic import create_model
from openbb_core.app.provider_interface import get_provider_interface
from openbb_core.app.provider_interface import ProviderInterface
# Here we create the BaseModel from the provider required credentials.
# This means that if a new provider extension is installed, the required
@@ -28,7 +28,7 @@ def format_map(
Credentials = create_model( # type: ignore
"Credentials",
__config__=Config,
**format_map(get_provider_interface().required_credentials),
**format_map(ProviderInterface().required_credentials),
)

View File

@@ -10,11 +10,11 @@ from openbb_core.app.model.abstract.tagged import Tagged
from openbb_core.app.model.abstract.warning import Warning_
from openbb_core.app.model.charts.chart import Chart
from openbb_core.app.model.metadata import Metadata
from openbb_core.app.provider_interface import get_provider_interface
from openbb_core.app.provider_interface import ProviderInterface
from openbb_core.app.utils import basemodel_to_df
T = TypeVar("T")
PROVIDERS = Literal[tuple(get_provider_interface().available_providers)] # type: ignore
PROVIDERS = Literal[tuple(ProviderInterface().available_providers)] # type: ignore
class OBBject(GenericModel, Generic[T], Tagged):

View File

@@ -8,6 +8,8 @@ from openbb_provider.utils.helpers import to_snake_case
from pydantic import BaseConfig, BaseModel, Extra, Field, create_model
from pydantic.fields import ModelField
from openbb_core.app.model.abstract.singleton import SingletonMeta
TupleFieldType = Tuple[str, type, Any]
@@ -45,7 +47,7 @@ class ProviderChoices:
provider: Literal # type: ignore
class ProviderInterface:
class ProviderInterface(metaclass=SingletonMeta):
"""Provider interface class. Provides access to 'openbb_provider' package information.
Properties
@@ -459,11 +461,3 @@ class ProviderInterface:
fields=[("provider", Literal[tuple(available_providers)])], # type: ignore
bases=(ProviderChoices,),
)
__provider_interface = ProviderInterface()
def get_provider_interface() -> ProviderInterface:
"""Get the provider interface, this only needs to be created once."""
return __provider_interface

View File

@@ -9,8 +9,8 @@ from openbb_core.app.model.command_context import CommandContext
from openbb_core.app.provider_interface import (
ExtraParams,
ProviderChoices,
ProviderInterface,
StandardParams,
get_provider_interface,
)
@@ -30,7 +30,7 @@ class Query:
self.standard_params = standard_params
self.extra_params = extra_params
self.name = self.standard_params.__class__.__name__
self.provider_interface = get_provider_interface()
self.provider_interface = ProviderInterface()
def filter_extra_params(
self,

View File

@@ -29,8 +29,8 @@ from openbb_core.app.model.obbject import OBBject
from openbb_core.app.provider_interface import (
ExtraParams,
ProviderChoices,
ProviderInterface,
StandardParams,
get_provider_interface,
)
P = ParamSpec("P")
@@ -223,7 +223,7 @@ class SignatureInspector:
cls, func: Callable[P, OBBject], model: str
) -> Optional[Callable[P, OBBject]]:
"""Complete function signature."""
provider_interface = get_provider_interface()
provider_interface = ProviderInterface()
return_type = func.__annotations__["return"]
is_list = False
@@ -375,7 +375,7 @@ class CommandMap:
) -> Dict[str, List[str]]:
api_router = router.api_router
mapping = get_provider_interface().map
mapping = ProviderInterface().map
coverage_map: Dict[Any, Any] = {}
for route in api_router.routes:
@@ -405,7 +405,7 @@ class CommandMap:
) -> Dict[str, List[str]]:
api_router = router.api_router
mapping = get_provider_interface().map
mapping = ProviderInterface().map
coverage_map: Dict[Any, Any] = {}
for route in api_router.routes:

View File

@@ -29,7 +29,7 @@ from typing_extensions import Annotated, _AnnotatedAlias
from openbb_core.app.env import Env
from openbb_core.app.model.custom_parameter import OpenBBCustomParameter
from openbb_core.app.provider_interface import get_provider_interface
from openbb_core.app.provider_interface import ProviderInterface
from openbb_core.app.router import RouterLoader
@@ -268,7 +268,7 @@ class ClassDefinition:
class DocstringGenerator:
"""Dynamically generate docstrings for the commands."""
provider_interface = get_provider_interface()
provider_interface = ProviderInterface()
@staticmethod
def get_OBBject_description(model_name: str, providers: Optional[str]) -> str:
@@ -532,7 +532,7 @@ class MethodDefinition:
"""Add the field description to the param signature."""
if model_name:
available_fields = (
get_provider_interface()
ProviderInterface()
.map[model_name]["openbb"]["QueryParams"]["fields"]
.keys()
)
@@ -542,7 +542,7 @@ class MethodDefinition:
continue
description = (
get_provider_interface()
ProviderInterface()
.map[model_name]["openbb"]["QueryParams"]["fields"][param]
.field_info.description
)

View File

@@ -4,7 +4,7 @@ from unittest.mock import patch
def test_credentials():
with patch(
target="openbb_core.app.model.credentials.get_provider_interface"
target="openbb_core.app.model.credentials.ProviderInterface"
) as mock_provider_interface:
mock_provider_interface.required_credentials = {
"benzinga_api_key": (typing.Optional[str], None),

View File

@@ -5,7 +5,6 @@ import pytest
from openbb_core.app.provider_interface import (
ProviderChoices,
ProviderInterface,
get_provider_interface,
)
@@ -20,11 +19,6 @@ def test_init(provider_interface):
assert provider_interface
def test_get_provider_interface():
"""Test get provider interface."""
assert isinstance(get_provider_interface(), ProviderInterface)
def test_map(provider_interface):
"""Test map."""
provider_interface_map = provider_interface.map

View File

@@ -84,7 +84,7 @@ def test_filter_extra_params_wrong_param(query):
def mock_registry():
"""Mock registry."""
with patch(
"openbb_core.app.provider_interface.get_provider_interface"
"openbb_core.app.provider_interface.ProviderInterface"
) as mock_get_provider_interface:
mock_registry = MagicMock()
mock_get_provider_interface.return_value.build_registry.return_value = (

View File

@@ -84,7 +84,7 @@ def test_filter_extra_params_wrong_param(query):
def mock_registry():
"""Mock registry."""
with patch(
"openbb_core.app.provider_interface.get_provider_interface"
"openbb_core.app.provider_interface.ProviderInterface"
) as mock_get_provider_interface:
mock_registry = MagicMock()
mock_get_provider_interface.return_value.build_registry.return_value = (