Skip to content

Commit

Permalink
Don't import concrete security factories in init (#1377)
Browse files Browse the repository at this point in the history
* Don't import concrete security factories in init

* Catch importerror in security init

* Move not_installed_error into utils
  • Loading branch information
Ruwann committed Jul 7, 2021
1 parent 1012721 commit c014299
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 1 addition & 9 deletions connexion/__init__.py
Expand Up @@ -10,20 +10,12 @@
from .operations import compat
from .problem import problem # NOQA
from .resolver import Resolution, Resolver, RestyResolver # NOQA
from .utils import not_installed_error # NOQA

full_name = f'{__package__}.operation'
sys.modules[full_name] = sys.modules[compat.__name__]


def not_installed_error(exc): # pragma: no cover
import functools

def _required_lib(exc, *args, **kwargs):
raise exc

return functools.partial(_required_lib, exc)


try:
from flask import request # NOQA

Expand Down
13 changes: 11 additions & 2 deletions connexion/security/__init__.py
Expand Up @@ -4,6 +4,15 @@
from .async_security_handler_factory import AbstractAsyncSecurityHandlerFactory # NOQA
from .security_handler_factory import AbstractSecurityHandlerFactory # NOQA

from ..utils import not_installed_error

# concrete
from .aiohttp_security_handler_factory import AioHttpSecurityHandlerFactory # NOQA
from .flask_security_handler_factory import FlaskSecurityHandlerFactory # NOQA
try:
from .flask_security_handler_factory import FlaskSecurityHandlerFactory
except ImportError as err: # pragma: no cover
FlaskSecurityHandlerFactory = not_installed_error(err)

try:
from .aiohttp_security_handler_factory import AioHttpSecurityHandlerFactory
except ImportError as err: # pragma: no cover
AioHttpSecurityHandlerFactory = not_installed_error(err)
9 changes: 9 additions & 0 deletions connexion/utils.py
Expand Up @@ -247,3 +247,12 @@ def ignore_aliases(self, *args):
yaml.representer.SafeRepresenter.represent_scalar = my_represent_scalar

return yaml.dump(openapi, allow_unicode=True, Dumper=NoAnchorDumper)


def not_installed_error(exc): # pragma: no cover
"""Raises the ImportError when the module/object is actually called."""

def _required_lib(exc, *args, **kwargs):
raise exc

return functools.partial(_required_lib, exc)

0 comments on commit c014299

Please sign in to comment.