Home / Function/ find_app_by_string() — flask Function Reference

find_app_by_string() — flask Function Reference

Architecture documentation for the find_app_by_string() function in cli.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  01192b9e_44fd_d2d6_4d3d_b0fadc14cc11["find_app_by_string()"]
  a96499c3_f8a9_e782_f156_1c1ee4a86c69["cli.py"]
  01192b9e_44fd_d2d6_4d3d_b0fadc14cc11 -->|defined in| a96499c3_f8a9_e782_f156_1c1ee4a86c69
  95e11cb3_caab_ad8f_3f8c_c26632862fe3["locate_app()"]
  95e11cb3_caab_ad8f_3f8c_c26632862fe3 -->|calls| 01192b9e_44fd_d2d6_4d3d_b0fadc14cc11
  c9d5aa34_1bf5_6777_6447_72b24ceb6b6b["_called_with_wrong_args()"]
  01192b9e_44fd_d2d6_4d3d_b0fadc14cc11 -->|calls| c9d5aa34_1bf5_6777_6447_72b24ceb6b6b
  style 01192b9e_44fd_d2d6_4d3d_b0fadc14cc11 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/cli.py lines 120–197

def find_app_by_string(module: ModuleType, app_name: str) -> Flask:
    """Check if the given string is a variable name or a function. Call
    a function to get the app instance, or return the variable directly.
    """
    from . import Flask

    # Parse app_name as a single expression to determine if it's a valid
    # attribute name or function call.
    try:
        expr = ast.parse(app_name.strip(), mode="eval").body
    except SyntaxError:
        raise NoAppException(
            f"Failed to parse {app_name!r} as an attribute name or function call."
        ) from None

    if isinstance(expr, ast.Name):
        name = expr.id
        args = []
        kwargs = {}
    elif isinstance(expr, ast.Call):
        # Ensure the function name is an attribute name only.
        if not isinstance(expr.func, ast.Name):
            raise NoAppException(
                f"Function reference must be a simple name: {app_name!r}."
            )

        name = expr.func.id

        # Parse the positional and keyword arguments as literals.
        try:
            args = [ast.literal_eval(arg) for arg in expr.args]
            kwargs = {
                kw.arg: ast.literal_eval(kw.value)
                for kw in expr.keywords
                if kw.arg is not None
            }
        except ValueError:
            # literal_eval gives cryptic error messages, show a generic
            # message with the full expression instead.
            raise NoAppException(
                f"Failed to parse arguments as literal values: {app_name!r}."
            ) from None
    else:
        raise NoAppException(
            f"Failed to parse {app_name!r} as an attribute name or function call."
        )

    try:
        attr = getattr(module, name)
    except AttributeError as e:
        raise NoAppException(
            f"Failed to find attribute {name!r} in {module.__name__!r}."
        ) from e

    # If the attribute is a function, call it with any args and kwargs
    # to get the real application.
    if inspect.isfunction(attr):
        try:
            app = attr(*args, **kwargs)
        except TypeError as e:
            if not _called_with_wrong_args(attr):
                raise

            raise NoAppException(
                f"The factory {app_name!r} in module"
                f" {module.__name__!r} could not be called with the"
                " specified arguments."
            ) from e
    else:
        app = attr

    if isinstance(app, Flask):
        return app

    raise NoAppException(
        "A valid Flask application was not obtained from"
        f" '{module.__name__}:{app_name}'."
    )

Subdomains

Defined In

Called By

Frequently Asked Questions

What does find_app_by_string() do?
find_app_by_string() is a function in the flask codebase, defined in src/flask/cli.py.
Where is find_app_by_string() defined?
find_app_by_string() is defined in src/flask/cli.py at line 120.
What does find_app_by_string() call?
find_app_by_string() calls 1 function(s): _called_with_wrong_args.
What calls find_app_by_string()?
find_app_by_string() is called by 1 function(s): locate_app.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free