Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed May 23, 2022
2 parents a52a7db + d506af1 commit 1888df3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ci:
autoupdate_schedule: monthly
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.0
rev: v2.32.1
hooks:
- id: pyupgrade
args: ["--py36-plus"]
Expand Down
10 changes: 5 additions & 5 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
-r typing.txt
cfgv==3.3.1
# via pre-commit
click==8.1.2
click==8.1.3
# via
# pip-compile-multi
# pip-tools
distlib==0.3.4
# via virtualenv
filelock==3.6.0
filelock==3.7.0
# via
# tox
# virtualenv
greenlet==1.1.2 ; python_version < "3.11"
# via -r requirements/tests.in
identify==2.5.0
identify==2.5.1
# via pre-commit
nodeenv==1.6.0
# via pre-commit
pep517==0.12.0
# via pip-tools
pip-compile-multi==2.4.5
# via -r requirements/dev.in
pip-tools==6.6.0
pip-tools==6.6.1
# via pip-compile-multi
platformdirs==2.5.2
# via virtualenv
pre-commit==2.18.1
pre-commit==2.19.0
# via -r requirements/dev.in
pyyaml==6.0
# via pre-commit
Expand Down
6 changes: 3 additions & 3 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ alabaster==0.7.12
# via sphinx
babel==2.10.1
# via sphinx
certifi==2021.10.8
certifi==2022.5.18.1
# via requests
charset-normalizer==2.0.12
# via requests
Expand All @@ -21,7 +21,7 @@ idna==3.3
# via requests
imagesize==1.3.0
# via sphinx
jinja2==3.1.1
jinja2==3.1.2
# via sphinx
markupsafe==2.1.1
# via jinja2
Expand All @@ -35,7 +35,7 @@ pygments==2.12.0
# via
# sphinx
# sphinx-tabs
pyparsing==3.0.8
pyparsing==3.0.9
# via packaging
pytz==2022.1
# via babel
Expand Down
4 changes: 2 additions & 2 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# pip-compile-multi
#
asgiref==3.5.0
asgiref==3.5.2
# via -r requirements/tests.in
attrs==21.4.0
# via pytest
Expand All @@ -21,7 +21,7 @@ pluggy==1.0.0
# via pytest
py==1.11.0
# via pytest
pyparsing==3.0.8
pyparsing==3.0.9
# via packaging
pytest==7.1.2
# via -r requirements/tests.in
Expand Down
4 changes: 2 additions & 2 deletions requirements/typing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
cffi==1.15.0
# via cryptography
cryptography==37.0.1
cryptography==37.0.2
# via -r requirements/typing.in
mypy==0.950
# via -r requirements/typing.in
Expand All @@ -21,7 +21,7 @@ types-contextvars==2.4.5
# via -r requirements/typing.in
types-dataclasses==0.6.5
# via -r requirements/typing.in
types-setuptools==57.4.14
types-setuptools==57.4.15
# via -r requirements/typing.in
typing-extensions==4.2.0
# via mypy
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ ignore_missing_imports = True

[mypy-cryptography.*]
ignore_missing_imports = True

[mypy-importlib_metadata]
ignore_missing_imports = True
53 changes: 22 additions & 31 deletions src/flask/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from operator import attrgetter
from threading import Lock
from threading import Thread
from typing import Any
from typing import TYPE_CHECKING

import click
from werkzeug.utils import import_string
Expand All @@ -20,31 +18,6 @@
from .helpers import get_env
from .helpers import get_load_dotenv

try:
import dotenv
except ImportError:
dotenv = None

try:
import ssl
except ImportError:
ssl = None # type: ignore

if sys.version_info >= (3, 10):
from importlib import metadata
else:
# Use a backport on Python < 3.10.
#
# We technically have importlib.metadata on 3.8+,
# but the API changed in 3.10, so use the backport
# for consistency.
if TYPE_CHECKING:
metadata: Any
else:
# we do this to avoid a version dependent mypy error
# because importlib_metadata is not installed in python3.10+
import importlib_metadata as metadata


class NoAppException(click.UsageError):
"""Raised if an application cannot be found or loaded."""
Expand Down Expand Up @@ -520,6 +493,14 @@ def _load_plugin_commands(self):
if self._loaded_plugin_commands:
return

if sys.version_info >= (3, 10):
from importlib import metadata
else:
# Use a backport on Python < 3.10. We technically have
# importlib.metadata on 3.8+, but the API changed in 3.10,
# so use the backport for consistency.
import importlib_metadata as metadata

for ep in metadata.entry_points(group="flask.commands"):
self.add_command(ep.load(), ep.name)

Expand Down Expand Up @@ -615,7 +596,9 @@ def load_dotenv(path=None):
.. versionadded:: 1.0
"""
if dotenv is None:
try:
import dotenv
except ImportError:
if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"):
click.secho(
" * Tip: There are .env or .flaskenv files present."
Expand Down Expand Up @@ -691,12 +674,14 @@ def __init__(self):
self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True)

def convert(self, value, param, ctx):
if ssl is None:
try:
import ssl
except ImportError:
raise click.BadParameter(
'Using "--cert" requires Python to be compiled with SSL support.',
ctx,
param,
)
) from None

try:
return self.path_type(value, param, ctx)
Expand Down Expand Up @@ -729,7 +714,13 @@ def _validate_key(ctx, param, value):
"""
cert = ctx.params.get("cert")
is_adhoc = cert == "adhoc"
is_context = ssl and isinstance(cert, ssl.SSLContext)

try:
import ssl
except ImportError:
is_context = False
else:
is_context = isinstance(cert, ssl.SSLContext)

if value is not None:
if is_adhoc:
Expand Down
19 changes: 15 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from flask import Flask
from flask.cli import AppGroup
from flask.cli import DispatchingApp
from flask.cli import dotenv
from flask.cli import find_best_app
from flask.cli import FlaskGroup
from flask.cli import get_version
Expand Down Expand Up @@ -492,7 +491,18 @@ def test_no_routes(self, invoke_no_routes):
assert "No routes were registered." in result.output


need_dotenv = pytest.mark.skipif(dotenv is None, reason="dotenv is not installed")
def dotenv_not_available():
try:
import dotenv # noqa: F401
except ImportError:
return True

return False


need_dotenv = pytest.mark.skipif(
dotenv_not_available(), reason="dotenv is not installed"
)


@need_dotenv
Expand Down Expand Up @@ -530,7 +540,7 @@ def test_dotenv_path(monkeypatch):


def test_dotenv_optional(monkeypatch):
monkeypatch.setattr("flask.cli.dotenv", None)
monkeypatch.setitem(sys.modules, "dotenv", None)
monkeypatch.chdir(test_path)
load_dotenv()
assert "FOO" not in os.environ
Expand Down Expand Up @@ -602,7 +612,8 @@ def test_run_cert_import(monkeypatch):


def test_run_cert_no_ssl(monkeypatch):
monkeypatch.setattr("flask.cli.ssl", None)
monkeypatch.setitem(sys.modules, "ssl", None)

with pytest.raises(click.BadParameter):
run_command.make_context("run", ["--cert", "not_here"])

Expand Down

0 comments on commit 1888df3

Please sign in to comment.