Skip to content

Commit e667ee4

Browse files
committed
Enforce Flake8 Check
* Fixes all flake8 errors/warnings * Removes PY2/PY3 detection * Removes Python 2-isms (long, xrange, etc) * Fixes test that was silently shadowed (test_merge_replace) * Adds Flake8 check to the tox test environments
1 parent cc03e68 commit e667ee4

9 files changed

Lines changed: 95 additions & 121 deletions

File tree

dpath/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
import sys
2-
3-
# Python version flags for Python 3 support
4-
python_major_version = 0
5-
if hasattr(sys.version_info, 'major'):
6-
python_major_version = sys.version_info.major
7-
else:
8-
python_major_version = sys.version_info[0]
9-
10-
PY2 = ( python_major_version == 2 )
11-
PY3 = ( python_major_version == 3 )
12-
13-
from .util import *

dpath/segments.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,16 @@
44
from fnmatch import fnmatchcase
55

66

7-
from . import PY2
8-
9-
10-
if PY2:
11-
from itertools import ifilter, izip
12-
else:
13-
ifilter = filter
14-
izip = zip
15-
xrange = range
16-
unicode = str
17-
long = int
18-
19-
207
def kvs(node):
218
'''
229
Return a (key, value) iterator for the node.
2310
2411
kvs(node) -> (generator -> (key, value))
2512
'''
2613
try:
27-
if PY2:
28-
return node.iteritems()
29-
else:
30-
return iter(node.items())
14+
return iter(node.items())
3115
except AttributeError:
32-
return izip(xrange(len(node)), node)
16+
return zip(range(len(node)), node)
3317

3418

3519
def leaf(thing):
@@ -38,10 +22,8 @@ def leaf(thing):
3822
3923
leaf(thing) -> bool
4024
'''
41-
if PY2:
42-
leaves = (str, unicode, int, long, float, bool, type(None))
43-
else:
44-
leaves = (bytes, str, int, float, bool, type(None))
25+
leaves = (bytes, str, int, float, bool, type(None))
26+
4527
return isinstance(thing, leaves)
4628

4729

@@ -140,7 +122,7 @@ def leaves(obj):
140122
141123
leaves(obj) -> (generator -> (segment, value))
142124
'''
143-
return ifilter(lambda p: leafy(p[1]), walk(obj))
125+
return filter(lambda p: leafy(p[1]), walk(obj))
144126

145127

146128
def int_str(segment):
@@ -154,7 +136,7 @@ def int_str(segment):
154136
155137
int_str(segment) -> str
156138
'''
157-
if isinstance(segment, (int, long)):
139+
if isinstance(segment, int):
158140
return str(segment)
159141
return segment
160142

@@ -270,7 +252,7 @@ def extend(thing, index, value=None):
270252
# Using this rather than the multiply notation in order to support a
271253
# wider variety of sequence like things.
272254
extra = (index + 1) - len(thing)
273-
for i in xrange(extra):
255+
for i in range(extra):
274256
expansion += [value]
275257
thing.extend(expansion)
276258
except TypeError:
@@ -284,15 +266,15 @@ def extend(thing, index, value=None):
284266

285267
def __default_creator__(current, segments, i, hints=()):
286268
'''
287-
Create missing path components. If the segment is an int or long, then it
288-
will create a list. Otherwise a dictionary is created.
269+
Create missing path components. If the segment is an int, then it will
270+
create a list. Otherwise a dictionary is created.
289271
290272
set(obj, segments, value) -> obj
291273
'''
292274
segment = segments[i]
293275
length = len(segments)
294276

295-
if isinstance(segment, (int, long)):
277+
if isinstance(segment, int):
296278
extend(current, segment)
297279

298280
# Infer the type from the hints provided.
@@ -306,7 +288,7 @@ def __default_creator__(current, segments, i, hints=()):
306288
else:
307289
segment_next = None
308290

309-
if isinstance(segment_next, (int, long)):
291+
if isinstance(segment_next, int):
310292
current[segment] = []
311293
else:
312294
current[segment] = {}
@@ -342,7 +324,7 @@ def set(obj, segments, value, creator=__default_creator__, hints=()):
342324
if i != length - 1 and leaf(current):
343325
raise PathNotFound('Path: {}[{}]'.format(segments, i))
344326

345-
if isinstance(segments[-1], (int, long)):
327+
if isinstance(segments[-1], int):
346328
extend(current, segments[-1])
347329

348330
current[segments[-1]] = value

dpath/util.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
from dpath.exceptions import InvalidKeyName
1+
from collections.abc import MutableMapping
2+
from collections.abc import MutableSequence
23
from dpath import options
4+
from dpath.exceptions import InvalidKeyName
35
import dpath.segments
46

5-
try:
6-
#python3, especially 3.8
7-
from collections.abc import MutableSequence
8-
from collections.abc import MutableMapping
9-
except ImportError:
10-
#python2
11-
from collections import MutableSequence
12-
from collections import MutableMapping
13-
147
MERGE_REPLACE = (1 << 1)
158
MERGE_ADDITIVE = (1 << 2)
169
MERGE_TYPESAFE = (1 << 3)
@@ -60,7 +53,7 @@ def new(obj, path, value, separator='/', creator=None):
6053
characters in it, they will become part of the resulting
6154
keys
6255
63-
creator allows you to pass in a creator method that is
56+
creator allows you to pass in a creator method that is
6457
responsible for creating missing keys at arbitrary levels of
6558
the path (see the help for dpath.path.set)
6659
'''
@@ -133,6 +126,7 @@ def set(obj, glob, value, separator='/', afilter=None):
133126
to the given value. Returns the number of elements changed.
134127
'''
135128
globlist = __safe_path__(glob, separator)
129+
136130
def f(obj, pair, counter):
137131
(segments, found) = pair
138132

@@ -248,7 +242,7 @@ def merge(dst, src, separator='/', afilter=None, flags=MERGE_ADDITIVE):
248242
249243
>>> a = {'a': [0] }
250244
>>> b = {'a': [1] }
251-
245+
252246
... and you merge them into an empty dictionary, like so:
253247
254248
>>> d = {}
@@ -276,12 +270,14 @@ def merge(dst, src, separator='/', afilter=None, flags=MERGE_ADDITIVE):
276270
filtered_src = search(src, '**', afilter=afilter, separator='/')
277271

278272
def are_both_mutable(o1, o2):
279-
if ( (isinstance(o1, MutableMapping) and isinstance(o2, MutableMapping)) or
280-
(isinstance(o1, MutableSequence) and isinstance(o2, MutableSequence))
281-
):
273+
mapP = isinstance(o1, MutableMapping) and isinstance(o2, MutableMapping)
274+
seqP = isinstance(o1, MutableSequence) and isinstance(o2, MutableSequence)
275+
276+
if mapP or seqP:
282277
return True
278+
283279
return False
284-
280+
285281
def merger(dst, src, _segments=()):
286282
for key, found in dpath.segments.kvs(src):
287283
# Our current path in the source.
@@ -298,7 +294,7 @@ def merger(dst, src, _segments=()):
298294
target = dpath.segments.get(dst, segments)
299295
tt = type(target)
300296
ft = type(found)
301-
if ( tt != ft ):
297+
if tt != ft:
302298
path = separator.join(segments)
303299
raise TypeError("Cannot merge objects of type"
304300
"{0} and {1} at {2}"
@@ -313,9 +309,7 @@ def merger(dst, src, _segments=()):
313309
target = dpath.segments.get(dst, segments)
314310

315311
# If the types don't match, replace it.
316-
if ( (type(found) != type(target)) and
317-
(not are_both_mutable(found, target))
318-
):
312+
if ((type(found) != type(target)) and (not are_both_mutable(found, target))):
319313
dpath.segments.set(dst, segments, found)
320314
continue
321315

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from distutils.core import setup
22
import dpath.version
33
import os
4-
import sys
4+
55

66
long_description = open(
77
os.path.join(
@@ -36,4 +36,3 @@
3636
'Topic :: Software Development :: Libraries :: Python Modules',
3737
],
3838
)
39-

tests/test_broken_afilter.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import nose
21
import dpath.util
32
import sys
43

4+
55
def test_broken_afilter():
66
def afilter(x):
77
if x in [1, 2]:
@@ -15,15 +15,16 @@ def afilter(x):
1515
"c": {
1616
"d": 0,
1717
"e": 1,
18-
"f": 2
19-
}
20-
}
21-
}
22-
}
18+
"f": 2,
19+
},
20+
},
21+
},
22+
}
2323
paths = [
2424
'a/b/c/e',
25-
'a/b/c/f'
26-
]
25+
'a/b/c/f',
26+
]
27+
2728
for (path, value) in dpath.util.search(dict, '/**', yielded=True, afilter=afilter):
2829
assert(path in paths)
2930
assert("view_failure" not in dpath.util.search(dict, '/**', afilter=afilter)['a'])
@@ -34,23 +35,25 @@ def afilter(x):
3435
assert("view_failure" not in dpath.util.search(dict, ['**'], afilter=afilter)['a'])
3536
assert("d" not in dpath.util.search(dict, ['**'], afilter=afilter)['a']['b']['c'])
3637

37-
3838
def filter(x):
3939
sys.stderr.write(x)
40-
return x.get('type', None) is 'correct'
41-
42-
a = { 'actions' : [ { 'type': 'correct' }, { 'type': 'incorrect' } ] }
40+
return x.get('type', None) == 'correct'
41+
42+
a = {
43+
'actions': [
44+
{
45+
'type': 'correct'
46+
},
47+
{
48+
'type': 'incorrect'
49+
},
50+
],
51+
}
52+
4353
results = [[x[0], x[1]] for x in dpath.util.search(a, 'actions/*', yielded=True)]
4454
print(results)
4555
results = [[x[0], x[1]] for x in dpath.util.search(a, 'actions/*', afilter=filter, yielded=True)]
4656
print(filter)
4757
print(results)
4858
assert(len(results) == 1)
4959
assert(results[0]['type'] == 'correct')
50-
51-
# def test_broken_afilter_lambda():
52-
# a = { 'actions' : [ { 'type': 'correct' }, { 'type': 'incorrect' } ] }
53-
# results = [[x[0], x[1]] for x in dpath.util.search(a, 'actions/*', afilter=(lambda x: x.get('type', None) is 'correct'), yielded=True)]
54-
# print(results)
55-
# assert(len(results) == 1)
56-
# assert(results[0]['type'] == 'correct')

tests/test_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import nose
22
import dpath.util
33
from nose.tools import assert_raises
4+
45
try:
5-
#python3, especially 3.8
6+
# python3, especially 3.8
67
from collections.abc import MutableSequence
78
from collections.abc import MutableMapping
89
except ImportError:
9-
#python2
10+
# python2
1011
from collections import MutableSequence
1112
from collections import MutableMapping
1213

0 commit comments

Comments
 (0)