Skip to content

The agent engineering platform.

core: Add ruff rules PGH (except PGH003) (#30656)

Christophe Bornetf0159c7125f7
diff --git a/libs/core/langchain_core/globals.py b/libs/core/langchain_core/globals.py
index 2e0daad765..47480f85e2 100644
--- a/libs/core/langchain_core/globals.py
+++ b/libs/core/langchain_core/globals.py
@@ -1,4 +1,3 @@
-# flake8: noqa
"""Global values and configuration that apply to all of LangChain."""
import warnings
@@ -27,25 +26,27 @@ def set_verbose(value: bool) -> None:
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=(
- "Importing verbose from langchain root module is no longer supported"
+ "Importing verbose from langchain root module "
+ "is no longer supported"
),
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
- # Remove it once `langchain.verbose` is no longer supported, and once all users
- # have migrated to using `set_verbose()` here.
+ # Remove it once `langchain.verbose` is no longer supported, and once all
+ # users have migrated to using `set_verbose()` here.
langchain.verbose = value
except ImportError:
pass
- global _verbose
+ global _verbose # noqa: PLW0603
_verbose = value
@@ -59,30 +60,32 @@ def get_verbose() -> bool:
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=(
- ".*Importing verbose from langchain root module is no longer supported"
+ ".*Importing verbose from langchain root module "
+ "is no longer supported"
),
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
- # Remove it once `langchain.verbose` is no longer supported, and once all users
- # have migrated to using `set_verbose()` here.
+ # Remove it once `langchain.verbose` is no longer supported, and once all
+ # users have migrated to using `set_verbose()` here.
#
- # In the meantime, the `verbose` setting is considered True if either the old
- # or the new value are True. This accommodates users who haven't migrated
- # to using `set_verbose()` yet. Those users are getting deprecation warnings
- # directing them to use `set_verbose()` when they import `langchain.verbose`.
+ # In the meantime, the `verbose` setting is considered True if either the
+ # old or the new value are True. This accommodates users who haven't
+ # migrated to using `set_verbose()` yet. Those users are getting
+ # deprecation warnings directing them to use `set_verbose()` when they
+ # import `langchain.verbose`.
old_verbose = langchain.verbose
except ImportError:
old_verbose = False
- global _verbose
return _verbose or old_verbose
@@ -96,23 +99,25 @@ def set_debug(value: bool) -> None:
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
- message="Importing debug from langchain root module is no longer supported",
+ message="Importing debug from langchain root module "
+ "is no longer supported",
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
- # Remove it once `langchain.debug` is no longer supported, and once all users
- # have migrated to using `set_debug()` here.
+ # Remove it once `langchain.debug` is no longer supported, and once all
+ # users have migrated to using `set_debug()` here.
langchain.debug = value
except ImportError:
pass
- global _debug
+ global _debug # noqa: PLW0603
_debug = value
@@ -126,18 +131,20 @@ def get_debug() -> bool:
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
- message="Importing debug from langchain root module is no longer supported",
+ message="Importing debug from langchain root module "
+ "is no longer supported",
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
- # Remove it once `langchain.debug` is no longer supported, and once all users
- # have migrated to using `set_debug()` here.
+ # Remove it once `langchain.debug` is no longer supported, and once all
+ # users have migrated to using `set_debug()` here.
#
# In the meantime, the `debug` setting is considered True if either the old
# or the new value are True. This accommodates users who haven't migrated
@@ -147,7 +154,6 @@ def get_debug() -> bool:
except ImportError:
old_debug = False
- global _debug
return _debug or old_debug
@@ -161,12 +167,14 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=(
- "Importing llm_cache from langchain root module is no longer supported"
+ "Importing llm_cache from langchain root module "
+ "is no longer supported"
),
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
@@ -179,7 +187,7 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
except ImportError:
pass
- global _llm_cache
+ global _llm_cache # noqa: PLW0603
_llm_cache = value
@@ -193,12 +201,14 @@ def get_llm_cache() -> "BaseCache":
import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it.
- # The user called the correct (non-deprecated) code path and shouldn't get warnings.
+ # The user called the correct (non-deprecated) code path and shouldn't get
+ # warnings.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=(
- "Importing llm_cache from langchain root module is no longer supported"
+ "Importing llm_cache from langchain root module "
+ "is no longer supported"
),
)
# N.B.: This is a workaround for an unfortunate quirk of Python's
@@ -218,5 +228,4 @@ def get_llm_cache() -> "BaseCache":
except ImportError:
old_llm_cache = None
- global _llm_cache
return _llm_cache or old_llm_cache
diff --git a/libs/core/langchain_core/output_parsers/format_instructions.py b/libs/core/langchain_core/output_parsers/format_instructions.py
index 400756e866..8ad789bcd6 100644
--- a/libs/core/langchain_core/output_parsers/format_instructions.py
+++ b/libs/core/langchain_core/output_parsers/format_instructions.py
@@ -1,4 +1,4 @@
-# flake8: noqa
+"""Format instructions."""
JSON_FORMAT_INSTRUCTIONS = """The output should be formatted as a JSON instance that conforms to the JSON schema below.
@@ -8,4 +8,4 @@ the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema.
Here is the output schema:
```
{schema}
-```"""
+```""" # noqa: E501
diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py
index e9165f90da..bad0d2fb69 100644
--- a/libs/core/langchain_core/prompts/string.py
+++ b/libs/core/langchain_core/prompts/string.py
@@ -97,7 +97,6 @@ def _get_jinja2_variables_from_template(template: str) -> set[str]:
"Please install it with `pip install jinja2`."
)
raise ImportError(msg) from e
- # noqa for insecure warning elsewhere
env = Environment() # noqa: S701
ast = env.parse(template)
return meta.find_undeclared_variables(ast)
diff --git a/libs/core/langchain_core/tracers/base.py b/libs/core/langchain_core/tracers/base.py
index d075abf263..38795d8e0f 100644
--- a/libs/core/langchain_core/tracers/base.py
+++ b/libs/core/langchain_core/tracers/base.py
@@ -15,7 +15,7 @@ from typing import (
from typing_extensions import override
from langchain_core.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler
-from langchain_core.exceptions import TracerException # noqa
+from langchain_core.exceptions import TracerException # noqa: F401
from langchain_core.tracers.core import _TracerCore
if TYPE_CHECKING:
diff --git a/libs/core/pyproject.toml b/libs/core/pyproject.toml
index 0f19d7b4f5..a7aa0452d5 100644
--- a/libs/core/pyproject.toml
+++ b/libs/core/pyproject.toml
@@ -101,7 +101,7 @@ ignore = [
"ERA",
"FBT001",
"FBT002",
- "PGH",
+ "PGH003",
"PLR",
"PYI",
"RUF",
diff --git a/libs/core/tests/unit_tests/runnables/test_imports.py b/libs/core/tests/unit_tests/runnables/test_imports.py
index e0a6ac47bb..e458cca21b 100644
--- a/libs/core/tests/unit_tests/runnables/test_imports.py
+++ b/libs/core/tests/unit_tests/runnables/test_imports.py
@@ -40,4 +40,4 @@ def test_all_imports() -> None:
def test_imports_for_specific_funcs() -> None:
"""Test that a few specific imports in more internal namespaces."""
# create_model implementation has been moved to langchain_core.utils.pydantic
- from langchain_core.runnables.utils import create_model # noqa
+ from langchain_core.runnables.utils import create_model # noqa: F401
diff --git a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
index 646df61fab..31ad313827 100644
--- a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
+++ b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py
@@ -444,7 +444,7 @@ def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
metadata={"some_foo": "some_bar"},
tags=["afoo"],
):
- collected: dict[str, RunTree] = {} # noqa
+ collected: dict[str, RunTree] = {}
def collect_run(run: RunTree) -> None:
collected[str(run.id)] = run