Skip to content

Commit

Permalink
Upgrade Python syntax with pyupgrade --py37-plus
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Aug 23, 2023
1 parent 8bb1561 commit ad8ff9d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# decorator documentation build configuration file, created by
# sphinx-quickstart on Sun Jul 23 17:11:44 2017.
Expand Down
11 changes: 5 additions & 6 deletions src/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


# this is not used anymore in the core, but kept for backward compatibility
class FunctionMaker(object):
class FunctionMaker:
"""
An object with the ability to create functions with a given signature.
It has attributes name, doc, module, signature, defaults, dict and
Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(self, func=None, name=None, signature=None,
allargs.append('*') # single star syntax
for a in self.kwonlyargs:
allargs.append('%s=None' % a)
allshortargs.append('%s=%s' % (a, a))
allshortargs.append(f'{a}={a}')
if self.varkw:
allargs.append('**' + self.varkw)
allshortargs.append('**' + self.varkw)
Expand Down Expand Up @@ -146,7 +146,7 @@ def make(self, src_templ, evaldict=None, addsource=False, **attrs):
self.shortsignature.split(',')])
for n in names:
if n in ('_func_', '_call_'):
raise NameError('%s is overridden in\n%s' % (n, src))
raise NameError(f'{n} is overridden in\n{src}')

if not src.endswith('\n'): # add a newline for old Pythons
src += '\n'
Expand Down Expand Up @@ -223,8 +223,7 @@ async def fun(*args, **kw):
def fun(*args, **kw):
if not kwsyntax:
args, kw = fix(args, kw, sig)
for res in caller(func, *(extras + args), **kw):
yield res
yield from caller(func, *(extras + args), **kw)
else:
def fun(*args, **kw):
if not kwsyntax:
Expand Down Expand Up @@ -392,7 +391,7 @@ def ancestors(*types):
n_vas = len(vas)
if n_vas > 1:
raise RuntimeError(
'Ambiguous dispatch for %s: %s' % (t, vas))
f'Ambiguous dispatch for {t}: {vas}')
elif n_vas == 1:
va, = vas
mro = type('t', (t, va), {}).mro()[1:]
Expand Down
30 changes: 15 additions & 15 deletions src/tests/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,8 @@ def decorator_apply(dec, func):


def _trace(f, *args, **kw):
kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
kwstr = ', '.join(f'{k!r}: {kw[k]!r}' for k in sorted(kw))
print(f"calling {f.__name__} with args {args}, {{{kwstr}}}")
return f(*args, **kw)


Expand All @@ -1409,11 +1409,11 @@ def __init__(self, func, *args, **kw):
counter = func.counter
except AttributeError: # instantiate the counter at the first call
counter = func.counter = itertools.count(1)
name = '%s-%s' % (func.__name__, next(counter))
name = f'{func.__name__}-{next(counter)}'

def func_wrapper():
self._result = func(*args, **kw)
super(Future, self).__init__(target=func_wrapper, name=name)
super().__init__(target=func_wrapper, name=name)
self.start()

def result(self):
Expand Down Expand Up @@ -1489,7 +1489,7 @@ def set_result():
return f.result


class User(object):
class User:
"Will just be able to see a page"


Expand Down Expand Up @@ -1525,7 +1525,7 @@ def restricted(func, user_class=User, *args, **kw):
% (self.user, func.__name__))


class Action(object):
class Action:
@restricted(user_class=User)
def view(self):
"Any user can view objects"
Expand All @@ -1539,7 +1539,7 @@ def delete(self):
"Only the admin can delete objects"


class TailRecursive(object):
class TailRecursive:
"""
tail_recursive decorator based on Kay Schluehr's recipe
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
Expand Down Expand Up @@ -1648,7 +1648,7 @@ def test_kwonly_star_notation():
# ####################### multiple dispatch ############################ #


class XMLWriter(object):
class XMLWriter:
def __init__(self, **config):
self.cfg = config

Expand All @@ -1662,15 +1662,15 @@ def writefloat(self, obj):
return '<float>%s</float>' % obj


class Rock(object):
class Rock:
ordinal = 0


class Paper(object):
class Paper:
ordinal = 1


class Scissors(object):
class Scissors:
ordinal = 2


Expand Down Expand Up @@ -1707,7 +1707,7 @@ def winStrongRockPaper(a, b):
return 0


class WithLength(object):
class WithLength:
def __len__(self):
return 0

Expand All @@ -1734,7 +1734,7 @@ def get_length_set(obj):
return 1


class C(object):
class C:
"Registered as Sized and Iterable"


Expand Down Expand Up @@ -1764,7 +1764,7 @@ def singledispatch_example2():
# adapted from functools.singledispatch test case
singledispatch = dispatch_on('arg')

class S(object):
class S:
pass

class V(c.Sized, S):
Expand Down Expand Up @@ -1856,7 +1856,7 @@ class with a .context attribute.
self = inspect.Parameter('self', inspect.Parameter.POSITIONAL_OR_KEYWORD)
params.insert(0, self) # insert self
del params[-1] # remove context
newsig = '%s%s' % (f.__name__, sig.replace(parameters=params))
newsig = f'{f.__name__}{sig.replace(parameters=params)}'
return FunctionMaker.create(
newsig, 'context = self.context; return _func_%s' % sig,
dict(_func_=f))
Expand Down
12 changes: 6 additions & 6 deletions src/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_mro(self):
def g(obj):
return "base"

class A(object):
class A:
pass

class C(A):
Expand Down Expand Up @@ -250,7 +250,7 @@ def g(obj):

@g.register(int)
def g_int(i):
return "int %s" % (i,)
return f"int {i}"
self.assertEqual(g(""), "base")
self.assertEqual(g(12), "int 12")

Expand Down Expand Up @@ -296,7 +296,7 @@ def _g(obj):
def test_register_abc(self):
d = {"a": "b"}
l = [1, 2, 3]
s = set([object(), None])
s = {object(), None}
f = frozenset(s)
t = (1, 2, 3)

Expand Down Expand Up @@ -425,7 +425,7 @@ def __len__(self):
c.Set.register(O)
self.assertEqual(g(o), "set")

class P(object):
class P:
pass
p = P()
self.assertEqual(g(p), "base")
Expand Down Expand Up @@ -484,7 +484,7 @@ def i_sequence(arg):
with assertRaises(RuntimeError): # was no error
self.assertEqual(i(r), "sequence")

class S(object):
class S:
pass

class T(S, c.Sized):
Expand All @@ -495,7 +495,7 @@ def __len__(self):
c.Container.register(T)
self.assertEqual(h(t), "sized") # because it's explicitly in the MRO

class U(object):
class U:
def __len__(self):
return 0
u = U()
Expand Down

0 comments on commit ad8ff9d

Please sign in to comment.