Skip to content

STYLE: fix pylint unnecessary-comprehension warnings #49674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions asv_bench/benchmarks/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def setup(self, dtype):
values = np.take(list(string.ascii_letters), indices)
values = [pd.Categorical(v) for v in values.T]

self.df = DataFrame(
{i: cat for i, cat in enumerate(values)}, index, columns
)
self.df = DataFrame(dict(enumerate(values)), index, columns)

self.df2 = self.df.iloc[:-1]

Expand Down
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@


html_context = {
"redirects": {old: new for old, new in moved_api_pages},
"redirects": dict(moved_api_pages),
"header": header,
}

Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_authors(revision_range):
pre.discard("Homu")

# Append '+' to new authors.
authors = [s + " +" for s in cur - pre] + [s for s in cur & pre]
authors = [s + " +" for s in cur - pre] + list(cur & pre)
authors.sort()
return authors

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class BaseExprVisitor(ast.NodeVisitor):

unary_ops = UNARY_OPS_SYMS
unary_op_nodes = "UAdd", "USub", "Invert", "Not"
unary_op_nodes_map = {k: v for k, v in zip(unary_ops, unary_op_nodes)}
unary_op_nodes_map = dict(zip(unary_ops, unary_op_nodes))

rewrite_map = {
ast.Eq: ast.In,
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _parse_nodes(self, elems: list[Any]) -> list[dict[str, str | None]]:
dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts]

if self.names:
dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts]
dicts = [dict(zip(self.names, d.values())) for d in dicts]

return dicts

Expand Down Expand Up @@ -380,7 +380,7 @@ def _iterparse_nodes(self, iterparse: Callable) -> list[dict[str, str | None]]:
dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts]

if self.names:
dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts]
dicts = [dict(zip(self.names, d.values())) for d in dicts]

return dicts

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_from_records_dictlike(self):
for b in blocks.values():
columns.extend(b.columns)

asdict = {x: y for x, y in df.items()}
asdict = dict(df.items())
asdict2 = {x: y.values for x, y in df.items()}

# dict of series & dict of ndarrays (have dtype info)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_constructor_mixed_dtypes(self, typ, ad):

for d, a in zip(dtypes, arrays):
assert a.dtype == d
ad.update({d: a for d, a in zip(dtypes, arrays)})
ad.update(dict(zip(dtypes, arrays)))
df = DataFrame(ad)

dtypes = MIXED_FLOAT_DTYPES + MIXED_INT_DTYPES
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def test_apply_index_has_complex_internals(index):
(lambda x: set(x.index.to_list()), [{0, 1}, {2, 3}]),
(lambda x: tuple(x.index.to_list()), [(0, 1), (2, 3)]),
(
lambda x: {n: i for (n, i) in enumerate(x.index.to_list())},
lambda x: dict(enumerate(x.index.to_list())),
[{0: 0, 1: 1}, {0: 2, 1: 3}],
),
(
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@ def test_multi_iter_frame(self, three_group):
df["k1"] = np.array(["b", "b", "b", "a", "a", "a"])
df["k2"] = np.array(["1", "1", "1", "2", "2", "2"])
grouped = df.groupby(["k1", "k2"])
# calling `dict` on a DataFrameGroupBy leads to a TypeError,
# we need to use a dictionary comprehension here
# pylint: disable-next=unnecessary-comprehension
groups = {key: gp for key, gp in grouped}
assert len(groups) == 2

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_map_with_dict_or_series(self):
# Order of categories in result can be different
tm.assert_index_equal(result, expected)

mapper = {o: n for o, n in zip(orig_values[:-1], new_values[:-1])}
mapper = dict(zip(orig_values[:-1], new_values[:-1]))
result = cur_index.map(mapper)
# Order of categories in result can be different
tm.assert_index_equal(result, expected)
2 changes: 1 addition & 1 deletion pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def create_and_load_iris(conn, iris_file: Path, dialect: str):
with iris_file.open(newline=None) as csvfile:
reader = csv.reader(csvfile)
header = next(reader)
params = [{key: value for key, value in zip(header, row)} for row in reader]
params = [dict(zip(header, row)) for row in reader]
stmt = insert(iris).values(params)
if isinstance(conn, Engine):
with conn.connect() as conn:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def test_replace_different_int_types(self, any_int_numpy_dtype):
labs = pd.Series([1, 1, 1, 0, 0, 2, 2, 2], dtype=any_int_numpy_dtype)

maps = pd.Series([0, 2, 1], dtype=any_int_numpy_dtype)
map_dict = {old: new for (old, new) in zip(maps.values, maps.index)}
map_dict = dict(zip(maps.values, maps.index))

result = labs.replace(map_dict)
expected = labs.replace({0: 0, 2: 1, 1: 2})
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ disable = [
"too-many-public-methods",
"too-many-return-statements",
"too-many-statements",
"unnecessary-comprehension",
"unnecessary-list-index-lookup",
"useless-option-value",

Expand Down