Skip to content

Commit

Permalink
Don't set the cli attribute in the sansio scaffold
Browse files Browse the repository at this point in the history
It is (currently) Flask specific and hence cannot be shared in the
sansio shared code.
  • Loading branch information
pgjones committed Dec 14, 2023
1 parent c275573 commit cfdb245
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Unreleased

- Correct type for ``path`` argument to ``send_file``. :issue:`5230`
- Fix a typo in an error message for the ``flask run --key`` option. :pr:`5344`
- Don't set the cli attribute in the sansio scaffold, but rather in
the Flask concrete classes. :pr:`5270`


Version 3.0.0
Expand Down
10 changes: 10 additions & 0 deletions src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ def __init__(
root_path=root_path,
)

#: The Click command group for registering CLI commands for this
#: object. The commands are available from the ``flask`` command
#: once the application has been discovered and blueprints have
#: been registered.
self.cli = cli.AppGroup()

# Set the name of the Click group in case someone wants to add
# the app's commands to another CLI tool.
self.cli.name = self.name

# Add a static route using the provided static_url_path, static_host,
# and static_folder if there is a configured static_folder.
# Note we do this without checking if static_folder exists.
Expand Down
38 changes: 38 additions & 0 deletions src/flask/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,54 @@
import typing as t
from datetime import timedelta

from .cli import AppGroup
from .globals import current_app
from .helpers import send_from_directory
from .sansio.blueprints import Blueprint as SansioBlueprint
from .sansio.blueprints import BlueprintSetupState as BlueprintSetupState # noqa
from .sansio.scaffold import _sentinel

if t.TYPE_CHECKING: # pragma: no cover
from .wrappers import Response


class Blueprint(SansioBlueprint):
def __init__(
self,
name: str,
import_name: str,
static_folder: str | os.PathLike | None = None,
static_url_path: str | None = None,
template_folder: str | os.PathLike | None = None,
url_prefix: str | None = None,
subdomain: str | None = None,
url_defaults: dict | None = None,
root_path: str | None = None,
cli_group: str | None = _sentinel, # type: ignore
) -> None:
super().__init__(
name,
import_name,
static_folder,
static_url_path,
template_folder,
url_prefix,
subdomain,
url_defaults,
root_path,
cli_group,
)

#: The Click command group for registering CLI commands for this
#: object. The commands are available from the ``flask`` command
#: once the application has been discovered and blueprints have
#: been registered.
self.cli = AppGroup()

# Set the name of the Click group in case someone wants to add
# the app's commands to another CLI tool.
self.cli.name = self.name

def get_send_file_max_age(self, filename: str | None) -> int | None:
"""Used by :func:`send_file` to determine the ``max_age`` cache
value for a given file path if it wasn't passed.
Expand Down
4 changes: 0 additions & 4 deletions src/flask/sansio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,6 @@ def __init__(
# request.
self._got_first_request = False

# Set the name of the Click group in case someone wants to add
# the app's commands to another CLI tool.
self.cli.name = self.name

def _check_setup_finished(self, f_name: str) -> None:
if self._got_first_request:
raise AssertionError(
Expand Down
11 changes: 4 additions & 7 deletions src/flask/sansio/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
from werkzeug.utils import cached_property

from .. import typing as ft
from ..cli import AppGroup
from ..helpers import get_root_path
from ..templating import _default_template_ctx_processor

if t.TYPE_CHECKING: # pragma: no cover
from click import Group

# a singleton sentinel value for parameter defaults
_sentinel = object()

Expand Down Expand Up @@ -64,6 +66,7 @@ class Scaffold:
.. versionadded:: 2.0
"""

cli: Group
name: str
_static_folder: str | None = None
_static_url_path: str | None = None
Expand Down Expand Up @@ -95,12 +98,6 @@ def __init__(
#: up resources contained in the package.
self.root_path = root_path

#: The Click command group for registering CLI commands for this
#: object. The commands are available from the ``flask`` command
#: once the application has been discovered and blueprints have
#: been registered.
self.cli = AppGroup()

#: A dictionary mapping endpoint names to view functions.
#:
#: To register a view function, use the :meth:`route` decorator.
Expand Down
2 changes: 1 addition & 1 deletion src/flask/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def invoke( # type: ignore
:return: a :class:`~click.testing.Result` object.
"""
if cli is None:
cli = self.app.cli # type: ignore
cli = self.app.cli

if "obj" not in kwargs:
kwargs["obj"] = ScriptInfo(create_app=lambda: self.app)
Expand Down

0 comments on commit cfdb245

Please sign in to comment.