Skip to content

try_import_app

faststream.cli.utils.imports.try_import_app #

try_import_app(module: Path, app: str) -> FastStream

Tries to import a FastStream app from a module.

PARAMETER DESCRIPTION
module

Path to the module containing the app.

TYPE: Path

app

Name of the FastStream app.

TYPE: str

RETURNS DESCRIPTION
FastStream

The imported FastStream app object.

RAISES DESCRIPTION
FileNotFoundError

If the module file is not found.

BadParameter

If the module or app name is not provided correctly.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/cli/utils/imports.py
def try_import_app(module: Path, app: str) -> FastStream:
    """Tries to import a FastStream app from a module.

    Args:
        module: Path to the module containing the app.
        app: Name of the FastStream app.

    Returns:
        The imported FastStream app object.

    Raises:
        FileNotFoundError: If the module file is not found.
        typer.BadParameter: If the module or app name is not provided correctly.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    try:
        app_object = import_object(module, app)

    except FileNotFoundError as e:
        typer.echo(e, err=True)
        raise typer.BadParameter(
            "Please, input module like [python_file:faststream_app_name] or [module:attribute]"
        ) from e

    else:
        return app_object  # type: ignore

Last update: 2023-11-13