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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 13 additions & 13 deletions
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

0 commit comments

Comments
 (0)