|
9 | 9 | from operator import attrgetter |
10 | 10 | from threading import Lock |
11 | 11 | from threading import Thread |
12 | | -from typing import Any |
13 | | -from typing import TYPE_CHECKING |
14 | 12 |
|
15 | 13 | import click |
16 | 14 | from werkzeug.utils import import_string |
|
20 | 18 | from .helpers import get_env |
21 | 19 | from .helpers import get_load_dotenv |
22 | 20 |
|
23 | | -try: |
24 | | - import dotenv |
25 | | -except ImportError: |
26 | | - dotenv = None |
27 | | - |
28 | | -try: |
29 | | - import ssl |
30 | | -except ImportError: |
31 | | - ssl = None # type: ignore |
32 | | - |
33 | | -if sys.version_info >= (3, 10): |
34 | | - from importlib import metadata |
35 | | -else: |
36 | | - # Use a backport on Python < 3.10. |
37 | | - # |
38 | | - # We technically have importlib.metadata on 3.8+, |
39 | | - # but the API changed in 3.10, so use the backport |
40 | | - # for consistency. |
41 | | - if TYPE_CHECKING: |
42 | | - metadata: Any |
43 | | - else: |
44 | | - # we do this to avoid a version dependent mypy error |
45 | | - # because importlib_metadata is not installed in python3.10+ |
46 | | - import importlib_metadata as metadata |
47 | | - |
48 | 21 |
|
49 | 22 | class NoAppException(click.UsageError): |
50 | 23 | """Raised if an application cannot be found or loaded.""" |
@@ -520,6 +493,14 @@ def _load_plugin_commands(self): |
520 | 493 | if self._loaded_plugin_commands: |
521 | 494 | return |
522 | 495 |
|
| 496 | + if sys.version_info >= (3, 10): |
| 497 | + from importlib import metadata |
| 498 | + else: |
| 499 | + # Use a backport on Python < 3.10. We technically have |
| 500 | + # importlib.metadata on 3.8+, but the API changed in 3.10, |
| 501 | + # so use the backport for consistency. |
| 502 | + import importlib_metadata as metadata |
| 503 | + |
523 | 504 | for ep in metadata.entry_points(group="flask.commands"): |
524 | 505 | self.add_command(ep.load(), ep.name) |
525 | 506 |
|
@@ -615,7 +596,9 @@ def load_dotenv(path=None): |
615 | 596 |
|
616 | 597 | .. versionadded:: 1.0 |
617 | 598 | """ |
618 | | - if dotenv is None: |
| 599 | + try: |
| 600 | + import dotenv |
| 601 | + except ImportError: |
619 | 602 | if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"): |
620 | 603 | click.secho( |
621 | 604 | " * Tip: There are .env or .flaskenv files present." |
@@ -691,12 +674,14 @@ def __init__(self): |
691 | 674 | self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True) |
692 | 675 |
|
693 | 676 | def convert(self, value, param, ctx): |
694 | | - if ssl is None: |
| 677 | + try: |
| 678 | + import ssl |
| 679 | + except ImportError: |
695 | 680 | raise click.BadParameter( |
696 | 681 | 'Using "--cert" requires Python to be compiled with SSL support.', |
697 | 682 | ctx, |
698 | 683 | param, |
699 | | - ) |
| 684 | + ) from None |
700 | 685 |
|
701 | 686 | try: |
702 | 687 | return self.path_type(value, param, ctx) |
@@ -729,7 +714,13 @@ def _validate_key(ctx, param, value): |
729 | 714 | """ |
730 | 715 | cert = ctx.params.get("cert") |
731 | 716 | is_adhoc = cert == "adhoc" |
732 | | - is_context = ssl and isinstance(cert, ssl.SSLContext) |
| 717 | + |
| 718 | + try: |
| 719 | + import ssl |
| 720 | + except ImportError: |
| 721 | + is_context = False |
| 722 | + else: |
| 723 | + is_context = isinstance(cert, ssl.SSLContext) |
733 | 724 |
|
734 | 725 | if value is not None: |
735 | 726 | if is_adhoc: |
|
0 commit comments