Skip to content

Commit b8e9b4a

Browse files
cborneteyurtsev
andauthored
cli: Add ruff rule UP (pyupgrade) (#31843)
See https://docs.astral.sh/ruff/rules/#pyupgrade-up All auto-fixed Co-authored-by: Eugene Yurtsev <[email protected]>
1 parent cd7dce6 commit b8e9b4a

File tree

17 files changed

+61
-66
lines changed

17 files changed

+61
-66
lines changed

libs/cli/langchain_cli/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Optional
1+
from typing import Annotated, Optional
22

33
import typer
4-
from typing_extensions import Annotated
54

65
from langchain_cli._version import __version__
76
from langchain_cli.namespaces import app as app_namespace

libs/cli/langchain_cli/dev_scripts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Development Scripts for template packages
44
"""
55

6-
from typing import Sequence
6+
from collections.abc import Sequence
77

88
from fastapi import FastAPI
99
from langserve import add_routes

libs/cli/langchain_cli/namespaces/app.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import sys
88
import warnings
99
from pathlib import Path
10-
from typing import Dict, List, Optional, Tuple
10+
from typing import Annotated, Optional
1111

1212
import typer
13-
from typing_extensions import Annotated
1413

1514
from langchain_cli.utils.events import create_events
1615
from langchain_cli.utils.git import (
@@ -44,7 +43,7 @@ def new(
4443
] = None,
4544
*,
4645
package: Annotated[
47-
Optional[List[str]],
46+
Optional[list[str]],
4847
typer.Option(help="Packages to seed the project with"),
4948
] = None,
5049
pip: Annotated[
@@ -132,19 +131,19 @@ def new(
132131
@app_cli.command()
133132
def add(
134133
dependencies: Annotated[
135-
Optional[List[str]], typer.Argument(help="The dependency to add")
134+
Optional[list[str]], typer.Argument(help="The dependency to add")
136135
] = None,
137136
*,
138-
api_path: Annotated[List[str], typer.Option(help="API paths to add")] = [],
137+
api_path: Annotated[list[str], typer.Option(help="API paths to add")] = [],
139138
project_dir: Annotated[
140139
Optional[Path], typer.Option(help="The project directory")
141140
] = None,
142141
repo: Annotated[
143-
List[str],
142+
list[str],
144143
typer.Option(help="Install templates from a specific github repo instead"),
145144
] = [],
146145
branch: Annotated[
147-
List[str], typer.Option(help="Install templates from a specific branch")
146+
list[str], typer.Option(help="Install templates from a specific branch")
148147
] = [],
149148
pip: Annotated[
150149
bool,
@@ -181,16 +180,16 @@ def add(
181180
)
182181

183182
# group by repo/ref
184-
grouped: Dict[Tuple[str, Optional[str]], List[DependencySource]] = {}
183+
grouped: dict[tuple[str, Optional[str]], list[DependencySource]] = {}
185184
for dep in parsed_deps:
186185
key_tup = (dep["git"], dep["ref"])
187186
lst = grouped.get(key_tup, [])
188187
lst.append(dep)
189188
grouped[key_tup] = lst
190189

191-
installed_destination_paths: List[Path] = []
192-
installed_destination_names: List[str] = []
193-
installed_exports: List[LangServeExport] = []
190+
installed_destination_paths: list[Path] = []
191+
installed_destination_names: list[str] = []
192+
installed_exports: list[LangServeExport] = []
194193

195194
for (git, ref), group_deps in grouped.items():
196195
if len(group_deps) == 1:
@@ -295,7 +294,7 @@ def add(
295294

296295
@app_cli.command()
297296
def remove(
298-
api_paths: Annotated[List[str], typer.Argument(help="The API paths to remove")],
297+
api_paths: Annotated[list[str], typer.Argument(help="The API paths to remove")],
299298
*,
300299
project_dir: Annotated[
301300
Optional[Path], typer.Option(help="The project directory")
@@ -311,7 +310,7 @@ def remove(
311310

312311
package_root = project_root / "packages"
313312

314-
remove_deps: List[str] = []
313+
remove_deps: list[str] = []
315314

316315
for api_path in api_paths:
317316
package_dir = package_root / api_path

libs/cli/langchain_cli/namespaces/integration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import shutil
77
import subprocess
88
from pathlib import Path
9-
from typing import Dict, Optional, cast
9+
from typing import Annotated, Optional, cast
1010

1111
import typer
12-
from typing_extensions import Annotated, TypedDict
12+
from typing_extensions import TypedDict
1313

1414
from langchain_cli.utils.find_replace import replace_file, replace_glob
1515

@@ -123,7 +123,7 @@ def new(
123123
shutil.move(destination_dir / "integration_template", package_dir)
124124

125125
# replacements in files
126-
replace_glob(destination_dir, "**/*", cast(Dict[str, str], replacements))
126+
replace_glob(destination_dir, "**/*", cast(dict[str, str], replacements))
127127

128128
# poetry install
129129
subprocess.run(
@@ -167,7 +167,7 @@ def new(
167167

168168
for src_path, dst_path in zip(src_paths, dst_paths):
169169
shutil.copy(src_path, dst_path)
170-
replace_file(dst_path, cast(Dict[str, str], replacements))
170+
replace_file(dst_path, cast(dict[str, str], replacements))
171171

172172

173173
TEMPLATE_MAP: dict[str, str] = {

libs/cli/langchain_cli/namespaces/migrate/generate/generic.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import importlib
44
import inspect
55
import pkgutil
6-
from typing import List, Tuple
76

87

98
def generate_raw_migrations(
109
from_package: str, to_package: str, filter_by_all: bool = False
11-
) -> List[Tuple[str, str]]:
10+
) -> list[tuple[str, str]]:
1211
"""Scan the `langchain` package and generate migrations for all modules."""
1312
package = importlib.import_module(from_package)
1413

@@ -56,7 +55,7 @@ def generate_raw_migrations(
5655
return items
5756

5857

59-
def generate_top_level_imports(pkg: str) -> List[Tuple[str, str]]:
58+
def generate_top_level_imports(pkg: str) -> list[tuple[str, str]]:
6059
"""This code will look at all the top level modules in langchain_community.
6160
6261
It'll attempt to import everything from each __init__ file
@@ -121,7 +120,7 @@ def handle_module(module, module_name):
121120

122121
def generate_simplified_migrations(
123122
from_package: str, to_package: str, filter_by_all: bool = True
124-
) -> List[Tuple[str, str]]:
123+
) -> list[tuple[str, str]]:
125124
"""Get all the raw migrations, then simplify them if possible."""
126125
raw_migrations = generate_raw_migrations(
127126
from_package, to_package, filter_by_all=filter_by_all

libs/cli/langchain_cli/namespaces/migrate/generate/grit.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from typing import List, Tuple
2-
3-
4-
def split_package(package: str) -> Tuple[str, str]:
1+
def split_package(package: str) -> tuple[str, str]:
52
"""Split a package name into the containing package and the final name"""
63
parts = package.split(".")
74
return ".".join(parts[:-1]), parts[-1]
85

96

10-
def dump_migrations_as_grit(name: str, migration_pairs: List[Tuple[str, str]]):
7+
def dump_migrations_as_grit(name: str, migration_pairs: list[tuple[str, str]]):
118
"""Dump the migration pairs as a Grit file."""
129
output = "language python"
1310
remapped = ",\n".join(

libs/cli/langchain_cli/namespaces/migrate/generate/partner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Generate migrations for partner packages."""
22

33
import importlib
4-
from typing import List, Tuple
54

65
from langchain_core.documents import BaseDocumentCompressor, BaseDocumentTransformer
76
from langchain_core.embeddings import Embeddings
@@ -19,7 +18,7 @@
1918
# PUBLIC API
2019

2120

22-
def get_migrations_for_partner_package(pkg_name: str) -> List[Tuple[str, str]]:
21+
def get_migrations_for_partner_package(pkg_name: str) -> list[tuple[str, str]]:
2322
"""Generate migrations from community package to partner package.
2423
2524
This code works

libs/cli/langchain_cli/namespaces/migrate/generate/utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import pathlib
55
from pathlib import Path
6-
from typing import Any, List, Optional, Tuple, Type
6+
from typing import Any, Optional
77

88
HERE = Path(__file__).parent
99
# Should bring us to [root]/src
@@ -29,7 +29,7 @@ def visit_ImportFrom(self, node):
2929
self.generic_visit(node)
3030

3131

32-
def _get_class_names(code: str) -> List[str]:
32+
def _get_class_names(code: str) -> list[str]:
3333
"""Extract class names from a code string."""
3434
# Parse the content of the file into an AST
3535
tree = ast.parse(code)
@@ -49,7 +49,7 @@ def visit_ClassDef(self, node):
4949
return class_names
5050

5151

52-
def is_subclass(class_obj: Any, classes_: List[Type]) -> bool:
52+
def is_subclass(class_obj: Any, classes_: list[type]) -> bool:
5353
"""Check if the given class object is a subclass of any class in list classes."""
5454
return any(
5555
issubclass(class_obj, kls)
@@ -58,7 +58,7 @@ def is_subclass(class_obj: Any, classes_: List[Type]) -> bool:
5858
)
5959

6060

61-
def find_subclasses_in_module(module, classes_: List[Type]) -> List[str]:
61+
def find_subclasses_in_module(module, classes_: list[type]) -> list[str]:
6262
"""Find all classes in the module that inherit from one of the classes."""
6363
subclasses = []
6464
# Iterate over all attributes of the module that are classes
@@ -68,7 +68,7 @@ def find_subclasses_in_module(module, classes_: List[Type]) -> List[str]:
6868
return subclasses
6969

7070

71-
def _get_all_classnames_from_file(file: Path, pkg: str) -> List[Tuple[str, str]]:
71+
def _get_all_classnames_from_file(file: Path, pkg: str) -> list[tuple[str, str]]:
7272
"""Extract all class names from a file."""
7373
with open(file, encoding="utf-8") as f:
7474
code = f.read()
@@ -80,7 +80,7 @@ def _get_all_classnames_from_file(file: Path, pkg: str) -> List[Tuple[str, str]]
8080

8181
def identify_all_imports_in_file(
8282
file: str, *, from_package: Optional[str] = None
83-
) -> List[Tuple[str, str]]:
83+
) -> list[tuple[str, str]]:
8484
"""Let's also identify all the imports in the given file."""
8585
with open(file, encoding="utf-8") as f:
8686
code = f.read()
@@ -103,7 +103,7 @@ def identify_pkg_source(pkg_root: str) -> pathlib.Path:
103103
return matching_dirs[0]
104104

105105

106-
def list_classes_by_package(pkg_root: str) -> List[Tuple[str, str]]:
106+
def list_classes_by_package(pkg_root: str) -> list[tuple[str, str]]:
107107
"""List all classes in a package."""
108108
module_classes = []
109109
pkg_source = identify_pkg_source(pkg_root)
@@ -117,7 +117,7 @@ def list_classes_by_package(pkg_root: str) -> List[Tuple[str, str]]:
117117
return module_classes
118118

119119

120-
def list_init_imports_by_package(pkg_root: str) -> List[Tuple[str, str]]:
120+
def list_init_imports_by_package(pkg_root: str) -> list[tuple[str, str]]:
121121
"""List all the things that are being imported in a package by module."""
122122
imports = []
123123
pkg_source = identify_pkg_source(pkg_root)
@@ -135,7 +135,7 @@ def list_init_imports_by_package(pkg_root: str) -> List[Tuple[str, str]]:
135135

136136
def find_imports_from_package(
137137
code: str, *, from_package: Optional[str] = None
138-
) -> List[Tuple[str, str]]:
138+
) -> list[tuple[str, str]]:
139139
# Parse the code into an AST
140140
tree = ast.parse(code)
141141
# Create an instance of the visitor

libs/cli/langchain_cli/namespaces/template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import shutil
77
import subprocess
88
from pathlib import Path
9-
from typing import Optional
9+
from typing import Annotated, Optional
1010

1111
import typer
12-
from typing_extensions import Annotated
1312

1413
from langchain_cli.utils.packages import get_langserve_export, get_package_root
1514

libs/cli/langchain_cli/utils/events.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import http.client
22
import json
3-
from typing import Any, Dict, List, Optional, TypedDict
3+
from typing import Any, Optional, TypedDict
44

55
WRITE_KEY = "310apTK0HUFl4AOv"
66

77

88
class EventDict(TypedDict):
99
event: str
10-
properties: Optional[Dict[str, Any]]
10+
properties: Optional[dict[str, Any]]
1111

1212

13-
def create_events(events: List[EventDict]) -> Optional[Any]:
13+
def create_events(events: list[EventDict]) -> Optional[Any]:
1414
try:
1515
data = {
1616
"events": [

0 commit comments

Comments
 (0)