Home / Function/ __init_subclass__() — flask Function Reference

__init_subclass__() — flask Function Reference

Architecture documentation for the __init_subclass__() function in app.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  9ecf8228_23ee_83b6_506a_89e897629e5e["__init_subclass__()"]
  9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5["Flask"]
  9ecf8228_23ee_83b6_506a_89e897629e5e -->|defined in| 9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5
  d6edd7d2_c2e0_7fc6_16f0_be9c77b66b0c["remove_ctx()"]
  9ecf8228_23ee_83b6_506a_89e897629e5e -->|calls| d6edd7d2_c2e0_7fc6_16f0_be9c77b66b0c
  00f5af9e_64cc_3191_e993_7b037a67bd5b["add_ctx()"]
  9ecf8228_23ee_83b6_506a_89e897629e5e -->|calls| 00f5af9e_64cc_3191_e993_7b037a67bd5b
  style 9ecf8228_23ee_83b6_506a_89e897629e5e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/app.py lines 253–307

    def __init_subclass__(cls, **kwargs: t.Any) -> None:
        import warnings

        # These method signatures were updated to take a ctx param. Detect
        # overridden methods in subclasses that still have the old signature.
        # Show a deprecation warning and wrap to call with correct args.
        for method in (
            cls.handle_http_exception,
            cls.handle_user_exception,
            cls.handle_exception,
            cls.log_exception,
            cls.dispatch_request,
            cls.full_dispatch_request,
            cls.finalize_request,
            cls.make_default_options_response,
            cls.preprocess_request,
            cls.process_response,
            cls.do_teardown_request,
            cls.do_teardown_appcontext,
        ):
            base_method = getattr(Flask, method.__name__)

            if method is base_method:
                # not overridden
                continue

            # get the second parameter (first is self)
            iter_params = iter(inspect.signature(method).parameters.values())
            next(iter_params)
            param = next(iter_params, None)

            # must have second parameter named ctx or annotated AppContext
            if param is None or not (
                # no annotation, match name
                (param.annotation is inspect.Parameter.empty and param.name == "ctx")
                or (
                    # string annotation, access path ends with AppContext
                    isinstance(param.annotation, str)
                    and param.annotation.rpartition(".")[2] == "AppContext"
                )
                or (
                    # class annotation
                    inspect.isclass(param.annotation)
                    and issubclass(param.annotation, AppContext)
                )
            ):
                warnings.warn(
                    f"The '{method.__name__}' method now takes 'ctx: AppContext'"
                    " as the first parameter. The old signature is deprecated"
                    " and will not be supported in Flask 4.0.",
                    DeprecationWarning,
                    stacklevel=2,
                )
                setattr(cls, method.__name__, remove_ctx(method))
                setattr(Flask, method.__name__, add_ctx(base_method))

Subdomains

Defined In

Frequently Asked Questions

What does __init_subclass__() do?
__init_subclass__() is a function in the flask codebase, defined in src/flask/app.py.
Where is __init_subclass__() defined?
__init_subclass__() is defined in src/flask/app.py at line 253.
What does __init_subclass__() call?
__init_subclass__() calls 2 function(s): add_ctx, remove_ctx.

Analyze Your Own Codebase

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

Try Supermodel Free