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

Add ruff rules PGH: https://docs.astral.sh/ruff/rules/#pygrep-hooks-pgh
Except PGH003 which will be dealt in a dedicated PR.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
This commit is contained in:
Christophe Bornet 2025-04-04 21:53:27 +02:00 committed by GitHub
parent 2491237473
commit f0159c7125
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 46 additions and 38 deletions

View File

@ -1,4 +1,3 @@
# flake8: noqa
"""Global values and configuration that apply to all of LangChain.""" """Global values and configuration that apply to all of LangChain."""
import warnings import warnings
@ -27,25 +26,27 @@ def set_verbose(value: bool) -> None:
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",
message=( 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 # N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation: # module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004 # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
# #
# Remove it once `langchain.verbose` is no longer supported, and once all users # Remove it once `langchain.verbose` is no longer supported, and once all
# have migrated to using `set_verbose()` here. # users have migrated to using `set_verbose()` here.
langchain.verbose = value langchain.verbose = value
except ImportError: except ImportError:
pass pass
global _verbose global _verbose # noqa: PLW0603
_verbose = value _verbose = value
@ -59,30 +60,32 @@ def get_verbose() -> bool:
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",
message=( 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 # N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation: # module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004 # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
# #
# Remove it once `langchain.verbose` is no longer supported, and once all users # Remove it once `langchain.verbose` is no longer supported, and once all
# have migrated to using `set_verbose()` here. # users have migrated to using `set_verbose()` here.
# #
# In the meantime, the `verbose` setting is considered True if either the old # In the meantime, the `verbose` setting is considered True if either the
# or the new value are True. This accommodates users who haven't migrated # old or the new value are True. This accommodates users who haven't
# to using `set_verbose()` yet. Those users are getting deprecation warnings # migrated to using `set_verbose()` yet. Those users are getting
# directing them to use `set_verbose()` when they import `langchain.verbose`. # deprecation warnings directing them to use `set_verbose()` when they
# import `langchain.verbose`.
old_verbose = langchain.verbose old_verbose = langchain.verbose
except ImportError: except ImportError:
old_verbose = False old_verbose = False
global _verbose
return _verbose or old_verbose return _verbose or old_verbose
@ -96,23 +99,25 @@ def set_debug(value: bool) -> None:
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "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 # N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation: # module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004 # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
# #
# Remove it once `langchain.debug` is no longer supported, and once all users # Remove it once `langchain.debug` is no longer supported, and once all
# have migrated to using `set_debug()` here. # users have migrated to using `set_debug()` here.
langchain.debug = value langchain.debug = value
except ImportError: except ImportError:
pass pass
global _debug global _debug # noqa: PLW0603
_debug = value _debug = value
@ -126,18 +131,20 @@ def get_debug() -> bool:
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "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 # N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation: # module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004 # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
# #
# Remove it once `langchain.debug` is no longer supported, and once all users # Remove it once `langchain.debug` is no longer supported, and once all
# have migrated to using `set_debug()` here. # users have migrated to using `set_debug()` here.
# #
# In the meantime, the `debug` setting is considered True if either the old # 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 # or the new value are True. This accommodates users who haven't migrated
@ -147,7 +154,6 @@ def get_debug() -> bool:
except ImportError: except ImportError:
old_debug = False old_debug = False
global _debug
return _debug or old_debug return _debug or old_debug
@ -161,12 +167,14 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",
message=( 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 # 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: except ImportError:
pass pass
global _llm_cache global _llm_cache # noqa: PLW0603
_llm_cache = value _llm_cache = value
@ -193,12 +201,14 @@ def get_llm_cache() -> "BaseCache":
import langchain # type: ignore[import] import langchain # type: ignore[import]
# We're about to run some deprecated code, don't report warnings from it. # 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(): with warnings.catch_warnings():
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",
message=( 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 # N.B.: This is a workaround for an unfortunate quirk of Python's
@ -218,5 +228,4 @@ def get_llm_cache() -> "BaseCache":
except ImportError: except ImportError:
old_llm_cache = None old_llm_cache = None
global _llm_cache
return _llm_cache or old_llm_cache return _llm_cache or old_llm_cache

View File

@ -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. 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: Here is the output schema:
``` ```
{schema} {schema}
```""" ```""" # noqa: E501

View File

@ -97,7 +97,6 @@ def _get_jinja2_variables_from_template(template: str) -> set[str]:
"Please install it with `pip install jinja2`." "Please install it with `pip install jinja2`."
) )
raise ImportError(msg) from e raise ImportError(msg) from e
# noqa for insecure warning elsewhere
env = Environment() # noqa: S701 env = Environment() # noqa: S701
ast = env.parse(template) ast = env.parse(template)
return meta.find_undeclared_variables(ast) return meta.find_undeclared_variables(ast)

View File

@ -15,7 +15,7 @@ from typing import (
from typing_extensions import override from typing_extensions import override
from langchain_core.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler 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 from langchain_core.tracers.core import _TracerCore
if TYPE_CHECKING: if TYPE_CHECKING:

View File

@ -101,7 +101,7 @@ ignore = [
"ERA", "ERA",
"FBT001", "FBT001",
"FBT002", "FBT002",
"PGH", "PGH003",
"PLR", "PLR",
"PYI", "PYI",
"RUF", "RUF",

View File

@ -40,4 +40,4 @@ def test_all_imports() -> None:
def test_imports_for_specific_funcs() -> None: def test_imports_for_specific_funcs() -> None:
"""Test that a few specific imports in more internal namespaces.""" """Test that a few specific imports in more internal namespaces."""
# create_model implementation has been moved to langchain_core.utils.pydantic # 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

View File

@ -444,7 +444,7 @@ def test_tree_is_constructed(parent_type: Literal["ls", "lc"]) -> None:
metadata={"some_foo": "some_bar"}, metadata={"some_foo": "some_bar"},
tags=["afoo"], tags=["afoo"],
): ):
collected: dict[str, RunTree] = {} # noqa collected: dict[str, RunTree] = {}
def collect_run(run: RunTree) -> None: def collect_run(run: RunTree) -> None:
collected[str(run.id)] = run collected[str(run.id)] = run