Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overriding server_settings on base provider class #825

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -196,6 +196,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

dlqqq marked this conversation as resolved.
Show resolved Hide resolved

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({})
Loading