langchain-qdrant[patch]: Add ruff bandit rules to linter (#31815)

- Add ruff bandit rules
- Address a few s101s
- Some formatting
This commit is contained in:
Mason Daugherty 2025-07-01 14:42:55 -04:00 committed by GitHub
parent b03e326231
commit 86a698d1b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 40 deletions

View File

@ -230,7 +230,7 @@ class QdrantVectorStore(VectorStore):
"""Get the Qdrant client instance that is being used. """Get the Qdrant client instance that is being used.
Returns: Returns:
QdrantClient: An instance of `QdrantClient`. QdrantClient: An instance of ``QdrantClient``.
""" """
return self._client return self._client
@ -239,10 +239,10 @@ class QdrantVectorStore(VectorStore):
"""Get the dense embeddings instance that is being used. """Get the dense embeddings instance that is being used.
Raises: Raises:
ValueError: If embeddings are `None`. ValueError: If embeddings are ``None``.
Returns: Returns:
Embeddings: An instance of `Embeddings`. Embeddings: An instance of ``Embeddings``.
""" """
if self._embeddings is None: if self._embeddings is None:
raise ValueError( raise ValueError(
@ -255,10 +255,10 @@ class QdrantVectorStore(VectorStore):
"""Get the sparse embeddings instance that is being used. """Get the sparse embeddings instance that is being used.
Raises: Raises:
ValueError: If sparse embeddings are `None`. ValueError: If sparse embeddings are ``None``.
Returns: Returns:
SparseEmbeddings: An instance of `SparseEmbeddings`. SparseEmbeddings: An instance of ``SparseEmbeddings``.
""" """
if self._sparse_embeddings is None: if self._sparse_embeddings is None:
raise ValueError( raise ValueError(
@ -302,7 +302,7 @@ class QdrantVectorStore(VectorStore):
validate_collection_config: bool = True, validate_collection_config: bool = True,
**kwargs: Any, **kwargs: Any,
) -> QdrantVectorStore: ) -> QdrantVectorStore:
"""Construct an instance of `QdrantVectorStore` from a list of texts. """Construct an instance of ``QdrantVectorStore`` from a list of texts.
This is a user-friendly interface that: This is a user-friendly interface that:
1. Creates embeddings, one for each text 1. Creates embeddings, one for each text
@ -382,11 +382,11 @@ class QdrantVectorStore(VectorStore):
validate_collection_config: bool = True, validate_collection_config: bool = True,
**kwargs: Any, **kwargs: Any,
) -> QdrantVectorStore: ) -> QdrantVectorStore:
"""Construct an instance of `QdrantVectorStore` from an existing collection """Construct an instance of ``QdrantVectorStore`` from an existing collection
without adding any data. without adding any data.
Returns: Returns:
QdrantVectorStore: A new instance of `QdrantVectorStore`. QdrantVectorStore: A new instance of ``QdrantVectorStore``.
""" """
client = QdrantClient( client = QdrantClient(
location=location, location=location,
@ -660,7 +660,6 @@ class QdrantVectorStore(VectorStore):
Maximal marginal relevance optimizes for similarity to query AND diversity Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents. among selected documents.
Returns: Returns:
List of Documents selected by maximal marginal relevance. List of Documents selected by maximal marginal relevance.
""" """
@ -911,16 +910,16 @@ class QdrantVectorStore(VectorStore):
@staticmethod @staticmethod
def _cosine_relevance_score_fn(distance: float) -> float: def _cosine_relevance_score_fn(distance: float) -> float:
"""Normalize the distance to a score on a scale [0, 1].""" """Normalize the distance to a score on a scale ``[0, 1]``."""
return (distance + 1.0) / 2.0 return (distance + 1.0) / 2.0
def _select_relevance_score_fn(self) -> Callable[[float], float]: def _select_relevance_score_fn(self) -> Callable[[float], float]:
""" """
The 'correct' relevance function The "correct" relevance function may differ depending on a few things,
may differ depending on a few things, including: including:
- the distance / similarity metric used by the VectorStore - The distance / similarity metric used by the VectorStore
- the scale of your embeddings (OpenAI's are unit normed. Many others are not!) - The scale of your embeddings (OpenAI's are unit normed. Many others are not!)
- embedding dimensionality - Embedding dimensionality
- etc. - etc.
""" """
@ -1039,9 +1038,10 @@ class QdrantVectorStore(VectorStore):
dense_embeddings = self.embeddings.embed_documents(list(texts)) dense_embeddings = self.embeddings.embed_documents(list(texts))
sparse_embeddings = self.sparse_embeddings.embed_documents(list(texts)) sparse_embeddings = self.sparse_embeddings.embed_documents(list(texts))
assert len(dense_embeddings) == len( if len(dense_embeddings) != len(sparse_embeddings):
sparse_embeddings raise ValueError(
), "Mismatched length between dense and sparse embeddings." "Mismatched length between dense and sparse embeddings."
)
return [ return [
{ {
@ -1128,7 +1128,8 @@ class QdrantVectorStore(VectorStore):
"set `force_recreate` to `True`." "set `force_recreate` to `True`."
) )
assert vector_config is not None, "VectorParams is None" if vector_config is None:
raise ValueError("VectorParams is None")
if isinstance(dense_embeddings, Embeddings): if isinstance(dense_embeddings, Embeddings):
vector_size = len(dense_embeddings.embed_documents(["dummy_text"])[0]) vector_size = len(dense_embeddings.embed_documents(["dummy_text"])[0])

View File

@ -105,7 +105,7 @@ class Qdrant(VectorStore):
if embeddings is None and embedding_function is None: if embeddings is None and embedding_function is None:
raise ValueError( raise ValueError(
"`embeddings` value can't be None. Pass `Embeddings` instance." "`embeddings` value can't be None. Pass `embeddings` instance."
) )
if embeddings is not None and embedding_function is not None: if embeddings is not None and embedding_function is not None:
@ -161,7 +161,7 @@ class Qdrant(VectorStore):
uuid-like strings. uuid-like strings.
batch_size: batch_size:
How many vectors upload per-request. How many vectors upload per-request.
Default: 64 Default: ``64``
Returns: Returns:
List of ids from adding the texts into the vectorstore. List of ids from adding the texts into the vectorstore.
@ -196,7 +196,7 @@ class Qdrant(VectorStore):
uuid-like strings. uuid-like strings.
batch_size: batch_size:
How many vectors upload per-request. How many vectors upload per-request.
Default: 64 Default: ``64``
Returns: Returns:
List of ids from adding the texts into the vectorstore. List of ids from adding the texts into the vectorstore.
@ -1669,7 +1669,8 @@ class Qdrant(VectorStore):
f"`None`. If you want to recreate the collection, set " f"`None`. If you want to recreate the collection, set "
f"`force_recreate` parameter to `True`." f"`force_recreate` parameter to `True`."
) )
assert isinstance(current_vector_config, models.VectorParams), ( if not isinstance(current_vector_config, models.VectorParams):
raise ValueError(
"Expected current_vector_config to be an instance of " "Expected current_vector_config to be an instance of "
f"models.VectorParams, but got {type(current_vector_config)}" f"models.VectorParams, but got {type(current_vector_config)}"
) )
@ -1829,8 +1830,8 @@ class Qdrant(VectorStore):
f"`None`. If you want to recreate the collection, set " f"`None`. If you want to recreate the collection, set "
f"`force_recreate` parameter to `True`." f"`force_recreate` parameter to `True`."
) )
if not isinstance(current_vector_config, models.VectorParams):
assert isinstance(current_vector_config, models.VectorParams), ( raise ValueError(
"Expected current_vector_config to be an instance of " "Expected current_vector_config to be an instance of "
f"models.VectorParams, but got {type(current_vector_config)}" f"models.VectorParams, but got {type(current_vector_config)}"
) )

View File

@ -51,7 +51,7 @@ langchain-core = { path = "../../core", editable = true }
target-version = "py39" target-version = "py39"
[tool.ruff.lint] [tool.ruff.lint]
select = ["E", "F", "I", "T201", "UP"] select = ["E", "F", "I", "T201", "UP", "S"]
ignore = [ "UP007", ] ignore = [ "UP007", ]
[tool.mypy] [tool.mypy]
@ -67,3 +67,9 @@ markers = [
"compile: mark placeholder test used to compile integration tests without running them", "compile: mark placeholder test used to compile integration tests without running them",
] ]
asyncio_mode = "auto" asyncio_mode = "auto"
[tool.ruff.lint.extend-per-file-ignores]
"tests/**/*.py" = [
"S101", # Tests need assertions
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
]