Skip to content

Commit 96d60a2

Browse files
committed
fix(typecheck): make the model_name stash env-independent
The `# type: ignore[assignment]` was correct locally and WRONG in CI. torch's Module.__setattr__ is typed Tensor|Module, so `model.model_name = <str>` only needs silencing when sentence-transformers/torch is actually installed; without it the name is untyped and the ignore becomes an unused-ignore error. The gate therefore failed in whichever environment you did not develop in — exactly the local-vs-CI drift TOOL-PINS exists to catch, arriving by a different door. setattr() is not checked against __setattr__ overloads, so it is clean either way, and it is the honest spelling: the attribute is dynamic, declared by neither SentenceTransformer nor torch. mypy clean over 365 files locally (torch present); no ignore left to go unused where it is absent.
1 parent 5b84398 commit 96d60a2

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

signalwire/signalwire/search/query_processor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ def _get_cached_model(model_name: str | None = None) -> Any:
291291

292292
logger.info(f"Loading sentence transformer model: {model_name}")
293293
model = SentenceTransformer(model_name)
294-
# Store the model name for identification. torch's Module.__setattr__
295-
# is typed for Tensor|Module, so stashing a plain str on the instance
296-
# is a third-party-stub gap, not a real type error.
297-
model.model_name = model_name # type: ignore[assignment]
294+
# Stash the name for identification. Dynamic: SentenceTransformer
295+
# does not declare it, and torch's Module.__setattr__ is typed
296+
# Tensor|Module, so a direct assignment only type-checks when torch
297+
# is absent — setattr is correct whether or not it is installed.
298+
setattr(model, "model_name", model_name)
298299
# Evict oldest entry if cache is full
299300
if len(_model_cache) >= _MAX_MODEL_CACHE_SIZE:
300301
oldest_key = next(iter(_model_cache))

signalwire/signalwire/search/search_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ def _load_resources(self) -> None:
528528
)
529529
try:
530530
model = SentenceTransformer(model_name)
531-
# Store for cache comparison; torch's
532-
# Module.__setattr__ is typed Tensor|Module, so a
533-
# plain str is a stub gap, not a real error.
534-
model.model_name = model_name # type: ignore[assignment]
531+
# Stash for cache comparison. Dynamic — see the
532+
# note in query_processor._load_model: a direct
533+
# assignment only type-checks when torch is absent.
534+
setattr(model, "model_name", model_name)
535535
self.models[model_name] = model
536536
except Exception as e:
537537
logger.error(f"Failed to load model {model_name}: {e}")

0 commit comments

Comments
 (0)