diff --git a/src/fastapi_cli/cli.py b/src/fastapi_cli/cli.py index d000bef5..ecf0f963 100644 --- a/src/fastapi_cli/cli.py +++ b/src/fastapi_cli/cli.py @@ -1,6 +1,6 @@ import logging from pathlib import Path -from typing import Annotated, Any, Union +from typing import Annotated, Any import typer from pydantic import ValidationError @@ -57,7 +57,7 @@ def version_callback(value: bool) -> None: @app.callback() def callback( version: Annotated[ - Union[bool, None], + bool | None, typer.Option( "--version", help="Show the version and exit.", callback=version_callback ), @@ -99,19 +99,19 @@ def _get_module_tree(module_paths: list[Path]) -> Tree: def _run( - path: Union[Path, None] = None, + path: Path | None = None, *, host: str = "127.0.0.1", port: int = 8000, reload: bool = True, - reload_dirs: Union[list[Path], None] = None, - workers: Union[int, None] = None, + reload_dirs: list[Path] | None = None, + workers: int | None = None, root_path: str = "", command: str, - app: Union[str, None] = None, - entrypoint: Union[str, None] = None, + app: str | None = None, + entrypoint: str | None = None, proxy_headers: bool = False, - forwarded_allow_ips: Union[str, None] = None, + forwarded_allow_ips: str | None = None, ) -> None: with get_rich_toolkit() as toolkit: server_type = "development" if command == "dev" else "production" @@ -236,7 +236,7 @@ def _run( @app.command() def dev( path: Annotated[ - Union[Path, None], + Path | None, typer.Argument( 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." ), @@ -262,7 +262,7 @@ def dev( ), ] = True, reload_dir: Annotated[ - Union[list[Path], None], + list[Path] | None, typer.Option( help="Set reload directories explicitly, instead of using the current working directory." ), @@ -274,13 +274,13 @@ def dev( ), ] = "", app: Annotated[ - Union[str, None], + str | None, typer.Option( 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." ), ] = None, entrypoint: Annotated[ - Union[str, None], + str | None, typer.Option( "--entrypoint", "-e", @@ -294,7 +294,7 @@ def dev( ), ] = True, forwarded_allow_ips: Annotated[ - Union[str, None], + str | None, typer.Option( help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything." ), @@ -343,7 +343,7 @@ def dev( @app.command() def run( path: Annotated[ - Union[Path, None], + Path | None, typer.Argument( 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." ), @@ -369,7 +369,7 @@ def run( ), ] = False, workers: Annotated[ - Union[int, None], + int | None, typer.Option( help="Use multiple worker processes. Mutually exclusive with the --reload flag." ), @@ -381,13 +381,13 @@ def run( ), ] = "", app: Annotated[ - Union[str, None], + str | None, typer.Option( 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." ), ] = None, entrypoint: Annotated[ - Union[str, None], + str | None, typer.Option( "--entrypoint", "-e", @@ -401,7 +401,7 @@ def run( ), ] = True, forwarded_allow_ips: Annotated[ - Union[str, None], + str | None, typer.Option( help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything." ), diff --git a/src/fastapi_cli/config.py b/src/fastapi_cli/config.py index f2af18aa..4d315be9 100644 --- a/src/fastapi_cli/config.py +++ b/src/fastapi_cli/config.py @@ -1,6 +1,6 @@ import logging from pathlib import Path -from typing import Any, Optional +from typing import Any from pydantic import BaseModel, StrictStr @@ -8,7 +8,7 @@ class FastAPIConfig(BaseModel): - entrypoint: Optional[StrictStr] = None + entrypoint: StrictStr | None = None @classmethod def _read_pyproject_toml(cls) -> dict[str, Any]: @@ -33,7 +33,7 @@ def _read_pyproject_toml(cls) -> dict[str, Any]: return data.get("tool", {}).get("fastapi", {}) # type: ignore @classmethod - def resolve(cls, entrypoint: Optional[str] = None) -> "FastAPIConfig": + def resolve(cls, entrypoint: str | None = None) -> "FastAPIConfig": config = cls._read_pyproject_toml() if entrypoint is not None: diff --git a/src/fastapi_cli/discover.py b/src/fastapi_cli/discover.py index 116ed860..7f5300a6 100644 --- a/src/fastapi_cli/discover.py +++ b/src/fastapi_cli/discover.py @@ -3,7 +3,6 @@ from dataclasses import dataclass from logging import getLogger from pathlib import Path -from typing import Union from fastapi_cli.exceptions import FastAPICLIException @@ -65,7 +64,7 @@ def get_module_data_from_path(path: Path) -> ModuleData: ) -def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) -> str: +def get_app_name(*, mod_data: ModuleData, app_name: str | None = None) -> str: try: mod = importlib.import_module(mod_data.module_import_str) except (ImportError, ValueError) as e: @@ -111,7 +110,7 @@ class ImportData: def get_import_data( - *, path: Union[Path, None] = None, app_name: Union[str, None] = None + *, path: Path | None = None, app_name: str | None = None ) -> ImportData: if not path: path = get_default_path() diff --git a/src/fastapi_cli/logging.py b/src/fastapi_cli/logging.py index 67f116c9..03ff41b3 100644 --- a/src/fastapi_cli/logging.py +++ b/src/fastapi_cli/logging.py @@ -1,13 +1,10 @@ import logging -from typing import Union from rich.console import Console from rich.logging import RichHandler -def setup_logging( - terminal_width: Union[int, None] = None, level: int = logging.INFO -) -> None: +def setup_logging(terminal_width: int | None = None, level: int = logging.INFO) -> None: logger = logging.getLogger("fastapi_cli") console = Console(width=terminal_width) if terminal_width else None rich_handler = RichHandler(