Skip to content

Commit

Permalink
Prevent overriding server_settings on base provider class
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jun 10, 2024
1 parent 6f32acc commit a13b5a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/jupyter-ai-magics/jupyter_ai_magics/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ def __new__(mcs, name, bases, namespace, **kwargs):

return cls

@property
def server_settings(cls):
return cls._server_settings

@server_settings.setter
def server_settings(cls, value):
if cls._server_settings is not None:
raise AttributeError("'server_settings' attribute was already set")
cls._server_settings = value

_server_settings = None


class BaseProvider(BaseModel, metaclass=ProviderMetaclass):
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from types import MappingProxyType
from typing import ClassVar, Optional

from langchain.pydantic_v1 import BaseModel
from pytest import raises

from ..providers import ProviderMetaclass
from ..providers import BaseProvider, ProviderMetaclass


def test_provider_metaclass():
Expand All @@ -24,3 +26,10 @@ class Child(Base, Parent, metaclass=ProviderMetaclass):
test: ClassVar[str] = "expected"

assert Child.test == "expected"


def test_base_provider_server_settings_read_only():
BaseProvider.server_settings = MappingProxyType({})

with raises(AttributeError, match="'server_settings' attribute was already set"):
BaseProvider.server_settings = MappingProxyType({})

0 comments on commit a13b5a6

Please sign in to comment.