Skip to content

Commit 486e626

Browse files
mroeschkejbrockmendel
authored andcommitted
STYLE/LINT: Set literals (#22202)
* Style: Use set literals: * flake8: * Fix empty sets * Undo spaces
1 parent 776fed3 commit 486e626

38 files changed

+75
-80
lines changed

ci/lint.sh

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,42 @@ if [ "$LINT" ]; then
1313
#E731, # do not assign a lambda expression, use a def
1414
#E741, # do not use variables named 'l', 'O', or 'I'
1515
#W503, # line break before binary operator
16-
#C405, # Unnecessary (list/tuple) literal - rewrite as a set literal.
1716
#C406, # Unnecessary (list/tuple) literal - rewrite as a dict literal.
1817
#C408, # Unnecessary (dict/list/tuple) call - rewrite as a literal.
1918
#C409, # Unnecessary (list/tuple) passed to tuple() - (remove the outer call to tuple()/rewrite as a tuple literal).
2019
#C410 # Unnecessary (list/tuple) passed to list() - (remove the outer call to list()/rewrite as a list literal).
2120

2221
# pandas/_libs/src is C code, so no need to search there.
2322
echo "Linting *.py"
24-
flake8 pandas --filename=*.py --exclude pandas/_libs/src --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
23+
flake8 pandas --filename=*.py --exclude pandas/_libs/src --ignore=C406,C408,C409,C410,E402,E731,E741,W503
2524
if [ $? -ne "0" ]; then
2625
RET=1
2726
fi
2827
echo "Linting *.py DONE"
2928

3029
echo "Linting setup.py"
31-
flake8 setup.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
30+
flake8 setup.py --ignore=C406,C408,C409,C410,E402,E731,E741,W503
3231
if [ $? -ne "0" ]; then
3332
RET=1
3433
fi
3534
echo "Linting setup.py DONE"
3635

3736
echo "Linting asv_bench/benchmarks/"
38-
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/*.py --ignore=F811,C405,C406,C408,C409,C410
37+
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/*.py --ignore=F811,C406,C408,C409,C410
3938
if [ $? -ne "0" ]; then
4039
RET=1
4140
fi
4241
echo "Linting asv_bench/benchmarks/*.py DONE"
4342

4443
echo "Linting scripts/*.py"
45-
flake8 scripts --filename=*.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
44+
flake8 scripts --filename=*.py --ignore=C406,C408,C409,C410,E402,E731,E741,W503
4645
if [ $? -ne "0" ]; then
4746
RET=1
4847
fi
4948
echo "Linting scripts/*.py DONE"
5049

5150
echo "Linting doc scripts"
52-
flake8 doc/make.py doc/source/conf.py --ignore=C405,C406,C408,C409,C410,E402,E731,E741,W503
51+
flake8 doc/make.py doc/source/conf.py --ignore=C406,C408,C409,C410,E402,E731,E741,W503
5352
if [ $? -ne "0" ]; then
5453
RET=1
5554
fi

pandas/_libs/ops.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ def maybe_convert_bool(ndarray[object] arr,
260260
result = np.empty(n, dtype=np.uint8)
261261

262262
# the defaults
263-
true_vals = set(('True', 'TRUE', 'true'))
264-
false_vals = set(('False', 'FALSE', 'false'))
263+
true_vals = {'True', 'TRUE', 'true'}
264+
false_vals = {'False', 'FALSE', 'false'}
265265

266266
if true_values is not None:
267267
true_vals = true_vals | set(true_values)

pandas/_libs/tslibs/frequencies.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ _lite_rule_alias = {
124124
'us': 'U',
125125
'ns': 'N'}
126126

127-
_dont_uppercase = set(('MS', 'ms'))
127+
_dont_uppercase = {'MS', 'ms'}
128128

129129
# ----------------------------------------------------------------------
130130

pandas/_libs/tslibs/nattype.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ from util cimport (get_nat,
2323

2424
# ----------------------------------------------------------------------
2525
# Constants
26-
nat_strings = set(['NaT', 'nat', 'NAT', 'nan', 'NaN', 'NAN'])
26+
nat_strings = {'NaT', 'nat', 'NAT', 'nan', 'NaN', 'NAN'}
2727

2828
cdef int64_t NPY_NAT = get_nat()
2929
iNaT = NPY_NAT # python-visible constant

pandas/_libs/tslibs/offsets.pyx

+4-6
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,10 @@ def _validate_business_time(t_input):
252252
# ---------------------------------------------------------------------
253253
# Constructor Helpers
254254

255-
relativedelta_kwds = set([
256-
'years', 'months', 'weeks', 'days',
257-
'year', 'month', 'day', 'weekday',
258-
'hour', 'minute', 'second', 'microsecond',
259-
'nanosecond', 'nanoseconds',
260-
'hours', 'minutes', 'seconds', 'microseconds'])
255+
relativedelta_kwds = {'years', 'months', 'weeks', 'days', 'year', 'month',
256+
'day', 'weekday', 'hour', 'minute', 'second',
257+
'microsecond', 'nanosecond', 'nanoseconds', 'hours',
258+
'minutes', 'seconds', 'microseconds'}
261259

262260

263261
def _determine_offset(kwds):

pandas/_libs/tslibs/period.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,6 @@ def _validate_end_alias(how):
19731973
'START': 'S', 'FINISH': 'E',
19741974
'BEGIN': 'S', 'END': 'E'}
19751975
how = how_dict.get(str(how).upper())
1976-
if how not in set(['S', 'E']):
1976+
if how not in {'S', 'E'}:
19771977
raise ValueError('How must be one of S or E')
19781978
return how

pandas/core/arrays/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from . import ExtensionArray, Categorical
2727

28-
_VALID_CLOSED = set(['left', 'right', 'both', 'neither'])
28+
_VALID_CLOSED = {'left', 'right', 'both', 'neither'}
2929
_interval_shared_docs = {}
3030
_shared_docs_kwargs = dict(
3131
klass='IntervalArray',

pandas/core/computation/expressions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
# the set of dtypes that we will allow pass to numexpr
2626
_ALLOWED_DTYPES = {
27-
'evaluate': set(['int64', 'int32', 'float64', 'float32', 'bool']),
28-
'where': set(['int64', 'float64', 'bool'])
27+
'evaluate': {'int64', 'int32', 'float64', 'float32', 'bool'},
28+
'where': {'int64', 'float64', 'bool'}
2929
}
3030

3131
# the minimum prod shape that we will use numexpr
@@ -81,7 +81,7 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):
8181
return False
8282
dtypes |= set(s.index)
8383
elif isinstance(o, np.ndarray):
84-
dtypes |= set([o.dtype.name])
84+
dtypes |= {o.dtype.name}
8585

8686
# allowed are a superset
8787
if not len(dtypes) or _ALLOWED_DTYPES[dtype_check] >= dtypes:

pandas/core/dtypes/concat.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def is_nonempty(x):
188188
typs = get_dtype_kinds(to_concat)
189189
if len(typs) != 1:
190190

191-
if (not len(typs - set(['i', 'u', 'f'])) or
192-
not len(typs - set(['bool', 'i', 'u']))):
191+
if (not len(typs - {'i', 'u', 'f'}) or
192+
not len(typs - {'bool', 'i', 'u'})):
193193
# let numpy coerce
194194
pass
195195
else:
@@ -600,7 +600,7 @@ def convert_sparse(x, axis):
600600
to_concat = [convert_sparse(x, axis) for x in to_concat]
601601
result = np.concatenate(to_concat, axis=axis)
602602

603-
if not len(typs - set(['sparse', 'f', 'i'])):
603+
if not len(typs - {'sparse', 'f', 'i'}):
604604
# sparsify if inputs are sparse and dense numerics
605605
# first sparse input's fill_value and SparseIndex is used
606606
result = SparseArray(result.ravel(), fill_value=fill_values[0],

pandas/core/groupby/grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
481481
if key.key is None:
482482
return grouper, [], obj
483483
else:
484-
return grouper, set([key.key]), obj
484+
return grouper, {key.key}, obj
485485

486486
# already have a BaseGrouper, just return it
487487
elif isinstance(key, BaseGrouper):

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class Index(IndexOpsMixin, PandasObject):
238238

239239
_engine_type = libindex.ObjectEngine
240240

241-
_accessors = set(['str'])
241+
_accessors = {'str'}
242242

243243
str = CachedAccessor("str", StringMethods)
244244

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from pandas.core.arrays.interval import (IntervalArray,
4545
_interval_shared_docs)
4646

47-
_VALID_CLOSED = set(['left', 'right', 'both', 'neither'])
47+
_VALID_CLOSED = {'left', 'right', 'both', 'neither'}
4848
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
4949
_index_doc_kwargs.update(
5050
dict(klass='IntervalIndex',

pandas/core/panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def dropna(self, axis=0, how='any', inplace=False):
716716
values = self.values
717717
mask = notna(values)
718718

719-
for ax in reversed(sorted(set(range(self._AXIS_LEN)) - set([axis]))):
719+
for ax in reversed(sorted(set(range(self._AXIS_LEN)) - {axis})):
720720
mask = mask.sum(ax)
721721

722722
per_slice = np.prod(values.shape[:axis] + values.shape[axis + 1:])

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
11991199

12001200
freq = to_offset(freq)
12011201

1202-
end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])
1202+
end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
12031203
rule = freq.rule_code
12041204
if (rule in end_types or
12051205
('-' in rule and rule[:rule.find('-')] in end_types)):

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
163163
Copy input data
164164
"""
165165
_metadata = ['name']
166-
_accessors = set(['dt', 'cat', 'str'])
166+
_accessors = {'dt', 'cat', 'str'}
167167
_deprecations = generic.NDFrame._deprecations | frozenset(
168168
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
169169
'from_csv', 'valid'])

pandas/core/strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ def str_get_dummies(arr, sep='|'):
10821082
tags = set()
10831083
for ts in arr.str.split(sep):
10841084
tags.update(ts)
1085-
tags = sorted(tags - set([""]))
1085+
tags = sorted(tags - {""})
10861086

10871087
dummies = np.empty((len(arr), len(tags)), dtype=np.int64)
10881088

pandas/io/common.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
# common NA values
2424
# no longer excluding inf representations
2525
# '1.#INF','-1.#INF', '1.#INF000000',
26-
_NA_VALUES = set([
27-
'-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A',
28-
'N/A', 'n/a', 'NA', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', ''
29-
])
26+
_NA_VALUES = {'-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A',
27+
'N/A', 'n/a', 'NA', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan',
28+
'-nan', ''}
3029

3130

3231
if compat.PY3:

pandas/io/formats/csvs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _save_header(self):
268268
# Write out the index line if it's not empty.
269269
# Otherwise, we will print out an extraneous
270270
# blank line between the mi and the data rows.
271-
if encoded_labels and set(encoded_labels) != set(['']):
271+
if encoded_labels and set(encoded_labels) != {''}:
272272
encoded_labels.extend([''] * len(columns))
273273
writer.writerow(encoded_labels)
274274

pandas/plotting/_misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def f(t):
338338
classes = frame[class_column].drop_duplicates()
339339
df = frame.drop(class_column, axis=1)
340340
t = np.linspace(-pi, pi, samples)
341-
used_legends = set([])
341+
used_legends = set()
342342

343343
color_values = _get_standard_colors(num_colors=len(classes),
344344
colormap=colormap, color_type='random',
@@ -518,7 +518,7 @@ def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None,
518518
else:
519519
df = frame[cols]
520520

521-
used_legends = set([])
521+
used_legends = set()
522522

523523
ncols = len(df.columns)
524524

pandas/tests/dtypes/test_inference.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __getitem__(self):
6666
"ll",
6767
[
6868
[], [1], (1, ), (1, 2), {'a': 1},
69-
set([1, 'a']), Series([1]),
69+
{1, 'a'}, Series([1]),
7070
Series([]), Series(['a']).str,
7171
np.array([2])])
7272
def test_is_list_like_passes(ll):
@@ -97,7 +97,7 @@ class DtypeList(list):
9797

9898

9999
@pytest.mark.parametrize('inner', [
100-
[], [1], (1, ), (1, 2), {'a': 1}, set([1, 'a']), Series([1]),
100+
[], [1], (1, ), (1, 2), {'a': 1}, {1, 'a'}, Series([1]),
101101
Series([]), Series(['a']).str, (x for x in range(5))
102102
])
103103
@pytest.mark.parametrize('outer', [
@@ -293,7 +293,7 @@ def test_maybe_convert_numeric_infinities(self):
293293
# see gh-13274
294294
infinities = ['inf', 'inF', 'iNf', 'Inf',
295295
'iNF', 'InF', 'INf', 'INF']
296-
na_values = set(['', 'NULL', 'nan'])
296+
na_values = {'', 'NULL', 'nan'}
297297

298298
pos = np.array(['inf'], dtype=np.float64)
299299
neg = np.array(['-inf'], dtype=np.float64)
@@ -332,7 +332,7 @@ def test_maybe_convert_numeric_post_floatify_nan(self, coerce):
332332
# see gh-13314
333333
data = np.array(['1.200', '-999.000', '4.500'], dtype=object)
334334
expected = np.array([1.2, np.nan, 4.5], dtype=np.float64)
335-
nan_values = set([-999, -999.0])
335+
nan_values = {-999, -999.0}
336336

337337
out = lib.maybe_convert_numeric(data, nan_values, coerce)
338338
tm.assert_numpy_array_equal(out, expected)
@@ -385,7 +385,7 @@ def test_convert_numeric_uint64_nan(self, coerce, arr):
385385

386386
def test_convert_numeric_uint64_nan_values(self, coerce):
387387
arr = np.array([2**63, 2**63 + 1], dtype=object)
388-
na_values = set([2**63])
388+
na_values = {2**63}
389389

390390
expected = (np.array([np.nan, 2**63 + 1], dtype=float)
391391
if coerce else arr.copy())

pandas/tests/frame/test_rank.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def _check2d(df, expected, method='average', axis=0):
274274
result = df.rank(method=method, axis=axis)
275275
assert_frame_equal(result, exp_df)
276276

277-
disabled = set([(object, 'first')])
277+
disabled = {(object, 'first')}
278278
if (dtype, method) in disabled:
279279
return
280280
frame = df if dtype is None else df.astype(dtype)

pandas/tests/indexing/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def _axify(obj, key, axis):
2828
class Base(object):
2929
""" indexing comprehensive base class """
3030

31-
_objs = set(['series', 'frame', 'panel'])
32-
_typs = set(['ints', 'uints', 'labels', 'mixed',
33-
'ts', 'floats', 'empty', 'ts_rev', 'multi'])
31+
_objs = {'series', 'frame', 'panel'}
32+
_typs = {'ints', 'uints', 'labels', 'mixed', 'ts', 'floats', 'empty',
33+
'ts_rev', 'multi'}
3434

3535
def setup_method(self, method):
3636

pandas/tests/io/formats/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_repr_obeys_max_seq_limit(self):
244244
assert len(printing.pprint_thing(lrange(1000))) < 100
245245

246246
def test_repr_set(self):
247-
assert printing.pprint_thing(set([1])) == '{1}'
247+
assert printing.pprint_thing({1}) == '{1}'
248248

249249
def test_repr_is_valid_construction_code(self):
250250
# for the case of Index, where the repr is traditional rather then

pandas/tests/io/parser/na_values.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def test_non_string_na_values(self):
6969
tm.assert_frame_equal(out, expected)
7070

7171
def test_default_na_values(self):
72-
_NA_VALUES = set(['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN',
73-
'#N/A', 'N/A', 'n/a', 'NA', '#NA', 'NULL', 'null',
74-
'NaN', 'nan', '-NaN', '-nan', '#N/A N/A', ''])
72+
_NA_VALUES = {'-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A',
73+
'N/A', 'n/a', 'NA', '#NA', 'NULL', 'null', 'NaN', 'nan',
74+
'-NaN', '-nan', '#N/A N/A', ''}
7575
assert _NA_VALUES == com._NA_VALUES
7676
nv = len(_NA_VALUES)
7777

pandas/tests/io/parser/parse_dates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def test_read_with_parse_dates_invalid_type(self):
455455
self.read_csv, StringIO(data),
456456
parse_dates=np.array([4, 5]))
457457
tm.assert_raises_regex(TypeError, errmsg, self.read_csv,
458-
StringIO(data), parse_dates=set([1, 3, 3]))
458+
StringIO(data), parse_dates={1, 3, 3})
459459

460460
def test_parse_dates_empty_string(self):
461461
# see gh-2263

pandas/tests/io/parser/usecols.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def test_empty_usecols(self):
413413
# should not raise
414414
data = 'a,b,c\n1,2,3\n4,5,6'
415415
expected = DataFrame()
416-
result = self.read_csv(StringIO(data), usecols=set([]))
416+
result = self.read_csv(StringIO(data), usecols=set())
417417
tm.assert_frame_equal(result, expected)
418418

419419
def test_np_array_usecols(self):

pandas/tests/io/test_html.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def test_skiprows_list(self):
170170
assert_framelist_equal(df1, df2)
171171

172172
def test_skiprows_set(self):
173-
df1 = self.read_html(self.spam_data, '.*Water.*', skiprows=set([1, 2]))
174-
df2 = self.read_html(self.spam_data, 'Unit', skiprows=set([2, 1]))
173+
df1 = self.read_html(self.spam_data, '.*Water.*', skiprows={1, 2})
174+
df2 = self.read_html(self.spam_data, 'Unit', skiprows={2, 1})
175175

176176
assert_framelist_equal(df1, df2)
177177

pandas/tests/io/test_pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def test_keys(self):
369369
store['d'] = tm.makePanel()
370370
store['foo/bar'] = tm.makePanel()
371371
assert len(store) == 5
372-
expected = set(['/a', '/b', '/c', '/d', '/foo/bar'])
372+
expected = {'/a', '/b', '/c', '/d', '/foo/bar'}
373373
assert set(store.keys()) == expected
374374
assert set(store) == expected
375375

0 commit comments

Comments
 (0)