Skip to content

Commit 8428af7

Browse files
authored
[minor] replace loops with comprehensions or calls (#13159)
1 parent 3e978b2 commit 8428af7

File tree

4 files changed

+9
-26
lines changed

4 files changed

+9
-26
lines changed

mypy/constraints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def any_constraints(options: List[Optional[List[Constraint]]], eager: bool) -> L
269269
else:
270270
merged_option = None
271271
merged_options.append(merged_option)
272-
return any_constraints([option for option in merged_options], eager)
272+
return any_constraints(list(merged_options), eager)
273273
# Otherwise, there are either no valid options or multiple, inconsistent valid
274274
# options. Give up and deduce nothing.
275275
return []

mypy/stubgenc.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ def generate_stub_for_c_module(module_name: str,
7474
if name not in done and not inspect.ismodule(obj):
7575
type_str = strip_or_import(get_type_fullname(type(obj)), module, imports)
7676
variables.append(f'{name}: {type_str}')
77-
output = []
78-
for line in sorted(set(imports)):
79-
output.append(line)
77+
output = sorted(set(imports))
8078
for line in variables:
8179
output.append(line)
8280
for line in types:

mypy/test/testcheck.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,7 @@ def find_error_message_paths(self, a: List[str]) -> Set[str]:
304304
return hits
305305

306306
def find_module_files(self, manager: build.BuildManager) -> Dict[str, str]:
307-
modules = {}
308-
for id, module in manager.modules.items():
309-
modules[id] = module.path
310-
return modules
307+
return {id: module.path for id, module in manager.modules.items()}
311308

312309
def find_missing_cache_files(self, modules: Dict[str, str],
313310
manager: build.BuildManager) -> Set[str]:

mypyc/codegen/literals.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ def encoded_tuple_values(self) -> List[str]:
126126
...
127127
"""
128128
values = self.tuple_literals
129-
value_by_index = {}
130-
for value, index in values.items():
131-
value_by_index[index] = value
129+
value_by_index = {index: value for value, index in values.items()}
132130
result = []
133131
num = len(values)
134132
result.append(str(num))
@@ -142,9 +140,7 @@ def encoded_tuple_values(self) -> List[str]:
142140

143141

144142
def _encode_str_values(values: Dict[str, int]) -> List[bytes]:
145-
value_by_index = {}
146-
for value, index in values.items():
147-
value_by_index[index] = value
143+
value_by_index = {index: value for value, index in values.items()}
148144
result = []
149145
line: List[bytes] = []
150146
line_len = 0
@@ -165,9 +161,7 @@ def _encode_str_values(values: Dict[str, int]) -> List[bytes]:
165161

166162

167163
def _encode_bytes_values(values: Dict[bytes, int]) -> List[bytes]:
168-
value_by_index = {}
169-
for value, index in values.items():
170-
value_by_index[index] = value
164+
value_by_index = {index: value for value, index in values.items()}
171165
result = []
172166
line: List[bytes] = []
173167
line_len = 0
@@ -212,9 +206,7 @@ def _encode_int_values(values: Dict[int, int]) -> List[bytes]:
212206
213207
Values are stored in base 10 and separated by 0 bytes.
214208
"""
215-
value_by_index = {}
216-
for value, index in values.items():
217-
value_by_index[index] = value
209+
value_by_index = {index: value for value, index in values.items()}
218210
result = []
219211
line: List[bytes] = []
220212
line_len = 0
@@ -248,9 +240,7 @@ def _encode_float_values(values: Dict[float, int]) -> List[str]:
248240
249241
The result contains the number of values followed by individual values.
250242
"""
251-
value_by_index = {}
252-
for value, index in values.items():
253-
value_by_index[index] = value
243+
value_by_index = {index: value for value, index in values.items()}
254244
result = []
255245
num = len(values)
256246
result.append(str(num))
@@ -266,9 +256,7 @@ def _encode_complex_values(values: Dict[complex, int]) -> List[str]:
266256
The result contains the number of values followed by pairs of doubles
267257
representing complex numbers.
268258
"""
269-
value_by_index = {}
270-
for value, index in values.items():
271-
value_by_index[index] = value
259+
value_by_index = {index: value for value, index in values.items()}
272260
result = []
273261
num = len(values)
274262
result.append(str(num))

0 commit comments

Comments
 (0)