Skip to content

Commit ff1fa4e

Browse files
mroeschkejreback
authored andcommitted
CLN/STYLE: Lint comprehensions (#22075)
1 parent 0c58a82 commit ff1fa4e

31 files changed

+88
-107
lines changed

ci/environment-dev.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- Cython>=0.28.2
77
- NumPy
88
- flake8
9+
- flake8-comprehensions
910
- moto
1011
- pytest>=3.1
1112
- python-dateutil>=2.5.0

ci/lint.sh

+6-19
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,42 @@ if [ "$LINT" ]; then
1010

1111
# pandas/_libs/src is C code, so no need to search there.
1212
echo "Linting *.py"
13-
flake8 pandas --filename=*.py --exclude pandas/_libs/src
13+
flake8 pandas --filename=*.py --exclude pandas/_libs/src --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
1414
if [ $? -ne "0" ]; then
1515
RET=1
1616
fi
1717
echo "Linting *.py DONE"
1818

1919
echo "Linting setup.py"
20-
flake8 setup.py
20+
flake8 setup.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
2121
if [ $? -ne "0" ]; then
2222
RET=1
2323
fi
2424
echo "Linting setup.py DONE"
2525

2626
echo "Linting asv_bench/benchmarks/"
27-
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/*.py --ignore=F811
27+
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/*.py --ignore=F811,C405,C406,C408,C409,C410
2828
if [ $? -ne "0" ]; then
2929
RET=1
3030
fi
3131
echo "Linting asv_bench/benchmarks/*.py DONE"
3232

3333
echo "Linting scripts/*.py"
34-
flake8 scripts --filename=*.py
34+
flake8 scripts --filename=*.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
3535
if [ $? -ne "0" ]; then
3636
RET=1
3737
fi
3838
echo "Linting scripts/*.py DONE"
3939

4040
echo "Linting doc scripts"
41-
flake8 doc/make.py doc/source/conf.py
41+
flake8 doc/make.py doc/source/conf.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
4242
if [ $? -ne "0" ]; then
4343
RET=1
4444
fi
4545
echo "Linting doc scripts DONE"
4646

4747
echo "Linting *.pyx"
48-
flake8 pandas --filename=*.pyx --select=E501,E302,E203,E111,E114,E221,E303,E128,E231,E126,E265,E305,E301,E127,E261,E271,E129,W291,E222,E241,E123,F403
48+
flake8 pandas --filename=*.pyx --select=E501,E302,E203,E111,E114,E221,E303,E128,E231,E126,E265,E305,E301,E127,E261,E271,E129,W291,E222,E241,E123,F403,C400,C401,C402,C403,C404,C407,C411
4949
if [ $? -ne "0" ]; then
5050
RET=1
5151
fi
@@ -131,19 +131,6 @@ if [ "$LINT" ]; then
131131
fi
132132
echo "Check for non-standard imports DONE"
133133

134-
echo "Check for use of lists instead of generators in built-in Python functions"
135-
136-
# Example: Avoid `any([i for i in some_iterator])` in favor of `any(i for i in some_iterator)`
137-
#
138-
# Check the following functions:
139-
# any(), all(), sum(), max(), min(), list(), dict(), set(), frozenset(), tuple(), str.join()
140-
grep -R --include="*.py*" -E "[^_](any|all|sum|max|min|list|dict|set|frozenset|tuple|join)\(\[.* for .* in .*\]\)" pandas
141-
142-
if [ $? = "0" ]; then
143-
RET=1
144-
fi
145-
echo "Check for use of lists instead of generators in built-in Python functions DONE"
146-
147134
echo "Check for incorrect sphinx directives"
148135
SPHINX_DIRECTIVES=$(echo \
149136
"autosummary|contents|currentmodule|deprecated|function|image|"\

ci/travis-27.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies:
99
- fastparquet
1010
- feather-format
1111
- flake8=3.4.1
12+
- flake8-comprehensions
1213
- gcsfs
1314
- html5lib
1415
- ipython

pandas/core/arrays/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ def _concat_same_type(cls, to_concat):
600600
-------
601601
IntervalArray
602602
"""
603-
closed = set(interval.closed for interval in to_concat)
603+
closed = {interval.closed for interval in to_concat}
604604
if len(closed) != 1:
605605
raise ValueError("Intervals must all be closed on the same side.")
606606
closed = closed.pop()

pandas/core/common.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ def dict_compat(d):
307307
dict
308308
309309
"""
310-
return dict((maybe_box_datetimelike(key), value)
311-
for key, value in iteritems(d))
310+
return {maybe_box_datetimelike(key): value for key, value in iteritems(d)}
312311

313312

314313
def standardize_mapping(into):

pandas/core/dtypes/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
is_named_tuple, is_array_like, is_decimal, is_complex, is_interval)
2222

2323

24-
_POSSIBLY_CAST_DTYPES = set([np.dtype(t).name
25-
for t in ['O', 'int8', 'uint8', 'int16', 'uint16',
26-
'int32', 'uint32', 'int64', 'uint64']])
24+
_POSSIBLY_CAST_DTYPES = {np.dtype(t).name
25+
for t in ['O', 'int8', 'uint8', 'int16', 'uint16',
26+
'int32', 'uint32', 'int64', 'uint64']}
2727

2828
_NS_DTYPE = conversion.NS_DTYPE
2929
_TD_DTYPE = conversion.TD_DTYPE

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8840,7 +8840,7 @@ def describe_1d(data):
88408840
ldesc = [describe_1d(s) for _, s in data.iteritems()]
88418841
# set a convenient order for rows
88428842
names = []
8843-
ldesc_indexes = sorted([x.index for x in ldesc], key=len)
8843+
ldesc_indexes = sorted((x.index for x in ldesc), key=len)
88448844
for idxnames in ldesc_indexes:
88458845
for name in idxnames:
88468846
if name not in names:

pandas/core/groupby/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def _gotitem(self, key, ndim, subset=None):
4343

4444
# we need to make a shallow copy of ourselves
4545
# with the same groupby
46-
kwargs = dict([(attr, getattr(self, attr))
47-
for attr in self._attributes])
46+
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
4847
self = self.__class__(subset,
4948
groupby=self._groupby[key],
5049
parent=self,

pandas/core/indexes/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def _get_consensus_names(indexes):
147147

148148
# find the non-none names, need to tupleify to make
149149
# the set hashable, then reverse on return
150-
consensus_names = set(tuple(i.names) for i in indexes
151-
if com._any_not_none(*i.names))
150+
consensus_names = {tuple(i.names) for i in indexes
151+
if com._any_not_none(*i.names)}
152152
if len(consensus_names) == 1:
153153
return list(list(consensus_names)[0])
154154
return [None] * indexes[0].nlevels

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ def f(k, stringify):
903903
if stringify and not isinstance(k, compat.string_types):
904904
k = str(k)
905905
return k
906-
key = tuple([f(k, stringify)
907-
for k, stringify in zip(key, self._have_mixed_levels)])
906+
key = tuple(f(k, stringify)
907+
for k, stringify in zip(key, self._have_mixed_levels))
908908
return hash_tuple(key)
909909

910910
@Appender(Index.duplicated.__doc__)

pandas/core/internals/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def is_uniform_reindex(join_units):
378378
return (
379379
# TODO: should this be ju.block._can_hold_na?
380380
all(ju.block and ju.block.is_extension for ju in join_units) and
381-
len(set(ju.block.dtype.name for ju in join_units)) == 1
381+
len({ju.block.dtype.name for ju in join_units}) == 1
382382
)
383383

384384

pandas/core/internals/managers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ def apply(self, f, axes=None, filter=None, do_integrity_check=False,
398398

399399
# TODO(EA): may interfere with ExtensionBlock.setitem for blocks
400400
# with a .values attribute.
401-
aligned_args = dict((k, kwargs[k])
402-
for k in align_keys
403-
if hasattr(kwargs[k], 'values') and
404-
not isinstance(kwargs[k], ABCExtensionArray))
401+
aligned_args = {k: kwargs[k]
402+
for k in align_keys
403+
if hasattr(kwargs[k], 'values') and
404+
not isinstance(kwargs[k], ABCExtensionArray)}
405405

406406
for b in self.blocks:
407407
if filter is not None:

pandas/core/panel.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1429,10 +1429,8 @@ def _extract_axes(self, data, axes, **kwargs):
14291429
@staticmethod
14301430
def _extract_axes_for_slice(self, axes):
14311431
""" return the slice dictionary for these axes """
1432-
return dict((self._AXIS_SLICEMAP[i], a)
1433-
for i, a in zip(
1434-
self._AXIS_ORDERS[self._AXIS_LEN - len(axes):],
1435-
axes))
1432+
return {self._AXIS_SLICEMAP[i]: a for i, a in
1433+
zip(self._AXIS_ORDERS[self._AXIS_LEN - len(axes):], axes)}
14361434

14371435
@staticmethod
14381436
def _prep_ndarray(self, values, copy=True):
@@ -1480,11 +1478,10 @@ def _homogenize_dict(self, frames, intersect=True, dtype=None):
14801478
adj_frames[k] = v
14811479

14821480
axes = self._AXIS_ORDERS[1:]
1483-
axes_dict = dict((a, ax) for a, ax in zip(axes, self._extract_axes(
1484-
self, adj_frames, axes, intersect=intersect)))
1481+
axes_dict = {a: ax for a, ax in zip(axes, self._extract_axes(
1482+
self, adj_frames, axes, intersect=intersect))}
14851483

1486-
reindex_dict = dict(
1487-
[(self._AXIS_SLICEMAP[a], axes_dict[a]) for a in axes])
1484+
reindex_dict = {self._AXIS_SLICEMAP[a]: axes_dict[a] for a in axes}
14881485
reindex_dict['copy'] = False
14891486
for key, frame in compat.iteritems(adj_frames):
14901487
if frame is not None:

pandas/io/json/normalize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ def _pull_field(js, spec):
194194
data = [data]
195195

196196
if record_path is None:
197-
if any([[isinstance(x, dict)
198-
for x in compat.itervalues(y)] for y in data]):
197+
if any([isinstance(x, dict)
198+
for x in compat.itervalues(y)] for y in data):
199199
# naive normalization, this is idempotent for flat records
200200
# and potentially will inflate the data considerably for
201201
# deeply nested structures:

pandas/io/parsers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3147,8 +3147,7 @@ def _clean_na_values(na_values, keep_default_na=True):
31473147
v = set(v) | _NA_VALUES
31483148

31493149
na_values[k] = v
3150-
na_fvalues = dict((k, _floatify_na_values(v))
3151-
for k, v in na_values.items())
3150+
na_fvalues = {k: _floatify_na_values(v) for k, v in na_values.items()}
31523151
else:
31533152
if not is_list_like(na_values):
31543153
na_values = [na_values]

pandas/tests/api/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def check(self, namespace, expected, ignored=None):
1515
# ignored ones
1616
# compare vs the expected
1717

18-
result = sorted([f for f in dir(namespace) if not f.startswith('_')])
18+
result = sorted(f for f in dir(namespace) if not f.startswith('_'))
1919
if ignored is not None:
2020
result = sorted(list(set(result) - set(ignored)))
2121

pandas/tests/extension/json/array.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def unique(self):
160160
# Parent method doesn't work since np.array will try to infer
161161
# a 2-dim object.
162162
return type(self)([
163-
dict(x) for x in list(set(tuple(d.items()) for d in self.data))
163+
dict(x) for x in list({tuple(d.items()) for d in self.data})
164164
])
165165

166166
@classmethod
@@ -176,5 +176,5 @@ def _values_for_argsort(self):
176176
# Disable NumPy's shape inference by including an empty tuple...
177177
# If all the elemnts of self are the same size P, NumPy will
178178
# cast them to an (N, P) array, instead of an (N,) array of tuples.
179-
frozen = [()] + list(tuple(x.items()) for x in self)
179+
frozen = [()] + [tuple(x.items()) for x in self]
180180
return np.array(frozen, dtype=object)[1:]

pandas/tests/frame/test_apply.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,14 @@ def test_apply_differently_indexed(self):
319319
df = DataFrame(np.random.randn(20, 10))
320320

321321
result0 = df.apply(Series.describe, axis=0)
322-
expected0 = DataFrame(dict((i, v.describe())
323-
for i, v in compat.iteritems(df)),
322+
expected0 = DataFrame({i: v.describe()
323+
for i, v in compat.iteritems(df)},
324324
columns=df.columns)
325325
assert_frame_equal(result0, expected0)
326326

327327
result1 = df.apply(Series.describe, axis=1)
328-
expected1 = DataFrame(dict((i, v.describe())
329-
for i, v in compat.iteritems(df.T)),
328+
expected1 = DataFrame({i: v.describe()
329+
for i, v in compat.iteritems(df.T)},
330330
columns=df.index).T
331331
assert_frame_equal(result1, expected1)
332332

pandas/tests/frame/test_dtypes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ def test_select_dtypes_typecodes(self):
397397
def test_dtypes_gh8722(self):
398398
self.mixed_frame['bool'] = self.mixed_frame['A'] > 0
399399
result = self.mixed_frame.dtypes
400-
expected = Series(dict((k, v.dtype)
401-
for k, v in compat.iteritems(self.mixed_frame)),
400+
expected = Series({k: v.dtype
401+
for k, v in compat.iteritems(self.mixed_frame)},
402402
index=result.index)
403403
assert_series_equal(result, expected)
404404

@@ -439,8 +439,8 @@ def test_astype(self):
439439

440440
# mixed casting
441441
def _check_cast(df, v):
442-
assert (list(set(s.dtype.name for
443-
_, s in compat.iteritems(df)))[0] == v)
442+
assert (list({s.dtype.name for
443+
_, s in compat.iteritems(df)})[0] == v)
444444

445445
mn = self.all_mixed._get_numeric_data().copy()
446446
mn['little_float'] = np.array(12345., dtype='float16')

pandas/tests/frame/test_indexing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def test_getitem_boolean(self):
276276

277277
data = df._get_numeric_data()
278278
bif = df[df > 0]
279-
bifw = DataFrame(dict((c, np.where(data[c] > 0, data[c], np.nan))
280-
for c in data.columns),
279+
bifw = DataFrame({c: np.where(data[c] > 0, data[c], np.nan)
280+
for c in data.columns},
281281
index=data.index, columns=data.columns)
282282

283283
# add back other columns to compare
@@ -2506,9 +2506,9 @@ def _check_get(df, cond, check_dtypes=True):
25062506
_check_get(df, cond)
25072507

25082508
# upcasting case (GH # 2794)
2509-
df = DataFrame(dict((c, Series([1] * 3, dtype=c))
2510-
for c in ['float32', 'float64',
2511-
'int32', 'int64']))
2509+
df = DataFrame({c: Series([1] * 3, dtype=c)
2510+
for c in ['float32', 'float64',
2511+
'int32', 'int64']})
25122512
df.iloc[1, :] = 0
25132513
result = df.where(df >= 0).get_dtype_counts()
25142514

pandas/tests/groupby/test_groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ def test_groupby_multiple_columns(df, op):
519519
for n1, gp1 in data.groupby('A'):
520520
for n2, gp2 in gp1.groupby('B'):
521521
expected[n1][n2] = op(gp2.loc[:, ['C', 'D']])
522-
expected = dict((k, DataFrame(v))
523-
for k, v in compat.iteritems(expected))
522+
expected = {k: DataFrame(v)
523+
for k, v in compat.iteritems(expected)}
524524
expected = Panel.fromDict(expected).swapaxes(0, 1)
525525
expected.major_axis.name, expected.minor_axis.name = 'A', 'B'
526526

pandas/tests/indexes/multi/test_copy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ def test_copy_method_kwargs(deep, kwarg, value):
8383
if kwarg == 'names':
8484
assert getattr(idx_copy, kwarg) == value
8585
else:
86-
assert list(list(i) for i in getattr(idx_copy, kwarg)) == value
86+
assert [list(i) for i in getattr(idx_copy, kwarg)] == value

0 commit comments

Comments
 (0)