Skip to content

Commit 33c9bf1

Browse files
authored
langchain-openai[patch]: Add ruff bandit rules to linter (#31788)
1 parent 645e25f commit 33c9bf1

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

libs/partners/openai/langchain_openai/chat_models/_client_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __del__(self) -> None:
2323

2424
try:
2525
self.close()
26-
except Exception:
26+
except Exception: # noqa: S110
2727
pass
2828

2929

@@ -37,7 +37,7 @@ def __del__(self) -> None:
3737
try:
3838
# TODO(someday): support non asyncio runtimes here
3939
asyncio.get_running_loop().create_task(self.aclose())
40-
except Exception:
40+
except Exception: # noqa: S110
4141
pass
4242

4343

libs/partners/openai/langchain_openai/llms/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def _generate(
310310
generation = chunk
311311
else:
312312
generation += chunk
313-
assert generation is not None
313+
if generation is None:
314+
raise ValueError("Generation is empty after streaming.")
314315
choices.append(
315316
{
316317
"text": generation.text,
@@ -378,7 +379,8 @@ async def _agenerate(
378379
generation = chunk
379380
else:
380381
generation += chunk
381-
assert generation is not None
382+
if generation is None:
383+
raise ValueError("Generation is empty after streaming.")
382384
choices.append(
383385
{
384386
"text": generation.text,

libs/partners/openai/pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ignore_missing_imports = true
6464
target-version = "py39"
6565

6666
[tool.ruff.lint]
67-
select = ["E", "F", "I", "T201", "UP"]
67+
select = ["E", "F", "I", "T201", "UP", "S"]
6868
ignore = [ "UP007", ]
6969

7070
[tool.ruff.format]
@@ -85,3 +85,9 @@ asyncio_mode = "auto"
8585
filterwarnings = [
8686
"ignore::langchain_core._api.beta_decorator.LangChainBetaWarning",
8787
]
88+
89+
[tool.ruff.lint.extend-per-file-ignores]
90+
"tests/**/*.py" = [
91+
"S101", # Tests need assertions
92+
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
93+
]

libs/partners/openai/tests/unit_tests/test_secrets.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
OpenAIEmbeddings,
1515
)
1616

17+
AZURE_AD_TOKEN = "secret-api-key" # noqa: S105
18+
1719

1820
def test_chat_openai_secrets() -> None:
1921
o = ChatOpenAI(openai_api_key="foo") # type: ignore[call-arg]
@@ -37,7 +39,7 @@ def test_azure_chat_openai_secrets() -> None:
3739
o = AzureChatOpenAI( # type: ignore[call-arg]
3840
openai_api_key="foo1",
3941
azure_endpoint="endpoint",
40-
azure_ad_token="foo2", # type: ignore[arg-type]
42+
azure_ad_token=AZURE_AD_TOKEN, # type: ignore[arg-type]
4143
api_version="version",
4244
)
4345
s = str(o)
@@ -49,7 +51,7 @@ def test_azure_openai_secrets() -> None:
4951
o = AzureOpenAI( # type: ignore[call-arg]
5052
openai_api_key="foo1",
5153
azure_endpoint="endpoint",
52-
azure_ad_token="foo2", # type: ignore[arg-type]
54+
azure_ad_token=AZURE_AD_TOKEN, # type: ignore[arg-type]
5355
api_version="version",
5456
)
5557
s = str(o)
@@ -61,7 +63,7 @@ def test_azure_openai_embeddings_secrets() -> None:
6163
o = AzureOpenAIEmbeddings( # type: ignore[call-arg]
6264
openai_api_key="foo1",
6365
azure_endpoint="endpoint",
64-
azure_ad_token="foo2", # type: ignore[arg-type]
66+
azure_ad_token=AZURE_AD_TOKEN, # type: ignore[arg-type]
6567
api_version="version",
6668
)
6769
s = str(o)
@@ -77,7 +79,7 @@ def test_azure_openai_api_key_is_secret_string(model_class: type) -> None:
7779
model = model_class(
7880
openai_api_key="secret-api-key",
7981
azure_endpoint="endpoint",
80-
azure_ad_token="secret-ad-token",
82+
azure_ad_token=AZURE_AD_TOKEN,
8183
api_version="version",
8284
)
8385
assert isinstance(model.openai_api_key, SecretStr)
@@ -115,7 +117,7 @@ def test_azure_openai_api_key_masked_when_passed_via_constructor(
115117
model = model_class(
116118
openai_api_key="secret-api-key",
117119
azure_endpoint="endpoint",
118-
azure_ad_token="secret-ad-token",
120+
azure_ad_token=AZURE_AD_TOKEN,
119121
api_version="version",
120122
)
121123
print(model.openai_api_key, end="") # noqa: T201
@@ -139,11 +141,11 @@ def test_azure_openai_uses_actual_secret_value_from_secretstr(
139141
model = model_class(
140142
openai_api_key="secret-api-key",
141143
azure_endpoint="endpoint",
142-
azure_ad_token="secret-ad-token",
144+
azure_ad_token=AZURE_AD_TOKEN,
143145
api_version="version",
144146
)
145147
assert cast(SecretStr, model.openai_api_key).get_secret_value() == "secret-api-key"
146-
assert cast(SecretStr, model.azure_ad_token).get_secret_value() == "secret-ad-token"
148+
assert cast(SecretStr, model.azure_ad_token).get_secret_value() == AZURE_AD_TOKEN
147149

148150

149151
@pytest.mark.parametrize("model_class", [ChatOpenAI, OpenAI, OpenAIEmbeddings])
@@ -195,7 +197,7 @@ def test_azure_serialized_secrets(model_class: type) -> None:
195197
assert serialized["kwargs"]["openai_api_key"]["id"] == ["AZURE_OPENAI_API_KEY"]
196198

197199
model = model_class(
198-
azure_ad_token="secret-token", api_version="foo", azure_endpoint="foo"
200+
azure_ad_token=AZURE_AD_TOKEN, api_version="foo", azure_endpoint="foo"
199201
)
200202
serialized = dumpd(model)
201203
assert serialized["kwargs"]["azure_ad_token"]["id"] == ["AZURE_OPENAI_AD_TOKEN"]

0 commit comments

Comments
 (0)