Skip to content

Commit

Permalink
Run pyupgrade on the code
Browse files Browse the repository at this point in the history
But don't touch string formatting.

https://pypi.org/project/pyupgrade/
  • Loading branch information
michael-k authored and Asif Saif Uddin committed Jan 19, 2021
1 parent fcdbfc0 commit 6cb932e
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 149 deletions.
9 changes: 4 additions & 5 deletions configurations/base.py
@@ -1,6 +1,5 @@
import os
import re
import six

from django.conf import global_settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -37,14 +36,14 @@ def __new__(cls, name, bases, attrs):
# 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.
Expand Down Expand Up @@ -91,10 +90,10 @@ def load_dotenv(cls):
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)
Expand Down
4 changes: 2 additions & 2 deletions configurations/importer.py
Expand Up @@ -51,7 +51,7 @@ def create_parser(self, prog_name, subcommand):
installed = True


class ConfigurationImporter(object):
class ConfigurationImporter:
modvar = SETTINGS_ENVIRONMENT_VARIABLE
namevar = CONFIGURATION_ENVIRONMENT_VARIABLE
error_msg = ("Configuration cannot be imported, "
Expand Down Expand Up @@ -134,7 +134,7 @@ def find_module(self, fullname, path=None):
return None


class ConfigurationLoader(object):
class ConfigurationLoader:

def __init__(self, name, location):
self.name = name
Expand Down
103 changes: 32 additions & 71 deletions configurations/utils.py
@@ -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
Expand All @@ -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=''):
Expand All @@ -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:
Expand All @@ -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)

0 comments on commit 6cb932e

Please sign in to comment.