Run pyupgrade on the code

But don't touch string formatting.

https://pypi.org/project/pyupgrade/
This commit is contained in:
Michael Käufl 2021-01-18 10:56:47 +01:00 committed by Asif Saif Uddin
parent fcdbfc0bcb
commit 6cb932e47f
18 changed files with 97 additions and 149 deletions

View file

@ -1,6 +1,5 @@
import os
import re
import six
from django.conf import global_settings
from django.core.exceptions import ImproperlyConfigured
@ -37,14 +36,14 @@ class ConfigurationBase(type):
# https://github.com/django/django/commit/226ebb17290b604ef29e82fb5c1fbac3594ac163#diff-ec2bed07bb264cb95a80f08d71a47c06R163-R170
if "PASSWORD_RESET_TIMEOUT_DAYS" in attrs and "PASSWORD_RESET_TIMEOUT" in attrs:
attrs.pop("PASSWORD_RESET_TIMEOUT_DAYS")
return super(ConfigurationBase, cls).__new__(cls, name, bases, attrs)
return super().__new__(cls, name, bases, attrs)
def __repr__(self):
return "<Configuration '{0}.{1}'>".format(self.__module__,
self.__name__)
class Configuration(six.with_metaclass(ConfigurationBase)):
class Configuration(metaclass=ConfigurationBase):
"""
The base configuration class to inherit from.
@ -91,10 +90,10 @@ class Configuration(six.with_metaclass(ConfigurationBase)):
try:
with open(dotenv, 'r') as f:
content = f.read()
except IOError as e:
except OSError as e:
raise ImproperlyConfigured("Couldn't read .env file "
"with the path {}. Error: "
"{}".format(dotenv, e))
"{}".format(dotenv, e)) from e
else:
for line in content.splitlines():
m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line)

View file

@ -51,7 +51,7 @@ def install(check_options=False):
installed = True
class ConfigurationImporter(object):
class ConfigurationImporter:
modvar = SETTINGS_ENVIRONMENT_VARIABLE
namevar = CONFIGURATION_ENVIRONMENT_VARIABLE
error_msg = ("Configuration cannot be imported, "
@ -134,7 +134,7 @@ class ConfigurationImporter(object):
return None
class ConfigurationLoader(object):
class ConfigurationLoader:
def __init__(self, name, location):
self.name = name

View file

@ -1,7 +1,7 @@
import inspect
import six
import sys
from functools import partial
from importlib import import_module
from django.core.exceptions import ImproperlyConfigured
@ -12,8 +12,7 @@ def isuppercase(name):
def uppercase_attributes(obj):
return dict((name, getattr(obj, name))
for name in filter(isuppercase, dir(obj)))
return {name: getattr(obj, name) for name in dir(obj) if isuppercase(name)}
def import_by_path(dotted_path, error_prefix=''):
@ -36,8 +35,7 @@ def import_by_path(dotted_path, error_prefix=''):
msg = '{0}Error importing module {1}: "{2}"'.format(error_prefix,
module_path,
err)
six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg),
sys.exc_info()[2])
raise ImproperlyConfigured(msg).with_traceback(sys.exc_info()[2])
try:
attr = getattr(module, class_name)
except AttributeError:
@ -61,77 +59,40 @@ def reraise(exc, prefix=None, suffix=None):
elif not (suffix.startswith('(') and suffix.endswith(')')):
suffix = '(' + suffix + ')'
exc.args = ('{0} {1} {2}'.format(prefix, args[0], suffix),) + args[1:]
raise
raise exc
# Copied over from Sphinx
if sys.version_info >= (3, 0):
from functools import partial
def getargspec(func):
"""Like inspect.getargspec but supports functools.partial as well."""
if inspect.ismethod(func):
func = func.__func__
if type(func) is partial:
orig_func = func.func
argspec = getargspec(orig_func)
args = list(argspec[0])
defaults = list(argspec[3] or ())
kwoargs = list(argspec[4])
kwodefs = dict(argspec[5] or {})
if func.args:
args = args[len(func.args):]
for arg in func.keywords or ():
try:
i = args.index(arg) - len(args)
del args[i]
try:
del defaults[i]
except IndexError:
pass
except ValueError: # must be a kwonly arg
i = kwoargs.index(arg)
del kwoargs[i]
del kwodefs[arg]
return inspect.FullArgSpec(args, argspec[1], argspec[2],
tuple(defaults), kwoargs,
kwodefs, argspec[6])
while hasattr(func, '__wrapped__'):
func = func.__wrapped__
if not inspect.isfunction(func):
raise TypeError('%r is not a Python function' % func)
return inspect.getfullargspec(func)
else: # 2.6, 2.7
from functools import partial
def getargspec(func):
"""Like inspect.getargspec but supports functools.partial as well."""
if inspect.ismethod(func):
func = func.im_func
parts = 0, ()
if type(func) is partial:
keywords = func.keywords
if keywords is None:
keywords = {}
parts = len(func.args), keywords.keys()
func = func.func
if not inspect.isfunction(func):
raise TypeError('%r is not a Python function' % func)
args, varargs, varkw = inspect.getargs(func.func_code)
func_defaults = func.func_defaults
if func_defaults is None:
func_defaults = []
else:
func_defaults = list(func_defaults)
if parts[0]:
args = args[parts[0]:]
if parts[1]:
for arg in parts[1]:
def getargspec(func):
"""Like inspect.getargspec but supports functools.partial as well."""
if inspect.ismethod(func):
func = func.__func__
if type(func) is partial:
orig_func = func.func
argspec = getargspec(orig_func)
args = list(argspec[0])
defaults = list(argspec[3] or ())
kwoargs = list(argspec[4])
kwodefs = dict(argspec[5] or {})
if func.args:
args = args[len(func.args):]
for arg in func.keywords or ():
try:
i = args.index(arg) - len(args)
del args[i]
try:
del func_defaults[i]
del defaults[i]
except IndexError:
pass
return inspect.ArgSpec(args, varargs, varkw, func_defaults)
except ValueError: # must be a kwonly arg
i = kwoargs.index(arg)
del kwoargs[i]
del kwodefs[arg]
return inspect.FullArgSpec(args, argspec[1], argspec[2],
tuple(defaults), kwoargs,
kwodefs, argspec[6])
while hasattr(func, '__wrapped__'):
func = func.__wrapped__
if not inspect.isfunction(func):
raise TypeError('%r is not a Python function' % func)
return inspect.getfullargspec(func)

View file

@ -2,7 +2,6 @@ import ast
import copy
import decimal
import os
import six
import sys
from django.core import validators
@ -20,7 +19,7 @@ def setup_value(target, name, value):
setattr(target, multiple_name, multiple_value)
class Value(object):
class Value:
"""
A single settings value that is able to interpret env variables
and implements a simple validation scheme.
@ -117,7 +116,7 @@ class Value(object):
return value
class MultipleMixin(object):
class MultipleMixin:
multiple = True
@ -126,7 +125,7 @@ class BooleanValue(Value):
false_values = ('no', 'n', 'false', '0', '')
def __init__(self, *args, **kwargs):
super(BooleanValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default not in (True, False):
raise ValueError('Default value {0!r} is not a '
'boolean value'.format(self.default))
@ -142,13 +141,13 @@ class BooleanValue(Value):
'boolean value {0!r}'.format(value))
class CastingMixin(object):
class CastingMixin:
exception = (TypeError, ValueError)
message = 'Cannot interpret value {0!r}'
def __init__(self, *args, **kwargs):
super(CastingMixin, self).__init__(*args, **kwargs)
if isinstance(self.caster, six.string_types):
super().__init__(*args, **kwargs)
if isinstance(self.caster, str):
self._caster = import_by_path(self.caster)
elif callable(self.caster):
self._caster = self.caster
@ -158,9 +157,7 @@ class CastingMixin(object):
raise ValueError(error)
try:
arg_names = getargspec(self._caster)[0]
self._params = dict((name, kwargs[name])
for name in arg_names
if name in kwargs)
self._params = {name: kwargs[name] for name in arg_names if name in kwargs}
except TypeError:
self._params = {}
@ -181,7 +178,7 @@ class IntegerValue(CastingMixin, Value):
class PositiveIntegerValue(IntegerValue):
def to_python(self, value):
int_value = super(PositiveIntegerValue, self).to_python(value)
int_value = super().to_python(value)
if int_value < 0:
raise ValueError(self.message.format(value))
return int_value
@ -213,7 +210,7 @@ class SequenceValue(Value):
converter = kwargs.pop('converter', None)
if converter is not None:
self.converter = converter
super(SequenceValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
# make sure the default is the correct sequence type
if self.default is None:
self.default = self.sequence_type()
@ -257,7 +254,7 @@ class SingleNestedSequenceValue(SequenceValue):
def __init__(self, *args, **kwargs):
self.seq_separator = kwargs.pop('seq_separator', ';')
super(SingleNestedSequenceValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def _convert(self, items):
# This could receive either a bare or nested sequence
@ -266,8 +263,7 @@ class SingleNestedSequenceValue(SequenceValue):
super(SingleNestedSequenceValue, self)._convert(i) for i in items
]
return self.sequence_type(converted_sequences)
return self.sequence_type(
super(SingleNestedSequenceValue, self)._convert(items))
return self.sequence_type(super()._convert(items))
def to_python(self, value):
split_value = [
@ -295,7 +291,7 @@ class BackendsValue(ListValue):
try:
import_by_path(value)
except ImproperlyConfigured as err:
six.reraise(ValueError, ValueError(err), sys.exc_info()[2])
raise ValueError(err).with_traceback(sys.exc_info()[2])
return value
@ -303,28 +299,28 @@ class SetValue(ListValue):
message = 'Cannot interpret set item {0!r} in set {1!r}'
def __init__(self, *args, **kwargs):
super(SetValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default is None:
self.default = set()
else:
self.default = set(self.default)
def to_python(self, value):
return set(super(SetValue, self).to_python(value))
return set(super().to_python(value))
class DictValue(Value):
message = 'Cannot interpret dict value {0!r}'
def __init__(self, *args, **kwargs):
super(DictValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default is None:
self.default = {}
else:
self.default = dict(self.default)
def to_python(self, value):
value = super(DictValue, self).to_python(value)
value = super().to_python(value)
if not value:
return {}
try:
@ -336,11 +332,11 @@ class DictValue(Value):
return evaled_value
class ValidationMixin(object):
class ValidationMixin:
def __init__(self, *args, **kwargs):
super(ValidationMixin, self).__init__(*args, **kwargs)
if isinstance(self.validator, six.string_types):
super().__init__(*args, **kwargs)
if isinstance(self.validator, str):
self._validator = import_by_path(self.validator)
elif callable(self.validator):
self._validator = self.validator
@ -380,16 +376,16 @@ class RegexValue(ValidationMixin, Value):
def __init__(self, *args, **kwargs):
regex = kwargs.pop('regex', None)
self.validator = validators.RegexValidator(regex=regex)
super(RegexValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
class PathValue(Value):
def __init__(self, *args, **kwargs):
self.check_exists = kwargs.pop('check_exists', True)
super(PathValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def setup(self, name):
value = super(PathValue, self).setup(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))
@ -401,13 +397,13 @@ class SecretValue(Value):
def __init__(self, *args, **kwargs):
kwargs['environ'] = True
kwargs['environ_required'] = True
super(SecretValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default is not None:
raise ValueError('Secret values are only allowed to '
'be set as environment variables')
def setup(self, name):
value = super(SecretValue, self).setup(name)
value = super().setup(name)
if not value:
raise ValueError('Secret value {0!r} is not set'.format(name))
return value
@ -422,7 +418,7 @@ class EmailURLValue(CastingMixin, MultipleMixin, Value):
kwargs.setdefault('environ', True)
kwargs.setdefault('environ_prefix', None)
kwargs.setdefault('environ_name', 'EMAIL_URL')
super(EmailURLValue, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default is None:
self.default = {}
else:
@ -437,14 +433,14 @@ class DictBackendMixin(Value):
kwargs.setdefault('environ', True)
kwargs.setdefault('environ_prefix', None)
kwargs.setdefault('environ_name', self.environ_name)
super(DictBackendMixin, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.default is None:
self.default = {}
else:
self.default = self.to_python(self.default)
def to_python(self, value):
value = super(DictBackendMixin, self).to_python(value)
value = super().to_python(value)
return {self.alias: value}

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# django-configurations documentation build configuration file, created by
# sphinx-quickstart on Sat Jul 21 15:03:23 2012.
#
@ -43,8 +41,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'django-configurations'
copyright = u'2012-2014, Jannis Leidel and other contributors'
project = 'django-configurations'
copyright = '2012-2014, Jannis Leidel and other contributors'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
@ -186,8 +184,8 @@ latex_elements = {
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'django-configurations.tex', u'django-configurations Documentation',
u'Jannis Leidel', 'manual'),
('index', 'django-configurations.tex', 'django-configurations Documentation',
'Jannis Leidel', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
@ -216,8 +214,8 @@ latex_documents = [
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'django-configurations', u'django-configurations Documentation',
[u'Jannis Leidel'], 1)
('index', 'django-configurations', 'django-configurations Documentation',
['Jannis Leidel'], 1)
]
# If true, show URL addresses after external links.
@ -230,8 +228,8 @@ man_pages = [
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'django-configurations', u'django-configurations Documentation',
u'Jannis Leidel', 'django-configurations', 'One line description of project.',
('index', 'django-configurations', 'django-configurations Documentation',
'Jannis Leidel', 'django-configurations', 'One line description of project.',
'Miscellaneous'),
]
@ -248,10 +246,10 @@ texinfo_documents = [
# -- Options for Epub output ---------------------------------------------------
# Bibliographic Dublin Core info.
epub_title = u'django-configurations'
epub_author = u'Jannis Leidel'
epub_publisher = u'Jannis Leidel'
epub_copyright = u'2012, Jannis Leidel'
epub_title = 'django-configurations'
epub_author = 'Jannis Leidel'
epub_publisher = 'Jannis Leidel'
epub_copyright = '2012, Jannis Leidel'
# The language of the text. It defaults to the language option
# or en if the language is not set.

View file

@ -91,7 +91,7 @@ a few mixin you re-use multiple times:
.. code-block:: python
class FullPageCaching(object):
class FullPageCaching:
USE_ETAGS = True
Then import that mixin class in your site settings module and use it with

View file

@ -1,4 +1,3 @@
from __future__ import print_function
import os
import codecs
from setuptools import setup
@ -27,7 +26,7 @@ setup(
'django-cadmin = configurations.management:execute_from_command_line',
],
},
install_requires=['six'],
install_requires=[],
extras_require={
'cache': ['django-cache-url'],
'database': ['dj-database-url'],

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import os
import sys

View file

@ -41,7 +41,7 @@ class Test(Configuration):
@property
def ALLOWED_HOSTS(self):
allowed_hosts = super(Test, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('base')
return allowed_hosts

View file

@ -1,19 +1,19 @@
from configurations import Configuration
class Mixin1(object):
class Mixin1:
@property
def ALLOWED_HOSTS(self):
allowed_hosts = super(Mixin1, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('test1')
return allowed_hosts
class Mixin2(object):
class Mixin2:
@property
def ALLOWED_HOSTS(self):
allowed_hosts = super(Mixin2, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('test2')
return allowed_hosts
@ -21,6 +21,6 @@ class Mixin2(object):
class Inheritance(Mixin2, Mixin1, Configuration):
def ALLOWED_HOSTS(self):
allowed_hosts = super(Inheritance, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('test3')
return allowed_hosts

View file

@ -4,6 +4,6 @@ from .single_inheritance import Inheritance as BaseInheritance
class Inheritance(BaseInheritance):
def ALLOWED_HOSTS(self):
allowed_hosts = super(Inheritance, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('test-test')
return allowed_hosts

View file

@ -5,6 +5,6 @@ class Inheritance(Base):
@property
def ALLOWED_HOSTS(self):
allowed_hosts = super(Inheritance, self).ALLOWED_HOSTS[:]
allowed_hosts = super().ALLOWED_HOSTS[:]
allowed_hosts.append('test')
return allowed_hosts

View file

@ -1,9 +1,6 @@
"""Used by tests to ensure logging is kept when calling setup() twice."""
try:
from unittest import mock
except ImportError:
from mock import mock
from unittest import mock
import configurations

View file

@ -1,6 +1,6 @@
import os
from django.test import TestCase
from mock import patch
from unittest.mock import patch
class DotEnvLoadingTests(TestCase):

View file

@ -2,7 +2,7 @@ import os
from django.test import TestCase
from mock import patch
from unittest.mock import patch
class InheritanceTests(TestCase):

View file

@ -5,7 +5,7 @@ import sys
from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured
from mock import patch
from unittest.mock import patch
from configurations.importer import ConfigurationImporter

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import subprocess
import os

View file

@ -5,7 +5,7 @@ from contextlib import contextmanager
from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured
from mock import patch
from unittest.mock import patch
from configurations.values import (Value, BooleanValue, IntegerValue,
FloatValue, DecimalValue, ListValue,
@ -270,9 +270,9 @@ class ValueTests(TestCase):
def test_set_values_default(self):
value = SetValue()
with env(DJANGO_TEST='2,2'):
self.assertEqual(value.setup('TEST'), set(['2', '2']))
self.assertEqual(value.setup('TEST'), {'2', '2'})
with env(DJANGO_TEST='2, 2 ,'):
self.assertEqual(value.setup('TEST'), set(['2', '2']))
self.assertEqual(value.setup('TEST'), {'2', '2'})
with env(DJANGO_TEST=''):
self.assertEqual(value.setup('TEST'), set())
@ -485,12 +485,12 @@ class ValueTests(TestCase):
self.assertEqual(value.value, set())
value = SetValue([1, 2])
self.assertEqual(value.default, set([1, 2]))
self.assertEqual(value.value, set([1, 2]))
self.assertEqual(value.default, {1, 2})
self.assertEqual(value.value, {1, 2})
def test_setup_value(self):
class Target(object):
class Target:
pass
value = EmailURLValue()