Skip to content

Commit 21d90cc

Browse files
committed
CLN: Lint for lists instead of generators in built-in Python functions
1 parent cfad581 commit 21d90cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+145
-135
lines changed

ci/lint.sh

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ if [ "$LINT" ]; then
8484
fi
8585
echo "Check for invalid testing DONE"
8686

87+
echo "Check for use of lists in built-in Python functions"
88+
89+
# Example: Avoid `any([i for i in some_iterator])` in favor of `any(i for i in some_iterator)`
90+
#
91+
# Check the following functions:
92+
# any(), all(), sum(), max(), min(), list(), dict(), set(), frozenset(), tuple(), str.join()
93+
grep -R --include="*.py*" -E "[^_](any|all|sum|max|min|list|dict|set|frozenset|tuple|join)\(\[.* for .* in .*\]\)"
94+
95+
if [ $? = "0" ]; then
96+
RET=1
97+
fi
98+
echo "Check for use of lists in built-in Python functions DONE"
99+
87100
else
88101
echo "NOT Linting"
89102
fi

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ cdef class TextReader:
770770
msg = self.orig_header
771771
if isinstance(msg, list):
772772
msg = "[%s], len of %d," % (
773-
','.join([ str(m) for m in msg ]), len(msg))
773+
','.join(str(m) for m in msg), len(msg))
774774
raise ParserError(
775775
'Passed header=%s but only %d lines in file'
776776
% (msg, self.parser.lines))
@@ -2227,7 +2227,7 @@ def _concatenate_chunks(list chunks):
22272227
for name in names:
22282228
arrs = [chunk.pop(name) for chunk in chunks]
22292229
# Check each arr for consistent types.
2230-
dtypes = set([a.dtype for a in arrs])
2230+
dtypes = set(a.dtype for a in arrs)
22312231
if len(dtypes) > 1:
22322232
common_type = np.find_common_type(dtypes, [])
22332233
if common_type == np.object:

pandas/_libs/src/inference.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
13091309

13101310
# we try to coerce datetime w/tz but must all have the same tz
13111311
if seen.datetimetz_:
1312-
if len(set([getattr(val, 'tzinfo', None) for val in objects])) == 1:
1312+
if len(set(getattr(val, 'tzinfo', None) for val in objects)) == 1:
13131313
from pandas import DatetimeIndex
13141314
return DatetimeIndex(objects)
13151315
seen.object_ = 1

pandas/_libs/tslibs/resolution.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Resolution(object):
218218
'U': 'N',
219219
'N': None}
220220

221-
_str_reso_map = dict([(v, k) for k, v in _reso_str_map.items()])
221+
_str_reso_map = dict((v, k) for k, v in _reso_str_map.items())
222222

223223
_reso_freq_map = {
224224
'year': 'A',
@@ -232,8 +232,7 @@ class Resolution(object):
232232
'microsecond': 'U',
233233
'nanosecond': 'N'}
234234

235-
_freq_reso_map = dict([(v, k)
236-
for k, v in _reso_freq_map.items()])
235+
_freq_reso_map = dict((v, k) for k, v in _reso_freq_map.items())
237236

238237
@classmethod
239238
def get_str(cls, reso):

pandas/_libs/tslibs/strptime.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ class TimeRE(dict):
568568
break
569569
else:
570570
return ''
571-
regex = '|'.join([re.escape(stuff) for stuff in to_convert])
571+
regex = '|'.join(re.escape(stuff) for stuff in to_convert)
572572
regex = '(?P<%s>%s' % (directive, regex)
573573
return '%s)' % regex
574574

pandas/_version.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
141141
if verbose:
142142
print("keywords are unexpanded, not using")
143143
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
144-
refs = set([r.strip() for r in refnames.strip("()").split(",")])
144+
refs = set(r.strip() for r in refnames.strip("()").split(","))
145145
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
146146
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
147147
TAG = "tag: "
148-
tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
148+
tags = set(r[len(TAG):] for r in refs if r.startswith(TAG))
149149
if not tags:
150150
# Either we're using git < 1.8.3, or there really are no tags. We use
151151
# a heuristic: assume all version tags have a digit. The old git %d
@@ -154,7 +154,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
154154
# between branches and tags. By ignoring refnames without digits, we
155155
# filter out many common branch names like "release" and
156156
# "stabilization", as well as "HEAD" and "master".
157-
tags = set([r for r in refs if re.search(r'\d', r)])
157+
tags = set(r for r in refs if re.search(r'\d', r))
158158
if verbose:
159159
print("discarding '{}', no digits".format(",".join(refs - tags)))
160160
if verbose:

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def map_indices_py(arr):
347347
Returns a dictionary with (element, index) pairs for each element in the
348348
given array/list
349349
"""
350-
return dict([(x, i) for i, x in enumerate(arr)])
350+
return dict((x, i) for i, x in enumerate(arr))
351351

352352

353353
def union(*seqs):

pandas/core/dtypes/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def _concat_datetimetz(to_concat, name=None):
459459
it is used in DatetimeIndex.append also
460460
"""
461461
# do not pass tz to set because tzlocal cannot be hashed
462-
if len(set([str(x.dtype) for x in to_concat])) != 1:
462+
if len(set(str(x.dtype) for x in to_concat)) != 1:
463463
raise ValueError('to_concat must have the same tz')
464464
tz = to_concat[0].tz
465465
# no need to localize because internal repr will not be changed

pandas/core/frame.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -3895,7 +3895,7 @@ def f(col):
38953895
return self._constructor_sliced(r, index=new_index,
38963896
dtype=r.dtype)
38973897

3898-
result = dict([(col, f(col)) for col in this])
3898+
result = dict((col, f(col)) for col in this)
38993899

39003900
# non-unique
39013901
else:
@@ -3906,9 +3906,7 @@ def f(i):
39063906
return self._constructor_sliced(r, index=new_index,
39073907
dtype=r.dtype)
39083908

3909-
result = dict([
3910-
(i, f(i)) for i, col in enumerate(this.columns)
3911-
])
3909+
result = dict((i, f(i)) for i, col in enumerate(this.columns))
39123910
result = self._constructor(result, index=new_index, copy=False)
39133911
result.columns = new_columns
39143912
return result
@@ -3986,7 +3984,7 @@ def _compare_frame_evaluate(self, other, func, str_rep, try_cast=True):
39863984
if self.columns.is_unique:
39873985

39883986
def _compare(a, b):
3989-
return dict([(col, func(a[col], b[col])) for col in a.columns])
3987+
return dict((col, func(a[col], b[col])) for col in a.columns)
39903988

39913989
new_data = expressions.evaluate(_compare, str_rep, self, other)
39923990
return self._constructor(data=new_data, index=self.index,
@@ -3995,8 +3993,8 @@ def _compare(a, b):
39953993
else:
39963994

39973995
def _compare(a, b):
3998-
return dict([(i, func(a.iloc[:, i], b.iloc[:, i]))
3999-
for i, col in enumerate(a.columns)])
3996+
return dict((i, func(a.iloc[:, i], b.iloc[:, i]))
3997+
for i, col in enumerate(a.columns))
40003998

40013999
new_data = expressions.evaluate(_compare, str_rep, self, other)
40024000
result = self._constructor(data=new_data, index=self.index,

pandas/core/generic.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,21 @@ def set_axis(a, i):
279279

280280
def _construct_axes_dict(self, axes=None, **kwargs):
281281
"""Return an axes dictionary for myself."""
282-
d = dict([(a, self._get_axis(a)) for a in (axes or self._AXIS_ORDERS)])
282+
d = dict((a, self._get_axis(a)) for a in (axes or self._AXIS_ORDERS))
283283
d.update(kwargs)
284284
return d
285285

286286
@staticmethod
287287
def _construct_axes_dict_from(self, axes, **kwargs):
288288
"""Return an axes dictionary for the passed axes."""
289-
d = dict([(a, ax) for a, ax in zip(self._AXIS_ORDERS, axes)])
289+
d = dict((a, ax) for a, ax in zip(self._AXIS_ORDERS, axes))
290290
d.update(kwargs)
291291
return d
292292

293293
def _construct_axes_dict_for_slice(self, axes=None, **kwargs):
294294
"""Return an axes dictionary for myself."""
295-
d = dict([(self._AXIS_SLICEMAP[a], self._get_axis(a))
296-
for a in (axes or self._AXIS_ORDERS)])
295+
d = dict((self._AXIS_SLICEMAP[a], self._get_axis(a))
296+
for a in (axes or self._AXIS_ORDERS))
297297
d.update(kwargs)
298298
return d
299299

@@ -329,7 +329,7 @@ def _construct_axes_from_arguments(self, args, kwargs, require_all=False):
329329
raise TypeError("not enough/duplicate arguments "
330330
"specified!")
331331

332-
axes = dict([(a, kwargs.pop(a, None)) for a in self._AXIS_ORDERS])
332+
axes = dict((a, kwargs.pop(a, None)) for a in self._AXIS_ORDERS)
333333
return axes, kwargs
334334

335335
@classmethod
@@ -586,10 +586,10 @@ def transpose(self, *args, **kwargs):
586586
# construct the args
587587
axes, kwargs = self._construct_axes_from_arguments(args, kwargs,
588588
require_all=True)
589-
axes_names = tuple([self._get_axis_name(axes[a])
590-
for a in self._AXIS_ORDERS])
591-
axes_numbers = tuple([self._get_axis_number(axes[a])
592-
for a in self._AXIS_ORDERS])
589+
axes_names = tuple(self._get_axis_name(axes[a])
590+
for a in self._AXIS_ORDERS)
591+
axes_numbers = tuple(self._get_axis_number(axes[a])
592+
for a in self._AXIS_ORDERS)
593593

594594
# we must have unique axes
595595
if len(axes) != len(set(axes)):
@@ -699,8 +699,8 @@ def squeeze(self, axis=None):
699699
(self._get_axis_number(axis),))
700700
try:
701701
return self.iloc[
702-
tuple([0 if i in axis and len(a) == 1 else slice(None)
703-
for i, a in enumerate(self.axes)])]
702+
tuple(0 if i in axis and len(a) == 1 else slice(None)
703+
for i, a in enumerate(self.axes))]
704704
except Exception:
705705
return self
706706

@@ -4277,8 +4277,8 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
42774277
elif self.ndim == 3:
42784278

42794279
# fill in 2d chunks
4280-
result = dict([(col, s.fillna(method=method, value=value))
4281-
for col, s in self.iteritems()])
4280+
result = dict((col, s.fillna(method=method, value=value))
4281+
for col, s in self.iteritems())
42824282
new_obj = self._constructor.\
42834283
from_dict(result).__finalize__(self)
42844284
new_data = new_obj._data

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def get_converter(s):
471471
raise ValueError(msg)
472472

473473
converters = [get_converter(s) for s in index_sample]
474-
names = [tuple([f(n) for f, n in zip(converters, name)])
474+
names = [tuple(f(n) for f, n in zip(converters, name))
475475
for name in names]
476476

477477
else:

pandas/core/indexes/api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def conv(i):
101101

102102

103103
def _sanitize_and_check(indexes):
104-
kinds = list(set([type(index) for index in indexes]))
104+
kinds = list(set(type(index) for index in indexes))
105105

106106
if list in kinds:
107107
if len(kinds) > 1:
@@ -122,8 +122,8 @@ def _get_consensus_names(indexes):
122122

123123
# find the non-none names, need to tupleify to make
124124
# the set hashable, then reverse on return
125-
consensus_names = set([tuple(i.names) for i in indexes
126-
if com._any_not_none(*i.names)])
125+
consensus_names = set(tuple(i.names) for i in indexes
126+
if com._any_not_none(*i.names))
127127
if len(consensus_names) == 1:
128128
return list(list(consensus_names)[0])
129129
return [None] * indexes[0].nlevels

pandas/core/indexes/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ def _coerce_to_ndarray(cls, data):
732732

733733
def _get_attributes_dict(self):
734734
""" return an attributes dict for my class """
735-
return dict([(k, getattr(self, k, None)) for k in self._attributes])
735+
return dict((k, getattr(self, k, None)) for k in self._attributes)
736736

737737
def view(self, cls=None):
738738

@@ -838,7 +838,7 @@ def __unicode__(self):
838838
space = self._format_space()
839839

840840
prepr = (u(",%s") %
841-
space).join([u("%s=%s") % (k, v) for k, v in attrs])
841+
space).join(u("%s=%s") % (k, v) for k, v in attrs)
842842

843843
# no data provided, just attributes
844844
if data is None:
@@ -1781,7 +1781,7 @@ def append(self, other):
17811781
if not isinstance(obj, Index):
17821782
raise TypeError('all inputs must be Index')
17831783

1784-
names = set([obj.name for obj in to_concat])
1784+
names = set(obj.name for obj in to_concat)
17851785
name = None if len(names) > 1 else self.name
17861786

17871787
return self._concat(to_concat, name)

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ def _concat_same_dtype(self, to_concat, name):
10021002
assert that we all have the same .closed
10031003
we allow a 0-len index here as well
10041004
"""
1005-
if not len(set([i.closed for i in to_concat if len(i)])) == 1:
1005+
if not len(set(i.closed for i in to_concat if len(i))) == 1:
10061006
msg = ('can only append two IntervalIndex objects '
10071007
'that are closed on the same side')
10081008
raise ValueError(msg)

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ def get_indexer(_i, _idx):
20552055
return (axes[_i].get_loc(_idx['key']) if isinstance(_idx, dict) else
20562056
_idx)
20572057

2058-
return tuple([get_indexer(_i, _idx) for _i, _idx in enumerate(indexer)])
2058+
return tuple(get_indexer(_i, _idx) for _i, _idx in enumerate(indexer))
20592059

20602060

20612061
def maybe_convert_indices(indices, n):

pandas/core/internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def __unicode__(self):
240240

241241
else:
242242

243-
shape = ' x '.join([pprint_thing(s) for s in self.shape])
243+
shape = ' x '.join(pprint_thing(s) for s in self.shape)
244244
result = '%s: %s, %s, dtype: %s' % (name, pprint_thing(
245245
self.mgr_locs.indexer), shape, self.dtype)
246246

@@ -3365,7 +3365,7 @@ def reduction(self, f, axis=0, consolidate=True, transposed=False,
33653365
blocks.append(block)
33663366

33673367
# note that some DatetimeTZ, Categorical are always ndim==1
3368-
ndim = set([b.ndim for b in blocks])
3368+
ndim = set(b.ndim for b in blocks)
33693369

33703370
if 2 in ndim:
33713371

@@ -4858,7 +4858,7 @@ def _merge_blocks(blocks, dtype=None, _can_consolidate=True):
48584858
if _can_consolidate:
48594859

48604860
if dtype is None:
4861-
if len(set([b.dtype for b in blocks])) != 1:
4861+
if len(set(b.dtype for b in blocks)) != 1:
48624862
raise AssertionError("_merge_blocks are invalid!")
48634863
dtype = blocks[0].dtype
48644864

pandas/core/panel.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ def _apply_1d(self, func, axis):
10801080
for i in range(np.prod(shape)):
10811081

10821082
# construct the object
1083-
pts = tuple([p[i] for p in points])
1083+
pts = tuple(p[i] for p in points)
10841084
indexer.put(indlist, slice_indexer)
10851085

10861086
obj = Series(values[tuple(indexer)], index=slice_axis, name=pts)
@@ -1417,10 +1417,10 @@ def _extract_axes(self, data, axes, **kwargs):
14171417
@staticmethod
14181418
def _extract_axes_for_slice(self, axes):
14191419
""" return the slice dictionary for these axes """
1420-
return dict([(self._AXIS_SLICEMAP[i], a)
1421-
for i, a in zip(
1422-
self._AXIS_ORDERS[self._AXIS_LEN - len(axes):],
1423-
axes)])
1420+
return dict((self._AXIS_SLICEMAP[i], a)
1421+
for i, a in zip(
1422+
self._AXIS_ORDERS[self._AXIS_LEN - len(axes):],
1423+
axes))
14241424

14251425
@staticmethod
14261426
def _prep_ndarray(self, values, copy=True):
@@ -1468,8 +1468,8 @@ def _homogenize_dict(self, frames, intersect=True, dtype=None):
14681468
adj_frames[k] = v
14691469

14701470
axes = self._AXIS_ORDERS[1:]
1471-
axes_dict = dict([(a, ax) for a, ax in zip(axes, self._extract_axes(
1472-
self, adj_frames, axes, intersect=intersect))])
1471+
axes_dict = dict((a, ax) for a, ax in zip(axes, self._extract_axes(
1472+
self, adj_frames, axes, intersect=intersect)))
14731473

14741474
reindex_dict = dict(
14751475
[(self._AXIS_SLICEMAP[a], axes_dict[a]) for a in axes])

pandas/core/panelnd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _combine_with_constructor(self, other, func):
105105
new_axes.append(getattr(self, a).union(getattr(other, a)))
106106

107107
# reindex: could check that everything's the same size, but forget it
108-
d = dict([(a, ax) for a, ax in zip(self._AXIS_ORDERS, new_axes)])
108+
d = dict((a, ax) for a, ax in zip(self._AXIS_ORDERS, new_axes))
109109
d['copy'] = False
110110
this = self.reindex(**d)
111111
other = other.reindex(**d)

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None):
568568
names = list(names)
569569
else:
570570
# make sure that all of the passed indices have the same nlevels
571-
if not len(set([idx.nlevels for idx in indexes])) == 1:
571+
if not len(set(idx.nlevels for idx in indexes)) == 1:
572572
raise AssertionError("Cannot concat indices that do"
573573
" not have the same number of levels")
574574

pandas/core/sparse/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _init_matrix(self, data, index, columns, dtype=None):
173173
""" Init self from ndarray or list of lists """
174174
data = _prep_ndarray(data, copy=False)
175175
index, columns = self._prep_index(data, index, columns)
176-
data = dict([(idx, data[:, i]) for i, idx in enumerate(columns)])
176+
data = dict((idx, data[:, i]) for i, idx in enumerate(columns))
177177
return self._init_dict(data, index, columns, dtype)
178178

179179
def _init_spmatrix(self, data, index, columns, dtype=None,

pandas/io/clipboards.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def read_clipboard(sep='\s+', **kwargs): # pragma: no cover
5353
# 0 1 2
5454
# 1 3 4
5555

56-
counts = set([x.lstrip().count('\t') for x in lines])
56+
counts = set(x.lstrip().count('\t') for x in lines)
5757
if len(lines) > 1 and len(counts) == 1 and counts.pop() != 0:
5858
sep = '\t'
5959

0 commit comments

Comments
 (0)