Skip to content

Commit 5e22cc9

Browse files
authored
Don't set the cli attribute in the sansio scaffold (#5270)
2 parents adb7dd9 + 5fdce4c commit 5e22cc9

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Unreleased
66
- The default ``hashlib.sha1`` may not be available in FIPS builds. Don't
77
access it at import time so the developer has time to change the default.
88
:issue:`5448`
9+
- Don't initialize the ``cli`` attribute in the sansio scaffold, but rather in
10+
the ``Flask`` concrete class. :pr:`5270`
911

1012

1113
Version 3.0.2

src/flask/app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def __init__(
241241
root_path=root_path,
242242
)
243243

244+
#: The Click command group for registering CLI commands for this
245+
#: object. The commands are available from the ``flask`` command
246+
#: once the application has been discovered and blueprints have
247+
#: been registered.
248+
self.cli = cli.AppGroup()
249+
250+
# Set the name of the Click group in case someone wants to add
251+
# the app's commands to another CLI tool.
252+
self.cli.name = self.name
253+
244254
# Add a static route using the provided static_url_path, static_host,
245255
# and static_folder if there is a configured static_folder.
246256
# Note we do this without checking if static_folder exists.

src/flask/blueprints.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,54 @@
44
import typing as t
55
from datetime import timedelta
66

7+
from .cli import AppGroup
78
from .globals import current_app
89
from .helpers import send_from_directory
910
from .sansio.blueprints import Blueprint as SansioBlueprint
1011
from .sansio.blueprints import BlueprintSetupState as BlueprintSetupState # noqa
12+
from .sansio.scaffold import _sentinel
1113

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

1517

1618
class Blueprint(SansioBlueprint):
19+
def __init__(
20+
self,
21+
name: str,
22+
import_name: str,
23+
static_folder: str | os.PathLike[str] | None = None,
24+
static_url_path: str | None = None,
25+
template_folder: str | os.PathLike[str] | None = None,
26+
url_prefix: str | None = None,
27+
subdomain: str | None = None,
28+
url_defaults: dict[str, t.Any] | None = None,
29+
root_path: str | None = None,
30+
cli_group: str | None = _sentinel, # type: ignore
31+
) -> None:
32+
super().__init__(
33+
name,
34+
import_name,
35+
static_folder,
36+
static_url_path,
37+
template_folder,
38+
url_prefix,
39+
subdomain,
40+
url_defaults,
41+
root_path,
42+
cli_group,
43+
)
44+
45+
#: The Click command group for registering CLI commands for this
46+
#: object. The commands are available from the ``flask`` command
47+
#: once the application has been discovered and blueprints have
48+
#: been registered.
49+
self.cli = AppGroup()
50+
51+
# Set the name of the Click group in case someone wants to add
52+
# the app's commands to another CLI tool.
53+
self.cli.name = self.name
54+
1755
def get_send_file_max_age(self, filename: str | None) -> int | None:
1856
"""Used by :func:`send_file` to determine the ``max_age`` cache
1957
value for a given file path if it wasn't passed.

src/flask/sansio/app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,6 @@ def __init__(
410410
# request.
411411
self._got_first_request = False
412412

413-
# Set the name of the Click group in case someone wants to add
414-
# the app's commands to another CLI tool.
415-
self.cli.name = self.name
416-
417413
def _check_setup_finished(self, f_name: str) -> None:
418414
if self._got_first_request:
419415
raise AssertionError(

src/flask/sansio/scaffold.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
from collections import defaultdict
99
from functools import update_wrapper
1010

11-
import click
1211
from jinja2 import BaseLoader
1312
from jinja2 import FileSystemLoader
1413
from werkzeug.exceptions import default_exceptions
1514
from werkzeug.exceptions import HTTPException
1615
from werkzeug.utils import cached_property
1716

1817
from .. import typing as ft
19-
from ..cli import AppGroup
2018
from ..helpers import get_root_path
2119
from ..templating import _default_template_ctx_processor
2220

21+
if t.TYPE_CHECKING: # pragma: no cover
22+
from click import Group
23+
2324
# a singleton sentinel value for parameter defaults
2425
_sentinel = object()
2526

@@ -66,6 +67,7 @@ class Scaffold:
6667
.. versionadded:: 2.0
6768
"""
6869

70+
cli: Group
6971
name: str
7072
_static_folder: str | None = None
7173
_static_url_path: str | None = None
@@ -97,12 +99,6 @@ def __init__(
9799
#: up resources contained in the package.
98100
self.root_path = root_path
99101

100-
#: The Click command group for registering CLI commands for this
101-
#: object. The commands are available from the ``flask`` command
102-
#: once the application has been discovered and blueprints have
103-
#: been registered.
104-
self.cli: click.Group = AppGroup()
105-
106102
#: A dictionary mapping endpoint names to view functions.
107103
#:
108104
#: To register a view function, use the :meth:`route` decorator.

0 commit comments

Comments
 (0)