ruff: more rules across the board & fixes (#31898)

* standardizes ruff dep version across all `pyproject.toml` files
* cli: ruff rules and corrections
* langchain: rules and corrections
This commit is contained in:
Mason Daugherty
2025-07-07 17:48:01 -04:00
committed by GitHub
parent 706a66eccd
commit e7eac27241
408 changed files with 2783 additions and 1671 deletions

View File

@@ -6,7 +6,7 @@ class File:
self.name = name
self.content = "\n".join(content or [])
def __eq__(self, __value: object) -> bool:
def __eq__(self, __value: object, /) -> bool:
if not isinstance(__value, File):
return NotImplemented

View File

@@ -34,12 +34,12 @@ class Folder:
files.append(cls.from_structure(path))
else:
files.append(
File(path.name, path.read_text(encoding="utf-8").splitlines())
File(path.name, path.read_text(encoding="utf-8").splitlines()),
)
return Folder(name, *files)
def __eq__(self, __value: object) -> bool:
def __eq__(self, __value: object, /) -> bool:
if isinstance(__value, File):
return False

View File

@@ -34,7 +34,7 @@ def find_issue(current: Folder, expected: Folder) -> str:
expected_file.content.splitlines(),
fromfile=current_file.name,
tofile=expected_file.name,
)
),
)
return "Unknown"
@@ -47,8 +47,10 @@ def test_command_line(tmp_path: Path) -> None:
before.create_structure(root=Path(td))
# The input is used to force through the confirmation.
result = runner.invoke(app, ["migrate", before.name, "--force"])
assert result.exit_code == 0, result.output
if result.exit_code != 0:
raise RuntimeError(result.output)
after = Folder.from_structure(Path(td) / before.name)
assert after == expected, find_issue(after, expected)
if after != expected:
raise ValueError(find_issue(after, expected))

View File

@@ -12,14 +12,15 @@ def test_create_json_agent_migration() -> None:
"""Test the migration of create_json_agent from langchain to langchain_community."""
with sup1(), sup2():
raw_migrations = generate_simplified_migrations(
from_package="langchain", to_package="langchain_community"
from_package="langchain",
to_package="langchain_community",
)
json_agent_migrations = [
migration
for migration in raw_migrations
if "create_json_agent" in migration[0]
]
assert json_agent_migrations == [
if json_agent_migrations != [
(
"langchain.agents.create_json_agent",
"langchain_community.agent_toolkits.create_json_agent",
@@ -32,7 +33,9 @@ def test_create_json_agent_migration() -> None:
"langchain.agents.agent_toolkits.json.base.create_json_agent",
"langchain_community.agent_toolkits.create_json_agent",
),
]
]:
msg = "json_agent_migrations did not match the expected value"
raise ValueError(msg)
@pytest.mark.xfail(reason="Unknown reason")
@@ -40,15 +43,21 @@ def test_create_single_store_retriever_db() -> None:
"""Test migration from langchain to langchain_core."""
with sup1(), sup2():
raw_migrations = generate_simplified_migrations(
from_package="langchain", to_package="langchain_core"
from_package="langchain",
to_package="langchain_core",
)
# SingleStore was an old name for VectorStoreRetriever
single_store_migration = [
migration for migration in raw_migrations if "SingleStore" in migration[0]
]
assert single_store_migration == [
if single_store_migration != [
(
"langchain.vectorstores.singlestoredb.SingleStoreDBRetriever",
"langchain_core.vectorstores.VectorStoreRetriever",
),
]
]:
msg = (
"Unexpected migration: single_store_migration does not match expected "
"value"
)
raise ValueError(msg)

View File

@@ -9,7 +9,7 @@ pytest.importorskip(modname="langchain_openai")
def test_generate_migrations() -> None:
migrations = get_migrations_for_partner_package("langchain_openai")
assert migrations == [
if migrations != [
("langchain_community.llms.openai.OpenAI", "langchain_openai.OpenAI"),
("langchain_community.llms.openai.AzureOpenAI", "langchain_openai.AzureOpenAI"),
(
@@ -43,4 +43,6 @@ def test_generate_migrations() -> None:
"langchain_openai.AzureChatOpenAI",
),
("langchain_community.chat_models.ChatOpenAI", "langchain_openai.ChatOpenAI"),
]
]:
msg = "Migrations do not match expected result"
raise ValueError(msg)

View File

@@ -2,4 +2,6 @@ from langchain_cli.namespaces.migrate.generate.utils import PKGS_ROOT
def test_root() -> None:
assert PKGS_ROOT.name == "libs"
if PKGS_ROOT.name != "libs":
msg = "Expected PKGS_ROOT.name to be 'libs'."
raise ValueError(msg)

View File

@@ -5,6 +5,7 @@ from langchain_cli.utils.events import EventDict, create_events
@pytest.mark.xfail(reason="Unknown reason")
def test_create_events() -> None:
assert create_events(
[EventDict(event="Test Event", properties={"test": "test"})]
) == {"status": "success"}
result = create_events([EventDict(event="Test Event", properties={"test": "test"})])
if result != {"status": "success"}:
msg = "Expected {'status': 'success'}, got " + repr(result)
raise ValueError(msg)

View File

@@ -18,17 +18,37 @@ def _assert_dependency_equals(
subdirectory: Optional[str] = None,
event_metadata: Optional[dict] = None,
) -> None:
assert dep["git"] == git
assert dep["ref"] == ref
assert dep["subdirectory"] == subdirectory
if event_metadata is not None:
assert dep["event_metadata"] == event_metadata
if dep["git"] != git:
msg = f"Expected git to be {git} but got {dep['git']}"
raise ValueError(msg)
if dep["ref"] != ref:
msg = f"Expected ref to be {ref} but got {dep['ref']}"
raise ValueError(msg)
if dep["subdirectory"] != subdirectory:
msg = (
f"Expected subdirectory to be {subdirectory} but got {dep['subdirectory']}"
)
raise ValueError(msg)
if dep["subdirectory"] != subdirectory:
msg = (
f"Expected subdirectory to be {subdirectory} but got {dep['subdirectory']}"
)
raise ValueError(msg)
if event_metadata is not None and dep["event_metadata"] != event_metadata:
msg = (
f"Expected event_metadata to be {event_metadata} "
f"but got {dep['event_metadata']}"
)
raise ValueError(msg)
def test_dependency_string() -> None:
_assert_dependency_equals(
parse_dependency_string(
"git+ssh://git@github.com/efriis/myrepo.git", None, None, None
"git+ssh://git@github.com/efriis/myrepo.git",
None,
None,
None,
),
git="ssh://git@github.com/efriis/myrepo.git",
ref=None,
@@ -49,7 +69,10 @@ def test_dependency_string() -> None:
_assert_dependency_equals(
parse_dependency_string(
"git+ssh://git@github.com:efriis/myrepo.git#develop", None, None, None
"git+ssh://git@github.com:efriis/myrepo.git#develop",
None,
None,
None,
),
git="ssh://git@github.com:efriis/myrepo.git",
ref="develop",
@@ -59,7 +82,10 @@ def test_dependency_string() -> None:
# also support a slash in ssh
_assert_dependency_equals(
parse_dependency_string(
"git+ssh://git@github.com/efriis/myrepo.git#develop", None, None, None
"git+ssh://git@github.com/efriis/myrepo.git#develop",
None,
None,
None,
),
git="ssh://git@github.com/efriis/myrepo.git",
ref="develop",
@@ -69,7 +95,10 @@ def test_dependency_string() -> None:
# looks like poetry supports both an @ and a #
_assert_dependency_equals(
parse_dependency_string(
"git+ssh://git@github.com:efriis/myrepo.git@develop", None, None, None
"git+ssh://git@github.com:efriis/myrepo.git@develop",
None,
None,
None,
),
git="ssh://git@github.com:efriis/myrepo.git",
ref="develop",
@@ -100,7 +129,8 @@ def test_dependency_string_both() -> None:
def test_dependency_string_invalids() -> None:
# expect error for wrong order
with pytest.raises(ValueError):
# Bypassing validation since the ValueError message is dynamic
with pytest.raises(ValueError): # noqa: PT011
parse_dependency_string(
"git+https://github.com/efriis/myrepo.git#subdirectory=src@branch",
None,