diff --git a/configurations/base.py b/configurations/base.py index b16903a..e14878e 100644 --- a/configurations/base.py +++ b/configurations/base.py @@ -66,7 +66,7 @@ def __new__(cls, name, bases, attrs): return super().__new__(cls, name, bases, attrs) def __repr__(self): - return "".format(self.__module__, + return "".format(self.__module__, self.__name__) @@ -115,7 +115,7 @@ def load_dotenv(cls): # now check if we can access the file since we know we really want to try: - with open(dotenv, 'r') as f: + with open(dotenv) as f: content = f.read() except OSError as e: raise ImproperlyConfigured("Couldn't read .env file " diff --git a/configurations/importer.py b/configurations/importer.py index 499eab6..a0b49d3 100644 --- a/configurations/importer.py +++ b/configurations/importer.py @@ -71,7 +71,7 @@ def __init__(self, check_options=False): self.announce() def __repr__(self): - return "".format(self.module, + return "".format(self.module, self.name) @property @@ -122,8 +122,8 @@ def stylize(text): if (self.argv[1] == 'runserver' and os.environ.get('RUN_MAIN') == 'true'): - message = ("django-configurations version {0}, using " - "configuration {1}".format(__version__ or "", + message = ("django-configurations version {}, using " + "configuration {}".format(__version__ or "", self.name)) self.logger.debug(stylize(message)) @@ -151,13 +151,13 @@ def load_module(self, fullname): sys.modules[fullname] = mod self.spec.loader.exec_module(mod) - cls_path = '{0}.{1}'.format(mod.__name__, self.name) + cls_path = f'{mod.__name__}.{self.name}' try: cls = getattr(mod, self.name) except AttributeError as err: # pragma: no cover - reraise(err, "Couldn't find configuration '{0}' " - "in module '{1}'".format(self.name, + reraise(err, "Couldn't find configuration '{}' " + "in module '{}'".format(self.name, mod.__package__)) try: cls.pre_setup() @@ -174,11 +174,11 @@ def load_module(self, fullname): continue setattr(mod, name, value) - setattr(mod, 'CONFIGURATION', '{0}.{1}'.format(fullname, + setattr(mod, 'CONFIGURATION', '{}.{}'.format(fullname, self.name)) cls.post_setup() except Exception as err: - reraise(err, "Couldn't setup configuration '{0}'".format(cls_path)) + reraise(err, f"Couldn't setup configuration '{cls_path}'") return mod diff --git a/configurations/utils.py b/configurations/utils.py index 1f618ee..aabd009 100644 --- a/configurations/utils.py +++ b/configurations/utils.py @@ -29,21 +29,21 @@ def import_by_path(dotted_path, error_prefix=''): try: module_path, class_name = dotted_path.rsplit('.', 1) except ValueError: - raise ImproperlyConfigured("{0}{1} doesn't look like " + raise ImproperlyConfigured("{}{} doesn't look like " "a module path".format(error_prefix, dotted_path)) try: module = import_module(module_path) except ImportError as err: - msg = '{0}Error importing module {1}: "{2}"'.format(error_prefix, + msg = '{}Error importing module {}: "{}"'.format(error_prefix, module_path, err) raise ImproperlyConfigured(msg).with_traceback(sys.exc_info()[2]) try: attr = getattr(module, class_name) except AttributeError: - raise ImproperlyConfigured('{0}Module "{1}" does not define a ' - '"{2}" attribute/class'.format(error_prefix, + raise ImproperlyConfigured('{}Module "{}" does not define a ' + '"{}" attribute/class'.format(error_prefix, module_path, class_name)) return attr @@ -61,7 +61,7 @@ def reraise(exc, prefix=None, suffix=None): suffix = '' elif not (suffix.startswith('(') and suffix.endswith(')')): suffix = '(' + suffix + ')' - exc.args = ('{0} {1} {2}'.format(prefix, args[0], suffix),) + args[1:] + exc.args = (f'{prefix} {args[0]} {suffix}',) + args[1:] raise exc diff --git a/configurations/values.py b/configurations/values.py index 1a30794..b1a9e9b 100644 --- a/configurations/values.py +++ b/configurations/values.py @@ -92,7 +92,7 @@ def full_environ_name(self, name): else: environ_name = name.upper() if self.environ_prefix: - environ_name = '{0}_{1}'.format(self.environ_prefix, environ_name) + environ_name = f'{self.environ_prefix}_{environ_name}' return environ_name def setup(self, name): @@ -102,8 +102,8 @@ def setup(self, name): if full_environ_name in os.environ: value = self.to_python(os.environ[full_environ_name]) elif self.environ_required: - raise ValueError('Value {0!r} is required to be set as the ' - 'environment variable {1!r}' + raise ValueError('Value {!r} is required to be set as the ' + 'environment variable {!r}' .format(name, full_environ_name)) self.value = value return value @@ -128,7 +128,7 @@ class BooleanValue(Value): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self.default not in (True, False): - raise ValueError('Default value {0!r} is not a ' + raise ValueError('Default value {!r} is not a ' 'boolean value'.format(self.default)) def to_python(self, value): @@ -139,7 +139,7 @@ def to_python(self, value): return False else: raise ValueError('Cannot interpret ' - 'boolean value {0!r}'.format(value)) + 'boolean value {!r}'.format(value)) class CastingMixin: @@ -152,12 +152,12 @@ def __init__(self, *args, **kwargs): try: self._caster = import_string(self.caster) except ImportError as err: - msg = "Could not import {!r}".format(self.caster) + msg = f"Could not import {self.caster!r}" raise ImproperlyConfigured(msg) from err elif callable(self.caster): self._caster = self.caster else: - error = 'Cannot use caster of {0} ({1!r})'.format(self, + error = 'Cannot use caster of {} ({!r})'.format(self, self.caster) raise ValueError(error) try: @@ -345,13 +345,13 @@ def __init__(self, *args, **kwargs): try: self._validator = import_string(self.validator) except ImportError as err: - msg = "Could not import {!r}".format(self.validator) + msg = f"Could not import {self.validator!r}" raise ImproperlyConfigured(msg) from err elif callable(self.validator): self._validator = self.validator else: raise ValueError('Cannot use validator of ' - '{0} ({1!r})'.format(self, self.validator)) + '{} ({!r})'.format(self, self.validator)) if self.default: self.to_python(self.default) @@ -397,7 +397,7 @@ def setup(self, name): value = super().setup(name) value = os.path.expanduser(value) if self.check_exists and not os.path.exists(value): - raise ValueError('Path {0!r} does not exist.'.format(value)) + raise ValueError(f'Path {value!r} does not exist.') return os.path.abspath(value) @@ -414,7 +414,7 @@ def __init__(self, *args, **kwargs): def setup(self, name): value = super().setup(name) if not value: - raise ValueError('Secret value {0!r} is not set'.format(name)) + raise ValueError(f'Secret value {name!r} is not set') return value diff --git a/tests/test_values.py b/tests/test_values.py index 55478e6..9d7ad39 100644 --- a/tests/test_values.py +++ b/tests/test_values.py @@ -34,7 +34,7 @@ class ValueTests(TestCase): def test_value_with_default(self): value = Value('default', environ=False) - self.assertEqual(type(value), type('default')) + self.assertEqual(type(value), str) self.assertEqual(value, 'default') self.assertEqual(str(value), 'default') @@ -44,17 +44,17 @@ def test_value_with_default_and_late_binding(self): with env(DJANGO_TEST='override'): self.assertEqual(value.setup('TEST'), 'default') value = Value(environ_name='TEST') - self.assertEqual(type(value), type('override')) + self.assertEqual(type(value), str) self.assertEqual(value, 'override') self.assertEqual(str(value), 'override') - self.assertEqual('{0}'.format(value), 'override') + self.assertEqual(f'{value}', 'override') self.assertEqual('%s' % value, 'override') value = Value(environ_name='TEST', late_binding=True) self.assertEqual(type(value), Value) self.assertEqual(value.value, 'override') self.assertEqual(str(value), 'override') - self.assertEqual('{0}'.format(value), 'override') + self.assertEqual(f'{value}', 'override') self.assertEqual('%s' % value, 'override') self.assertEqual(repr(value), repr('override'))