Skip to content

Commit 66041a2

Browse files
authored
refactor(cli): target ruff 310 (#32985)
Use union types for optional parameters
1 parent ab1b822 commit 66041a2

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

libs/cli/langchain_cli/namespaces/app.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import warnings
77
from pathlib import Path
8-
from typing import Annotated, Optional
8+
from typing import Annotated
99

1010
import typer
1111
import uvicorn
@@ -35,18 +35,18 @@
3535
@app_cli.command()
3636
def new(
3737
name: Annotated[
38-
Optional[str],
38+
str | None,
3939
typer.Argument(
4040
help="The name of the folder to create",
4141
),
4242
] = None,
4343
*,
4444
package: Annotated[
45-
Optional[list[str]],
45+
list[str] | None,
4646
typer.Option(help="Packages to seed the project with"),
4747
] = None,
4848
pip: Annotated[
49-
Optional[bool],
49+
bool | None,
5050
typer.Option(
5151
"--pip/--no-pip",
5252
help="Pip install the template(s) as editable dependencies",
@@ -127,24 +127,24 @@ def new(
127127
@app_cli.command()
128128
def add(
129129
dependencies: Annotated[
130-
Optional[list[str]],
130+
list[str] | None,
131131
typer.Argument(help="The dependency to add"),
132132
] = None,
133133
*,
134134
api_path: Annotated[
135-
Optional[list[str]],
135+
list[str] | None,
136136
typer.Option(help="API paths to add"),
137137
] = None,
138138
project_dir: Annotated[
139-
Optional[Path],
139+
Path | None,
140140
typer.Option(help="The project directory"),
141141
] = None,
142142
repo: Annotated[
143-
Optional[list[str]],
143+
list[str] | None,
144144
typer.Option(help="Install templates from a specific github repo instead"),
145145
] = None,
146146
branch: Annotated[
147-
Optional[list[str]],
147+
list[str] | None,
148148
typer.Option(help="Install templates from a specific branch"),
149149
] = None,
150150
pip: Annotated[
@@ -186,7 +186,7 @@ def add(
186186
)
187187

188188
# group by repo/ref
189-
grouped: dict[tuple[str, Optional[str]], list[DependencySource]] = {}
189+
grouped: dict[tuple[str, str | None], list[DependencySource]] = {}
190190
for dep in parsed_deps:
191191
key_tup = (dep["git"], dep["ref"])
192192
lst = grouped.get(key_tup, [])
@@ -305,7 +305,7 @@ def remove(
305305
api_paths: Annotated[list[str], typer.Argument(help="The API paths to remove")],
306306
*,
307307
project_dir: Annotated[
308-
Optional[Path],
308+
Path | None,
309309
typer.Option(help="The project directory"),
310310
] = None,
311311
) -> None:
@@ -344,15 +344,15 @@ def remove(
344344
def serve(
345345
*,
346346
port: Annotated[
347-
Optional[int],
347+
int | None,
348348
typer.Option(help="The port to run the server on"),
349349
] = None,
350350
host: Annotated[
351-
Optional[str],
351+
str | None,
352352
typer.Option(help="The host to run the server on"),
353353
] = None,
354354
app: Annotated[
355-
Optional[str],
355+
str | None,
356356
typer.Option(help="The app to run, e.g. `app.server:app`"),
357357
] = None,
358358
) -> None:

libs/cli/langchain_cli/namespaces/integration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import shutil
66
import subprocess
77
from pathlib import Path
8-
from typing import Annotated, Optional, cast
8+
from typing import Annotated, cast
99

1010
import typer
1111
from typing_extensions import TypedDict
@@ -66,22 +66,22 @@ def new(
6666
),
6767
],
6868
name_class: Annotated[
69-
Optional[str],
69+
str | None,
7070
typer.Option(
7171
help="The name of the integration in PascalCase. e.g. `MyIntegration`."
7272
" This is used to name classes like `MyIntegrationVectorStore`",
7373
),
7474
] = None,
7575
src: Annotated[
76-
Optional[list[str]],
76+
list[str] | None,
7777
typer.Option(
7878
help="The name of the single template file to copy."
7979
" e.g. `--src integration_template/chat_models.py "
8080
"--dst my_integration/chat_models.py`. Can be used multiple times.",
8181
),
8282
] = None,
8383
dst: Annotated[
84-
Optional[list[str]],
84+
list[str] | None,
8585
typer.Option(
8686
help="The relative path to the integration package to place the new file in"
8787
". e.g. `my-integration/my_integration.py`",
@@ -220,7 +220,7 @@ def create_doc(
220220
),
221221
],
222222
name_class: Annotated[
223-
Optional[str],
223+
str | None,
224224
typer.Option(
225225
help=(
226226
"The PascalCase name of the integration (e.g. `OpenAI`, "

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pathlib
77
from pathlib import Path
88
from types import ModuleType
9-
from typing import Optional
109

1110
from typing_extensions import override
1211

@@ -22,7 +21,7 @@
2221
class ImportExtractor(ast.NodeVisitor):
2322
"""Import extractor."""
2423

25-
def __init__(self, *, from_package: Optional[str] = None) -> None:
24+
def __init__(self, *, from_package: str | None = None) -> None:
2625
"""Extract all imports from the given code, optionally filtering by package."""
2726
self.imports: list[tuple[str, str]] = []
2827
self.package = from_package
@@ -105,7 +104,7 @@ def _get_all_classnames_from_file(file: Path, pkg: str) -> list[tuple[str, str]]
105104
def identify_all_imports_in_file(
106105
file: str,
107106
*,
108-
from_package: Optional[str] = None,
107+
from_package: str | None = None,
109108
) -> list[tuple[str, str]]:
110109
"""Identify all the imports in the given file.
111110
@@ -191,7 +190,7 @@ def list_init_imports_by_package(pkg_root: str) -> list[tuple[str, str]]:
191190
def find_imports_from_package(
192191
code: str,
193192
*,
194-
from_package: Optional[str] = None,
193+
from_package: str | None = None,
195194
) -> list[tuple[str, str]]:
196195
"""Find imports in code.
197196

libs/cli/langchain_cli/namespaces/template.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
import subprocess
66
from pathlib import Path
7-
from typing import Annotated, Optional
7+
from typing import Annotated
88

99
import typer
1010
import uvicorn
@@ -88,15 +88,15 @@ def new(
8888
def serve(
8989
*,
9090
port: Annotated[
91-
Optional[int],
91+
int | None,
9292
typer.Option(help="The port to run the server on"),
9393
] = None,
9494
host: Annotated[
95-
Optional[str],
95+
str | None,
9696
typer.Option(help="The host to run the server on"),
9797
] = None,
9898
configurable: Annotated[
99-
Optional[bool],
99+
bool | None,
100100
typer.Option(
101101
"--configurable/--no-configurable",
102102
help="Whether to include a configurable route",
@@ -140,7 +140,7 @@ def serve(
140140

141141

142142
@package_cli.command()
143-
def list(contains: Annotated[Optional[str], typer.Argument()] = None) -> None: # noqa: A001
143+
def list(contains: Annotated[str | None, typer.Argument()] = None) -> None: # noqa: A001
144144
"""List all or search for available templates."""
145145
packages = list_packages(contains=contains)
146146
for package in packages:

libs/cli/pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ test_integration = []
3939
langchain-core = { path = "../core", editable = true }
4040
langchain = { path = "../langchain", editable = true }
4141

42-
[tool.ruff]
43-
target-version = "py39"
44-
4542
[tool.ruff.format]
4643
docstring-code-format = true
4744

0 commit comments

Comments
 (0)