|
6 | 6 | import os |
7 | 7 | import sys |
8 | 8 | from collections.abc import Sequence |
| 9 | +from contextlib import AbstractContextManager, nullcontext |
9 | 10 | from gettext import gettext as _ |
10 | 11 | from io import TextIOWrapper |
11 | 12 | from pathlib import Path |
@@ -1058,6 +1059,9 @@ def identify_imports_main( |
1058 | 1059 | print(str(identified_import)) |
1059 | 1060 |
|
1060 | 1061 |
|
| 1062 | +# Ignore DeepSource cyclomatic complexity check for this function. It is one |
| 1063 | +# the main entrypoints so sort of expected to be complex. |
| 1064 | +# skipcq: PY-R1000 |
1061 | 1065 | def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) -> None: |
1062 | 1066 | arguments = parse_args(argv) |
1063 | 1067 | if arguments.get("show_version"): |
@@ -1193,58 +1197,65 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) |
1193 | 1197 | print(ASCII_ART) |
1194 | 1198 |
|
1195 | 1199 | if jobs: |
1196 | | - import multiprocessing # noqa: PLC0415 |
| 1200 | + import multiprocessing.pool # noqa: PLC0415 |
1197 | 1201 |
|
1198 | | - executor = multiprocessing.Pool(jobs if jobs > 0 else multiprocessing.cpu_count()) |
1199 | | - attempt_iterator = executor.imap( |
1200 | | - functools.partial( |
1201 | | - sort_imports, |
1202 | | - config=config, |
1203 | | - check=check, |
1204 | | - ask_to_apply=ask_to_apply, |
1205 | | - show_diff=show_diff, |
1206 | | - write_to_stdout=write_to_stdout, |
1207 | | - extension=ext_format, |
1208 | | - config_trie=config_trie, |
1209 | | - ), |
1210 | | - file_names, |
| 1202 | + executor_ctx: multiprocessing.pool.Pool | AbstractContextManager[None] = ( |
| 1203 | + multiprocessing.pool.Pool(jobs if jobs > 0 else multiprocessing.cpu_count()) |
1211 | 1204 | ) |
1212 | 1205 | else: |
1213 | | - # https://github.com/python/typeshed/pull/2814 |
1214 | | - attempt_iterator = ( |
1215 | | - sort_imports( # type: ignore |
1216 | | - file_name, |
1217 | | - config=config, |
1218 | | - check=check, |
1219 | | - ask_to_apply=ask_to_apply, |
1220 | | - show_diff=show_diff, |
1221 | | - write_to_stdout=write_to_stdout, |
1222 | | - extension=ext_format, |
1223 | | - config_trie=config_trie, |
| 1206 | + executor_ctx = nullcontext() |
| 1207 | + |
| 1208 | + with executor_ctx as executor: |
| 1209 | + if executor is not None: |
| 1210 | + attempt_iterator = executor.imap( |
| 1211 | + functools.partial( |
| 1212 | + sort_imports, |
| 1213 | + config=config, |
| 1214 | + check=check, |
| 1215 | + ask_to_apply=ask_to_apply, |
| 1216 | + show_diff=show_diff, |
| 1217 | + write_to_stdout=write_to_stdout, |
| 1218 | + extension=ext_format, |
| 1219 | + config_trie=config_trie, |
| 1220 | + ), |
| 1221 | + file_names, |
1224 | 1222 | ) |
1225 | | - for file_name in file_names |
1226 | | - ) |
1227 | | - |
1228 | | - # If any files passed in are missing considered as error, should be removed |
1229 | | - is_no_attempt = True |
1230 | | - any_encoding_valid = False |
1231 | | - for sort_attempt in attempt_iterator: |
1232 | | - if not sort_attempt: |
1233 | | - continue # pragma: no cover - shouldn't happen, satisfies type constraint |
1234 | | - incorrectly_sorted = sort_attempt.incorrectly_sorted |
1235 | | - if arguments.get("check", False) and incorrectly_sorted: |
1236 | | - wrong_sorted_files = True |
1237 | | - if sort_attempt.skipped: |
1238 | | - num_skipped += ( |
1239 | | - 1 # pragma: no cover - shouldn't happen, due to skip in iter_source_code |
| 1223 | + else: |
| 1224 | + # https://github.com/python/typeshed/pull/2814 |
| 1225 | + attempt_iterator = ( |
| 1226 | + sort_imports( # type: ignore |
| 1227 | + file_name, |
| 1228 | + config=config, |
| 1229 | + check=check, |
| 1230 | + ask_to_apply=ask_to_apply, |
| 1231 | + show_diff=show_diff, |
| 1232 | + write_to_stdout=write_to_stdout, |
| 1233 | + extension=ext_format, |
| 1234 | + config_trie=config_trie, |
| 1235 | + ) |
| 1236 | + for file_name in file_names |
1240 | 1237 | ) |
1241 | 1238 |
|
1242 | | - if not sort_attempt.supported_encoding: |
1243 | | - num_invalid_encoding += 1 |
1244 | | - else: |
1245 | | - any_encoding_valid = True |
| 1239 | + # If any files passed in are missing considered as error, should be removed |
| 1240 | + is_no_attempt = True |
| 1241 | + any_encoding_valid = False |
| 1242 | + for sort_attempt in attempt_iterator: |
| 1243 | + if not sort_attempt: |
| 1244 | + continue # pragma: no cover - shouldn't happen, satisfies type constraint |
| 1245 | + incorrectly_sorted = sort_attempt.incorrectly_sorted |
| 1246 | + if arguments.get("check", False) and incorrectly_sorted: |
| 1247 | + wrong_sorted_files = True |
| 1248 | + if sort_attempt.skipped: |
| 1249 | + num_skipped += ( |
| 1250 | + 1 # pragma: no cover - shouldn't happen, due to skip in iter_source_code |
| 1251 | + ) |
| 1252 | + |
| 1253 | + if not sort_attempt.supported_encoding: |
| 1254 | + num_invalid_encoding += 1 |
| 1255 | + else: |
| 1256 | + any_encoding_valid = True |
1246 | 1257 |
|
1247 | | - is_no_attempt = False |
| 1258 | + is_no_attempt = False |
1248 | 1259 |
|
1249 | 1260 | num_skipped += len(skipped) |
1250 | 1261 | if num_skipped and not config.quiet: |
|
0 commit comments