Skip to content

Commit

Permalink
Lowercase global vars, they are not constants
Browse files Browse the repository at this point in the history
  • Loading branch information
vtaskow committed Jul 26, 2023
1 parent cf68aa1 commit 3701066
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions mlserver/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

from .settings import ModelSettings

MODEL_NAME_VAR: ContextVar[str] = ContextVar("model_name")
MODEL_VERSION_VAR: ContextVar[str] = ContextVar("model_version")
model_name_var: ContextVar[str] = ContextVar("model_name")
model_version_var: ContextVar[str] = ContextVar("model_version")


@contextmanager
def model_context(model_settings: ModelSettings):
model_name_token = MODEL_NAME_VAR.set(model_settings.name)
model_name_token = model_name_var.set(model_settings.name)

model_version = ""
if model_settings.version:
model_version = model_settings.version
model_version_token = MODEL_VERSION_VAR.set(model_version)
model_version_token = model_version_var.set(model_version)

try:
yield
finally:
MODEL_NAME_VAR.reset(model_name_token)
MODEL_VERSION_VAR.reset(model_version_token)
model_name_var.reset(model_name_token)
model_version_var.reset(model_version_token)
6 changes: 3 additions & 3 deletions mlserver/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Optional, Dict, Union
import logging.config

from .context import MODEL_NAME_VAR, MODEL_VERSION_VAR
from .context import model_name_var, model_version_var
from .settings import Settings

LoggerName = "mlserver"
Expand Down Expand Up @@ -75,8 +75,8 @@ def _format_structured_model_details(name: str, version: str) -> str:
return model_details

def format(self, record: logging.LogRecord) -> str:
model_name = MODEL_NAME_VAR.get("")
model_version = MODEL_VERSION_VAR.get("")
model_name = model_name_var.get("")
model_version = model_version_var.get("")

record.model = (
self._format_structured_model_details(model_name, model_version)
Expand Down
6 changes: 3 additions & 3 deletions mlserver/metrics/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .registry import REGISTRY
from .errors import InvalidModelContext
from ..context import MODEL_NAME_VAR, MODEL_VERSION_VAR
from ..context import model_name_var, model_version_var

SELDON_MODEL_NAME_LABEL = "model_name"
SELDON_MODEL_VERSION_LABEL = "model_version"
Expand All @@ -28,8 +28,8 @@ def register(name: str, description: str) -> Histogram:

def _get_labels_from_context() -> dict:
try:
model_name = MODEL_NAME_VAR.get()
model_version = MODEL_VERSION_VAR.get()
model_name = model_name_var.get()
model_version = model_version_var.get()
return {
SELDON_MODEL_NAME_LABEL: model_name,
SELDON_MODEL_VERSION_LABEL: model_version,
Expand Down
14 changes: 7 additions & 7 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

from mlserver.settings import ModelSettings, ModelParameters
from mlserver.context import model_context, MODEL_NAME_VAR, MODEL_VERSION_VAR
from mlserver.context import model_context, model_name_var, model_version_var
from .fixtures import SumModel


Expand All @@ -29,19 +29,19 @@ def test_model_context(name: str, version: Optional[str], expected_version: str)
)

with pytest.raises(LookupError):
_ = MODEL_NAME_VAR.get()
_ = model_name_var.get()

with pytest.raises(LookupError):
_ = MODEL_VERSION_VAR.get()
_ = model_version_var.get()

with model_context(model_settings):
var_name = MODEL_NAME_VAR.get()
var_version = MODEL_VERSION_VAR.get()
var_name = model_name_var.get()
var_version = model_version_var.get()
assert var_name == name
assert var_version == expected_version

with pytest.raises(LookupError):
_ = MODEL_NAME_VAR.get()
_ = model_name_var.get()

with pytest.raises(LookupError):
_ = MODEL_VERSION_VAR.get()
_ = model_version_var.get()

0 comments on commit 3701066

Please sign in to comment.