Skip to content

Commit 04e357e

Browse files
authored
🎨 Update format and types for Python 3.10 (#310)
1 parent 8b239d0 commit 04e357e

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

src/fastapi_cli/cli.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from pathlib import Path
3-
from typing import Annotated, Any, Union
3+
from typing import Annotated, Any
44

55
import typer
66
from pydantic import ValidationError
@@ -57,7 +57,7 @@ def version_callback(value: bool) -> None:
5757
@app.callback()
5858
def callback(
5959
version: Annotated[
60-
Union[bool, None],
60+
bool | None,
6161
typer.Option(
6262
"--version", help="Show the version and exit.", callback=version_callback
6363
),
@@ -99,19 +99,19 @@ def _get_module_tree(module_paths: list[Path]) -> Tree:
9999

100100

101101
def _run(
102-
path: Union[Path, None] = None,
102+
path: Path | None = None,
103103
*,
104104
host: str = "127.0.0.1",
105105
port: int = 8000,
106106
reload: bool = True,
107-
reload_dirs: Union[list[Path], None] = None,
108-
workers: Union[int, None] = None,
107+
reload_dirs: list[Path] | None = None,
108+
workers: int | None = None,
109109
root_path: str = "",
110110
command: str,
111-
app: Union[str, None] = None,
112-
entrypoint: Union[str, None] = None,
111+
app: str | None = None,
112+
entrypoint: str | None = None,
113113
proxy_headers: bool = False,
114-
forwarded_allow_ips: Union[str, None] = None,
114+
forwarded_allow_ips: str | None = None,
115115
) -> None:
116116
with get_rich_toolkit() as toolkit:
117117
server_type = "development" if command == "dev" else "production"
@@ -236,7 +236,7 @@ def _run(
236236
@app.command()
237237
def dev(
238238
path: Annotated[
239-
Union[Path, None],
239+
Path | None,
240240
typer.Argument(
241241
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
242242
),
@@ -262,7 +262,7 @@ def dev(
262262
),
263263
] = True,
264264
reload_dir: Annotated[
265-
Union[list[Path], None],
265+
list[Path] | None,
266266
typer.Option(
267267
help="Set reload directories explicitly, instead of using the current working directory."
268268
),
@@ -274,13 +274,13 @@ def dev(
274274
),
275275
] = "",
276276
app: Annotated[
277-
Union[str, None],
277+
str | None,
278278
typer.Option(
279279
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
280280
),
281281
] = None,
282282
entrypoint: Annotated[
283-
Union[str, None],
283+
str | None,
284284
typer.Option(
285285
"--entrypoint",
286286
"-e",
@@ -294,7 +294,7 @@ def dev(
294294
),
295295
] = True,
296296
forwarded_allow_ips: Annotated[
297-
Union[str, None],
297+
str | None,
298298
typer.Option(
299299
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
300300
),
@@ -343,7 +343,7 @@ def dev(
343343
@app.command()
344344
def run(
345345
path: Annotated[
346-
Union[Path, None],
346+
Path | None,
347347
typer.Argument(
348348
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
349349
),
@@ -369,7 +369,7 @@ def run(
369369
),
370370
] = False,
371371
workers: Annotated[
372-
Union[int, None],
372+
int | None,
373373
typer.Option(
374374
help="Use multiple worker processes. Mutually exclusive with the --reload flag."
375375
),
@@ -381,13 +381,13 @@ def run(
381381
),
382382
] = "",
383383
app: Annotated[
384-
Union[str, None],
384+
str | None,
385385
typer.Option(
386386
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
387387
),
388388
] = None,
389389
entrypoint: Annotated[
390-
Union[str, None],
390+
str | None,
391391
typer.Option(
392392
"--entrypoint",
393393
"-e",
@@ -401,7 +401,7 @@ def run(
401401
),
402402
] = True,
403403
forwarded_allow_ips: Annotated[
404-
Union[str, None],
404+
str | None,
405405
typer.Option(
406406
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
407407
),

src/fastapi_cli/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import logging
22
from pathlib import Path
3-
from typing import Any, Optional
3+
from typing import Any
44

55
from pydantic import BaseModel, StrictStr
66

77
logger = logging.getLogger(__name__)
88

99

1010
class FastAPIConfig(BaseModel):
11-
entrypoint: Optional[StrictStr] = None
11+
entrypoint: StrictStr | None = None
1212

1313
@classmethod
1414
def _read_pyproject_toml(cls) -> dict[str, Any]:
@@ -33,7 +33,7 @@ def _read_pyproject_toml(cls) -> dict[str, Any]:
3333
return data.get("tool", {}).get("fastapi", {}) # type: ignore
3434

3535
@classmethod
36-
def resolve(cls, entrypoint: Optional[str] = None) -> "FastAPIConfig":
36+
def resolve(cls, entrypoint: str | None = None) -> "FastAPIConfig":
3737
config = cls._read_pyproject_toml()
3838

3939
if entrypoint is not None:

src/fastapi_cli/discover.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from dataclasses import dataclass
44
from logging import getLogger
55
from pathlib import Path
6-
from typing import Union
76

87
from fastapi_cli.exceptions import FastAPICLIException
98

@@ -65,7 +64,7 @@ def get_module_data_from_path(path: Path) -> ModuleData:
6564
)
6665

6766

68-
def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) -> str:
67+
def get_app_name(*, mod_data: ModuleData, app_name: str | None = None) -> str:
6968
try:
7069
mod = importlib.import_module(mod_data.module_import_str)
7170
except (ImportError, ValueError) as e:
@@ -111,7 +110,7 @@ class ImportData:
111110

112111

113112
def get_import_data(
114-
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
113+
*, path: Path | None = None, app_name: str | None = None
115114
) -> ImportData:
116115
if not path:
117116
path = get_default_path()

src/fastapi_cli/logging.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import logging
2-
from typing import Union
32

43
from rich.console import Console
54
from rich.logging import RichHandler
65

76

8-
def setup_logging(
9-
terminal_width: Union[int, None] = None, level: int = logging.INFO
10-
) -> None:
7+
def setup_logging(terminal_width: int | None = None, level: int = logging.INFO) -> None:
118
logger = logging.getLogger("fastapi_cli")
129
console = Console(width=terminal_width) if terminal_width else None
1310
rich_handler = RichHandler(

0 commit comments

Comments
 (0)