diff --git a/sanic/config.py b/sanic/config.py index d3b03f4f..719528e4 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -1,10 +1,8 @@ import os import types -import import_string - from sanic.exceptions import PyFileError - +from sanic.helpers import import_string SANIC_PREFIX = "SANIC_" diff --git a/sanic/helpers.py b/sanic/helpers.py index 3312f0c7..d3e54529 100644 --- a/sanic/helpers.py +++ b/sanic/helpers.py @@ -1,4 +1,5 @@ """Defines basics of HTTP standard.""" +from importlib import import_module STATUS_CODES = { 100: b"Continue", @@ -131,3 +132,9 @@ def remove_entity_headers(headers, allowed=("content-location", "expires")): if not is_entity_header(header) or header.lower() in allowed } return headers + + +def import_string(module_name): + module, obj = module_name.rsplit('.', 1) + module = import_module(module) + return getattr(module, obj)() diff --git a/setup.py b/setup.py index 87de21fb..38b70da4 100644 --- a/setup.py +++ b/setup.py @@ -63,7 +63,6 @@ requirements = [ 'aiofiles>=0.3.0', 'websockets>=6.0,<7.0', 'multidict>=4.0,<5.0', - 'import-string>=0.1.0' ] if strtobool(os.environ.get("SANIC_NO_UJSON", "no")): print("Installing without uJSON") diff --git a/tests/test_config.py b/tests/test_config.py index 0f8ef746..885c745e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -35,6 +35,11 @@ def test_load_from_object_string(app): assert 'not_for_config' not in app.config +def test_load_from_object_string_exception(app): + with pytest.raises(ImportError): + app.config.from_object('test_config.Config.test') + + def test_auto_load_env(): environ["SANIC_TEST_ANSWER"] = "42" app = Sanic() diff --git a/tests/test_helpers.py b/tests/test_helpers.py index b5f6dd16..db1f9b61 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,4 +1,5 @@ from sanic import helpers +import pytest def test_has_message_body(): @@ -72,3 +73,14 @@ def test_remove_entity_headers(): for header, expected in tests: assert helpers.remove_entity_headers(header) == expected + + +def test_import_string(): + from sanic.config import Config + obj = helpers.import_string('sanic.config.Config') + assert isinstance(obj, Config) + + +def test_import_string_exception(): + with pytest.raises(ImportError): + helpers.import_string('test.test.test')