Skip to content

Commit 30a7c43

Browse files
committed
pylint it
1 parent 1a4088b commit 30a7c43

File tree

15 files changed

+76
-113
lines changed

15 files changed

+76
-113
lines changed

unpythonic/amb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def mklist(value, unpack_iterable=True): # pack value into a List if not alread
198198
return tuple(mlst)
199199

200200
class List:
201+
"""List monad."""
201202
def __init__(self, *elts): # unit: x: a -> M a
202203
# Accept the sentinel nil as a special **item** that, when passed to
203204
# the List constructor, produces an empty list.
@@ -223,8 +224,7 @@ def then(self, f): # self: M a, f : M b -> M b
223224
def guard(cls, b): # bool -> List (for the list monad)
224225
if b:
225226
return cls(True) # List with one element; value not intended to be actually used.
226-
else:
227-
return cls() # 0-element List; short-circuit this branch of the computation.
227+
return cls() # 0-element List; short-circuit this branch of the computation.
228228

229229
def __getitem__(self, i): # make List iterable so that "for result in f(elt)" works
230230
return self.x[i] # (when f outputs a List monad)

unpythonic/arity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from inspect import signature, Parameter
1414

1515
class UnknownArity(ValueError):
16-
pass
16+
"""Raised when the arity of a function cannot be inspected."""
1717

1818
def arities(f):
1919
"""Inspect f's minimum and maximum positional arity.
@@ -41,7 +41,7 @@ def arities(f):
4141
u = 0
4242
poskinds = set((Parameter.POSITIONAL_ONLY,
4343
Parameter.POSITIONAL_OR_KEYWORD))
44-
for k, v in signature(f).parameters.items():
44+
for _, v in signature(f).parameters.items():
4545
if v.kind in poskinds:
4646
u += 1
4747
if v.default is Parameter.empty:

unpythonic/dynscope.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def _asdict(self):
108108

109109
def __iter__(self):
110110
return iter(self._asdict())
111+
# no __next__, iterating over dict.
111112

112113
def items(self):
113114
"""Like dict.items(). Return a snapshot of the current state."""

unpythonic/env.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ def __getattr__(self, name):
7777
# Block invalid names in subscripting (which redirects here).
7878
if not name.isidentifier():
7979
raise ValueError("'{}' is not a valid identifier".format(name))
80-
env = self._env # __getattr__ not called if direct attr lookup succeeds, no need for hook.
81-
if name in env:
82-
return env[name]
80+
e = self._env # __getattr__ not called if direct attr lookup succeeds, no need for hook.
81+
if name in e:
82+
return e[name]
8383
else:
8484
raise AttributeError("name '{:s}' is not defined".format(name))
8585

@@ -90,11 +90,10 @@ def __contains__(self, k):
9090
# iteration
9191
def __iter__(self):
9292
return self._env.__iter__()
93-
94-
def __next__(self):
95-
return self._env.__next__()
93+
# no __next__, iterating over dict.
9694

9795
def items(self):
96+
"""Like dict.items()."""
9897
return self._env.items()
9998

10099
def __len__(self):

unpythonic/fasttco.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ def test():
268268
def fact(n, acc=1):
269269
if n == 0:
270270
return acc
271-
else:
272-
return jump(fact, n - 1, n * acc)
271+
return jump(fact, n - 1, n * acc)
273272
assert fact(4) == 24
274273

275274
# tail recursion in a lambda
@@ -282,14 +281,12 @@ def fact(n, acc=1):
282281
def even(n):
283282
if n == 0:
284283
return True
285-
else:
286-
return jump(odd, n - 1)
284+
return jump(odd, n - 1)
287285
@trampolined
288286
def odd(n):
289287
if n == 0:
290288
return False
291-
else:
292-
return jump(even, n - 1)
289+
return jump(even, n - 1)
293290
assert even(42) is True
294291
assert odd(4) is False
295292
assert even(10000) is True # no crash
@@ -340,7 +337,7 @@ def foo3():
340337
import time
341338

342339
t0 = time.time()
343-
for i in range(n):
340+
for _ in range(n):
344341
pass
345342
dt_ip = time.time() - t0
346343

unpythonic/fold.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def test():
206206
assert tuple(scanl1(add, (1, 2, 3))) == (1, 3, 6)
207207
assert tuple(scanr1(add, (1, 2, 3))) == (3, 5, 6)
208208

209-
from operator import add, mul
210209
psums = composer(tail, curry(scanl, add, 0)) # tail to drop the init value
211210
pprods = composer(tail, curry(scanl, mul, 1))
212211
data = range(1, 5)

unpythonic/fploop.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def iter(loop, i=0):
2929

3030
from functools import partial
3131

32+
from unpythonic.ec import call_ec
33+
from unpythonic.arity import arity_includes, UnknownArity
34+
3235
# TCO implementation switchable at runtime
3336
import unpythonic.rc
3437
if unpythonic.rc._tco_impl == "exc":
@@ -38,11 +41,6 @@ def iter(loop, i=0):
3841
else:
3942
raise ValueError("Unknown TCO implementation '{}'".format(unpythonic.rc._tco_impl))
4043

41-
from unpythonic.ec import call_ec
42-
43-
# evil inspect dependency, used only to provide informative error messages.
44-
from unpythonic.arity import arity_includes, UnknownArity
45-
4644
def looped(body):
4745
"""Decorator to make a functional loop and run it immediately.
4846
@@ -352,18 +350,16 @@ def test():
352350
def s(loop, acc=0, i=0):
353351
if i == 10:
354352
return acc # there's no "break"; loop terminates at the first normal return
355-
else:
356-
# same as return jump(SELF, acc+i, i+1), but sets up the "loop" arg.
357-
return loop(acc + i, i + 1)
353+
# same as return jump(SELF, acc+i, i+1), but sets up the "loop" arg.
354+
return loop(acc + i, i + 1)
358355
assert s == 45
359356

360357
# equivalent to:
361358
@trampolined
362359
def dowork(acc=0, i=0):
363360
if i == 10:
364361
return acc
365-
else:
366-
return jump(dowork, acc + i, i + 1)
362+
return jump(dowork, acc + i, i + 1)
367363
s = dowork() # when using just @trampolined, must start the loop manually
368364
assert s == 45
369365

@@ -429,8 +425,7 @@ def out(loop, i=0, acc=[]):
429425
if i < 3:
430426
acc.append(i)
431427
return loop(i + 1)
432-
else:
433-
return acc
428+
return acc
434429
assert out == [0, 1, 2]
435430

436431
# there's no "continue"; package your own:
@@ -441,8 +436,7 @@ def s(loop, acc=0, i=0):
441436
return cont()
442437
elif i == 10:
443438
return acc
444-
else:
445-
return cont(acc + i)
439+
return cont(acc + i)
446440
assert s == 35
447441

448442
# FP loop over iterable
@@ -501,8 +495,7 @@ def filter_fp(predicate, iterable):
501495
def out(loop, x, acc):
502496
if predicate(x):
503497
return loop(acc + (x,))
504-
else:
505-
return loop(acc)
498+
return loop(acc)
506499
return out
507500
assert filter_fp(lambda x: x % 2 == 0, range(10)) == (0, 2, 4, 6, 8)
508501

@@ -638,8 +631,7 @@ def s(loop, acc=0, i=0):
638631
cont = lambda newacc=acc: loop(newacc, i + 1)
639632
if i < 10:
640633
return cont(acc + i)
641-
else:
642-
return acc
634+
return acc
643635
print("s is {:d}".format(s))
644636
return 2 * s
645637
assert result == 90
@@ -680,8 +672,7 @@ def s(loop, acc=0, i=0):
680672
def result(loop, brk, acc=0, i=0):
681673
if i == 10:
682674
return brk(acc)
683-
else:
684-
return loop(acc + i, i + 1) # provide the additional parameters
675+
return loop(acc + i, i + 1) # provide the additional parameters
685676
assert result == 45
686677

687678
# break, continue

unpythonic/fun.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def memoized(*args, **kwargs):
4848
sentinel, value = memo[k]
4949
if sentinel is success:
5050
return value
51-
else:
52-
raise value
51+
raise value
5352
return memoized
5453

5554
#def memoize_simple(f): # essential idea, without exception handling
@@ -159,15 +158,14 @@ def curried(*args, **kwargs):
159158
now_args, later_args = args[:max_arity], args[max_arity:]
160159
now_result = f(*now_args, **kwargs) # use up all kwargs now
161160
if callable(now_result):
162-
# curry it now, to sustain the chain in case we still have
163-
# too many args.
161+
# curry it now, to sustain the chain in case we have
162+
# too many (or too few) args for it.
164163
if not iscurried(now_result):
165164
now_result = curry(now_result)
166165
return now_result(*later_args)
167-
elif isinstance(now_result, (tuple, list)):
166+
if isinstance(now_result, (tuple, list)):
168167
return tuple(now_result) + later_args
169-
else:
170-
return (now_result,) + later_args
168+
return (now_result,) + later_args
171169
return f(*args, **kwargs)
172170
curried._is_curried_function = True # stash for detection
173171
# curry itself is curried: if we get args, they're the first step
@@ -394,8 +392,7 @@ def composed(*args):
394392
# (consider chaining functions that manipulate a generator).
395393
if isinstance(a, (list, tuple)):
396394
return f(*a)
397-
else:
398-
return f(a)
395+
return f(a)
399396
return composed
400397
if direction == "right":
401398
compose_two = flip(compose_two)

unpythonic/fup.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,33 @@ def make_output(seq):
9797
gen = (x for x in seq)
9898
if hasattr(cls, "_make"): # namedtuple support
9999
return cls._make(gen)
100-
else:
101-
return cls(gen)
100+
return cls(gen)
102101
if isinstance(indices, (list, tuple)):
103102
seq = target
104103
for index, value in zip(indices, values):
105104
seq = ShadowedSequence(seq, index, value)
106105
return make_output(seq)
107-
else: # one index (or slice), value(s) pair only
108-
return make_output(ShadowedSequence(target, indices, values))
109-
elif mappings:
106+
# one index (or slice), value(s) pair only
107+
return make_output(ShadowedSequence(target, indices, values))
108+
if mappings:
110109
t = copy(target)
111110
t.update(**mappings) # TODO: use collections.ChainMap instead?
112111
return t
113112
return copy(target)
114113

115114
# Needed by fupdate for immutable sequence inputs (no item assignment).
116115
class ShadowedSequence(Sequence):
117-
def __init__(self, seq, ix, v):
118-
"""Sequence with some elements shadowed by those from another sequence.
116+
"""Sequence with some elements shadowed by those from another sequence.
119117
120-
Or in other words, a functionally updated view of a sequence.
118+
Or in other words, a functionally updated view of a sequence.
121119
122-
Essentially, ``out[k] = v[index_in_slice(k, ix)] if in_slice(k, ix) else seq[k]``,
123-
but doesn't actually allocate ``out``.
120+
Essentially, ``out[k] = v[index_in_slice(k, ix)] if in_slice(k, ix) else seq[k]``,
121+
but doesn't actually allocate ``out``.
124122
125-
``ix`` may be integer (if ``v`` represents one item only)
126-
or slice (if ``v`` is intended as a sequence).
127-
"""
123+
``ix`` may be integer (if ``v`` represents one item only)
124+
or slice (if ``v`` is intended as a sequence).
125+
"""
126+
def __init__(self, seq, ix, v):
128127
self.seq = seq
129128
self.ix = ix
130129
self.v = v
@@ -141,10 +140,8 @@ def __getitem__(self, k):
141140
# in fupdate automatically catches that, hiding the error.
142141
raise ValueError("Replacement sequence too short; attempted to access index {} with len {} (items: {})".format(i, len(self.v), self.v))
143142
return self.v[i]
144-
else: # int, just one item
145-
return self.v
146-
else:
147-
return self.seq[k]
143+
return self.v # int, just one item
144+
return self.seq[k] # not in slice
148145

149146
def __len__(self):
150147
return len(self.seq)
@@ -175,9 +172,8 @@ def in_slice(i, s, l=None):
175172
before_stop = cmp_end(i, stop)
176173
on_grid = (i - start) % step == 0
177174
return at_or_after_start and on_grid and before_stop
178-
else:
179-
s = wrap(s)
180-
return i == s
175+
s = wrap(s) # int
176+
return i == s
181177

182178
def index_in_slice(i, s, l=None):
183179
"""Return the index of the int i in the slice s, or None if i is not in s.

unpythonic/gmemo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ def __next__(self):
127127
self.j += 1
128128
if sentinel is _success:
129129
return value
130-
else:
131-
raise value
130+
raise value
132131

133132
def imemoize(iterable):
134133
"""Memoize an iterable.

0 commit comments

Comments
 (0)