fix error in import_string
This commit is contained in:
		| @@ -1,5 +1,7 @@ | |||||||
| """Defines basics of HTTP standard.""" | """Defines basics of HTTP standard.""" | ||||||
|  |  | ||||||
| from importlib import import_module | from importlib import import_module | ||||||
|  | from inspect import ismodule | ||||||
|  |  | ||||||
|  |  | ||||||
| STATUS_CODES = { | STATUS_CODES = { | ||||||
| @@ -147,6 +149,7 @@ def import_string(module_name): | |||||||
|     """ |     """ | ||||||
|     module, klass = module_name.rsplit(".", 1) |     module, klass = module_name.rsplit(".", 1) | ||||||
|     module = import_module(module) |     module = import_module(module) | ||||||
|     if hasattr(module, klass): |     obj = getattr(module, klass) | ||||||
|         return getattr(module, klass)() |     if ismodule(obj): | ||||||
|     return module |         return obj | ||||||
|  |     return obj() | ||||||
|   | |||||||
| @@ -1,3 +1,5 @@ | |||||||
|  | import inspect | ||||||
|  |  | ||||||
| from sanic import helpers | from sanic import helpers | ||||||
| from sanic.config import Config | from sanic.config import Config | ||||||
| import pytest | import pytest | ||||||
| @@ -76,11 +78,16 @@ def test_remove_entity_headers(): | |||||||
|         assert helpers.remove_entity_headers(header) == expected |         assert helpers.remove_entity_headers(header) == expected | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_import_string(): | def test_import_string_class(): | ||||||
|     obj = helpers.import_string('sanic.config.Config') |     obj = helpers.import_string('sanic.config.Config') | ||||||
|     assert isinstance(obj, Config) |     assert isinstance(obj, Config) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def test_import_string_module(): | ||||||
|  |     module = helpers.import_string('sanic.config') | ||||||
|  |     assert inspect.ismodule(module) | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_import_string_exception(): | def test_import_string_exception(): | ||||||
|     with pytest.raises(ImportError): |     with pytest.raises(ImportError): | ||||||
|         helpers.import_string('test.test.test') |         helpers.import_string('test.test.test') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jotagê Sales
					Jotagê Sales