mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
Added lazy_import to graphene. Fixed #316
This commit is contained in:
parent
0e2d9be6a8
commit
74400642ed
|
@ -43,6 +43,7 @@ if not __SETUP__:
|
|||
PageInfo
|
||||
)
|
||||
from .utils.resolve_only_args import resolve_only_args
|
||||
from .utils.module_loading import lazy_import
|
||||
|
||||
__all__ = [
|
||||
'AbstractType',
|
||||
|
@ -72,4 +73,6 @@ if not __SETUP__:
|
|||
'ClientIDMutation',
|
||||
'Connection',
|
||||
'ConnectionField',
|
||||
'PageInfo']
|
||||
'PageInfo',
|
||||
'lazy_import',
|
||||
]
|
||||
|
|
26
graphene/utils/module_loading.py
Normal file
26
graphene/utils/module_loading.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from functools import partial
|
||||
from importlib import import_module
|
||||
|
||||
|
||||
def import_string(dotted_path):
|
||||
"""
|
||||
Import a dotted module path and return the attribute/class designated by the
|
||||
last name in the path. Raise ImportError if the import failed.
|
||||
"""
|
||||
try:
|
||||
module_path, class_name = dotted_path.rsplit('.', 1)
|
||||
except ValueError as err:
|
||||
raise ImportError("%s doesn't look like a module path" % dotted_path)
|
||||
|
||||
module = import_module(module_path)
|
||||
|
||||
try:
|
||||
return getattr(module, class_name)
|
||||
except AttributeError as err:
|
||||
raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
|
||||
module_path, class_name)
|
||||
)
|
||||
|
||||
|
||||
def lazy_import(dotted_path):
|
||||
return partial(import_string, dotted_path)
|
29
graphene/utils/tests/test_module_loading.py
Normal file
29
graphene/utils/tests/test_module_loading.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from pytest import raises
|
||||
|
||||
from graphene import String
|
||||
from ..module_loading import lazy_import, import_string
|
||||
|
||||
|
||||
def test_import_string():
|
||||
MyString = import_string('graphene.String')
|
||||
assert MyString == String
|
||||
|
||||
|
||||
def test_import_string_module():
|
||||
with raises(Exception) as exc_info:
|
||||
import_string('graphenea')
|
||||
|
||||
assert str(exc_info.value) == 'graphenea doesn\'t look like a module path'
|
||||
|
||||
|
||||
def test_import_string_class():
|
||||
with raises(Exception) as exc_info:
|
||||
import_string('graphene.Stringa')
|
||||
|
||||
assert str(exc_info.value) == 'Module "graphene" does not define a "Stringa" attribute/class'
|
||||
|
||||
|
||||
def test_lazy_import():
|
||||
f = lazy_import('graphene.String')
|
||||
MyString = f()
|
||||
assert MyString == String
|
Loading…
Reference in New Issue
Block a user