main.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. """Tool for sorting imports alphabetically, and automatically separated into sections."""
  2. import argparse
  3. import functools
  4. import json
  5. import os
  6. import sys
  7. from gettext import gettext as _
  8. from io import TextIOWrapper
  9. from pathlib import Path
  10. from typing import Any, Dict, List, Optional, Sequence, Union
  11. from warnings import warn
  12. from . import __version__, api, files, sections
  13. from .exceptions import FileSkipped, ISortError, UnsupportedEncoding
  14. from .format import create_terminal_printer
  15. from .logo import ASCII_ART
  16. from .profiles import profiles
  17. from .settings import VALID_PY_TARGETS, Config, find_all_configs
  18. from .utils import Trie
  19. from .wrap_modes import WrapModes
  20. DEPRECATED_SINGLE_DASH_ARGS = {
  21. "-ac",
  22. "-af",
  23. "-ca",
  24. "-cs",
  25. "-df",
  26. "-ds",
  27. "-dt",
  28. "-fas",
  29. "-fass",
  30. "-ff",
  31. "-fgw",
  32. "-fss",
  33. "-lai",
  34. "-lbt",
  35. "-le",
  36. "-ls",
  37. "-nis",
  38. "-nlb",
  39. "-ot",
  40. "-rr",
  41. "-sd",
  42. "-sg",
  43. "-sl",
  44. "-sp",
  45. "-tc",
  46. "-wl",
  47. "-ws",
  48. }
  49. QUICK_GUIDE = f"""
  50. {ASCII_ART}
  51. Nothing to do: no files or paths have have been passed in!
  52. Try one of the following:
  53. `isort .` - sort all Python files, starting from the current directory, recursively.
  54. `isort . --interactive` - Do the same, but ask before making any changes.
  55. `isort . --check --diff` - Check to see if imports are correctly sorted within this project.
  56. `isort --help` - In-depth information about isort's available command-line options.
  57. Visit https://pycqa.github.io/isort/ for complete information about how to use isort.
  58. """
  59. class SortAttempt:
  60. def __init__(self, incorrectly_sorted: bool, skipped: bool, supported_encoding: bool) -> None:
  61. self.incorrectly_sorted = incorrectly_sorted
  62. self.skipped = skipped
  63. self.supported_encoding = supported_encoding
  64. def sort_imports(
  65. file_name: str,
  66. config: Config,
  67. check: bool = False,
  68. ask_to_apply: bool = False,
  69. write_to_stdout: bool = False,
  70. **kwargs: Any,
  71. ) -> Optional[SortAttempt]:
  72. incorrectly_sorted: bool = False
  73. skipped: bool = False
  74. try:
  75. if check:
  76. try:
  77. incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
  78. except FileSkipped:
  79. skipped = True
  80. return SortAttempt(incorrectly_sorted, skipped, True)
  81. try:
  82. incorrectly_sorted = not api.sort_file(
  83. file_name,
  84. config=config,
  85. ask_to_apply=ask_to_apply,
  86. write_to_stdout=write_to_stdout,
  87. **kwargs,
  88. )
  89. except FileSkipped:
  90. skipped = True
  91. return SortAttempt(incorrectly_sorted, skipped, True)
  92. except (OSError, ValueError) as error:
  93. warn(f"Unable to parse file {file_name} due to {error}")
  94. return None
  95. except UnsupportedEncoding:
  96. if config.verbose:
  97. warn(f"Encoding not supported for {file_name}")
  98. return SortAttempt(incorrectly_sorted, skipped, False)
  99. except ISortError as error:
  100. _print_hard_fail(config, message=str(error))
  101. sys.exit(1)
  102. except Exception:
  103. _print_hard_fail(config, offending_file=file_name)
  104. raise
  105. def _print_hard_fail(
  106. config: Config, offending_file: Optional[str] = None, message: Optional[str] = None
  107. ) -> None:
  108. """Fail on unrecoverable exception with custom message."""
  109. message = message or (
  110. f"Unrecoverable exception thrown when parsing {offending_file or ''}!"
  111. "This should NEVER happen.\n"
  112. "If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new"
  113. )
  114. printer = create_terminal_printer(
  115. color=config.color_output, error=config.format_error, success=config.format_success
  116. )
  117. printer.error(message)
  118. def _build_arg_parser() -> argparse.ArgumentParser:
  119. parser = argparse.ArgumentParser(
  120. description="Sort Python import definitions alphabetically "
  121. "within logical sections. Run with no arguments to see a quick "
  122. "start guide, otherwise, one or more files/directories/stdin must be provided. "
  123. "Use `-` as the first argument to represent stdin. Use --interactive to use the pre 5.0.0 "
  124. "interactive behavior."
  125. " "
  126. "If you've used isort 4 but are new to isort 5, see the upgrading guide: "
  127. "https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html",
  128. add_help=False, # prevent help option from appearing in "optional arguments" group
  129. )
  130. general_group = parser.add_argument_group("general options")
  131. target_group = parser.add_argument_group("target options")
  132. output_group = parser.add_argument_group("general output options")
  133. inline_args_group = output_group.add_mutually_exclusive_group()
  134. section_group = parser.add_argument_group("section output options")
  135. deprecated_group = parser.add_argument_group("deprecated options")
  136. general_group.add_argument(
  137. "-h",
  138. "--help",
  139. action="help",
  140. default=argparse.SUPPRESS,
  141. help=_("show this help message and exit"),
  142. )
  143. general_group.add_argument(
  144. "-V",
  145. "--version",
  146. action="store_true",
  147. dest="show_version",
  148. help="Displays the currently installed version of isort.",
  149. )
  150. general_group.add_argument(
  151. "--vn",
  152. "--version-number",
  153. action="version",
  154. version=__version__,
  155. help="Returns just the current version number without the logo",
  156. )
  157. general_group.add_argument(
  158. "-v",
  159. "--verbose",
  160. action="store_true",
  161. dest="verbose",
  162. help="Shows verbose output, such as when files are skipped or when a check is successful.",
  163. )
  164. general_group.add_argument(
  165. "--only-modified",
  166. "--om",
  167. dest="only_modified",
  168. action="store_true",
  169. help="Suppresses verbose output for non-modified files.",
  170. )
  171. general_group.add_argument(
  172. "--dedup-headings",
  173. dest="dedup_headings",
  174. action="store_true",
  175. help="Tells isort to only show an identical custom import heading comment once, even if"
  176. " there are multiple sections with the comment set.",
  177. )
  178. general_group.add_argument(
  179. "-q",
  180. "--quiet",
  181. action="store_true",
  182. dest="quiet",
  183. help="Shows extra quiet output, only errors are outputted.",
  184. )
  185. general_group.add_argument(
  186. "-d",
  187. "--stdout",
  188. help="Force resulting output to stdout, instead of in-place.",
  189. dest="write_to_stdout",
  190. action="store_true",
  191. )
  192. general_group.add_argument(
  193. "--overwrite-in-place",
  194. help="Tells isort to overwrite in place using the same file handle. "
  195. "Comes at a performance and memory usage penalty over its standard "
  196. "approach but ensures all file flags and modes stay unchanged.",
  197. dest="overwrite_in_place",
  198. action="store_true",
  199. )
  200. general_group.add_argument(
  201. "--show-config",
  202. dest="show_config",
  203. action="store_true",
  204. help="See isort's determined config, as well as sources of config options.",
  205. )
  206. general_group.add_argument(
  207. "--show-files",
  208. dest="show_files",
  209. action="store_true",
  210. help="See the files isort will be run against with the current config options.",
  211. )
  212. general_group.add_argument(
  213. "--df",
  214. "--diff",
  215. dest="show_diff",
  216. action="store_true",
  217. help="Prints a diff of all the changes isort would make to a file, instead of "
  218. "changing it in place",
  219. )
  220. general_group.add_argument(
  221. "-c",
  222. "--check-only",
  223. "--check",
  224. action="store_true",
  225. dest="check",
  226. help="Checks the file for unsorted / unformatted imports and prints them to the "
  227. "command line without modifying the file. Returns 0 when nothing would change and "
  228. "returns 1 when the file would be reformatted.",
  229. )
  230. general_group.add_argument(
  231. "--ws",
  232. "--ignore-whitespace",
  233. action="store_true",
  234. dest="ignore_whitespace",
  235. help="Tells isort to ignore whitespace differences when --check-only is being used.",
  236. )
  237. general_group.add_argument(
  238. "--sp",
  239. "--settings-path",
  240. "--settings-file",
  241. "--settings",
  242. dest="settings_path",
  243. help="Explicitly set the settings path or file instead of auto determining "
  244. "based on file location.",
  245. )
  246. general_group.add_argument(
  247. "--cr",
  248. "--config-root",
  249. dest="config_root",
  250. help="Explicitly set the config root for resolving all configs. When used "
  251. "with the --resolve-all-configs flag, isort will look at all sub-folders "
  252. "in this config root to resolve config files and sort files based on the "
  253. "closest available config(if any)",
  254. )
  255. general_group.add_argument(
  256. "--resolve-all-configs",
  257. dest="resolve_all_configs",
  258. action="store_true",
  259. help="Tells isort to resolve the configs for all sub-directories "
  260. "and sort files in terms of its closest config files.",
  261. )
  262. general_group.add_argument(
  263. "--profile",
  264. dest="profile",
  265. type=str,
  266. help="Base profile type to use for configuration. "
  267. f"Profiles include: {', '.join(profiles.keys())}. As well as any shared profiles.",
  268. )
  269. general_group.add_argument(
  270. "--old-finders",
  271. "--magic-placement",
  272. dest="old_finders",
  273. action="store_true",
  274. help="Use the old deprecated finder logic that relies on environment introspection magic.",
  275. )
  276. general_group.add_argument(
  277. "-j",
  278. "--jobs",
  279. help="Number of files to process in parallel.",
  280. dest="jobs",
  281. type=int,
  282. nargs="?",
  283. const=-1,
  284. )
  285. general_group.add_argument(
  286. "--ac",
  287. "--atomic",
  288. dest="atomic",
  289. action="store_true",
  290. help="Ensures the output doesn't save if the resulting file contains syntax errors.",
  291. )
  292. general_group.add_argument(
  293. "--interactive",
  294. dest="ask_to_apply",
  295. action="store_true",
  296. help="Tells isort to apply changes interactively.",
  297. )
  298. general_group.add_argument(
  299. "--format-error",
  300. dest="format_error",
  301. help="Override the format used to print errors.",
  302. )
  303. general_group.add_argument(
  304. "--format-success",
  305. dest="format_success",
  306. help="Override the format used to print success.",
  307. )
  308. target_group.add_argument(
  309. "files", nargs="*", help="One or more Python source files that need their imports sorted."
  310. )
  311. target_group.add_argument(
  312. "--filter-files",
  313. dest="filter_files",
  314. action="store_true",
  315. help="Tells isort to filter files even when they are explicitly passed in as "
  316. "part of the CLI command.",
  317. )
  318. target_group.add_argument(
  319. "-s",
  320. "--skip",
  321. help="Files that isort should skip over. If you want to skip multiple "
  322. "files you should specify twice: --skip file1 --skip file2. Values can be "
  323. "file names, directory names or file paths. To skip all files in a nested path "
  324. "use --skip-glob.",
  325. dest="skip",
  326. action="append",
  327. )
  328. target_group.add_argument(
  329. "--extend-skip",
  330. help="Extends --skip to add additional files that isort should skip over. "
  331. "If you want to skip multiple "
  332. "files you should specify twice: --skip file1 --skip file2. Values can be "
  333. "file names, directory names or file paths. To skip all files in a nested path "
  334. "use --skip-glob.",
  335. dest="extend_skip",
  336. action="append",
  337. )
  338. target_group.add_argument(
  339. "--sg",
  340. "--skip-glob",
  341. help="Files that isort should skip over.",
  342. dest="skip_glob",
  343. action="append",
  344. )
  345. target_group.add_argument(
  346. "--extend-skip-glob",
  347. help="Additional files that isort should skip over (extending --skip-glob).",
  348. dest="extend_skip_glob",
  349. action="append",
  350. )
  351. target_group.add_argument(
  352. "--gitignore",
  353. "--skip-gitignore",
  354. action="store_true",
  355. dest="skip_gitignore",
  356. help="Treat project as a git repository and ignore files listed in .gitignore."
  357. "\nNOTE: This requires git to be installed and accessible from the same shell as isort.",
  358. )
  359. target_group.add_argument(
  360. "--ext",
  361. "--extension",
  362. "--supported-extension",
  363. dest="supported_extensions",
  364. action="append",
  365. help="Specifies what extensions isort can be run against.",
  366. )
  367. target_group.add_argument(
  368. "--blocked-extension",
  369. dest="blocked_extensions",
  370. action="append",
  371. help="Specifies what extensions isort can never be run against.",
  372. )
  373. target_group.add_argument(
  374. "--dont-follow-links",
  375. dest="dont_follow_links",
  376. action="store_true",
  377. help="Tells isort not to follow symlinks that are encountered when running recursively.",
  378. )
  379. target_group.add_argument(
  380. "--filename",
  381. dest="filename",
  382. help="Provide the filename associated with a stream.",
  383. )
  384. target_group.add_argument(
  385. "--allow-root",
  386. action="store_true",
  387. default=False,
  388. help="Tells isort not to treat / specially, allowing it to be run against the root dir.",
  389. )
  390. output_group.add_argument(
  391. "-a",
  392. "--add-import",
  393. dest="add_imports",
  394. action="append",
  395. help="Adds the specified import line to all files, "
  396. "automatically determining correct placement.",
  397. )
  398. output_group.add_argument(
  399. "--append",
  400. "--append-only",
  401. dest="append_only",
  402. action="store_true",
  403. help="Only adds the imports specified in --add-import if the file"
  404. " contains existing imports.",
  405. )
  406. output_group.add_argument(
  407. "--af",
  408. "--force-adds",
  409. dest="force_adds",
  410. action="store_true",
  411. help="Forces import adds even if the original file is empty.",
  412. )
  413. output_group.add_argument(
  414. "--rm",
  415. "--remove-import",
  416. dest="remove_imports",
  417. action="append",
  418. help="Removes the specified import from all files.",
  419. )
  420. output_group.add_argument(
  421. "--float-to-top",
  422. dest="float_to_top",
  423. action="store_true",
  424. help="Causes all non-indented imports to float to the top of the file having its imports "
  425. "sorted (immediately below the top of file comment).\n"
  426. "This can be an excellent shortcut for collecting imports every once in a while "
  427. "when you place them in the middle of a file to avoid context switching.\n\n"
  428. "*NOTE*: It currently doesn't work with cimports and introduces some extra over-head "
  429. "and a performance penalty.",
  430. )
  431. output_group.add_argument(
  432. "--dont-float-to-top",
  433. dest="dont_float_to_top",
  434. action="store_true",
  435. help="Forces --float-to-top setting off. See --float-to-top for more information.",
  436. )
  437. output_group.add_argument(
  438. "--ca",
  439. "--combine-as",
  440. dest="combine_as_imports",
  441. action="store_true",
  442. help="Combines as imports on the same line.",
  443. )
  444. output_group.add_argument(
  445. "--cs",
  446. "--combine-star",
  447. dest="combine_star",
  448. action="store_true",
  449. help="Ensures that if a star import is present, "
  450. "nothing else is imported from that namespace.",
  451. )
  452. output_group.add_argument(
  453. "-e",
  454. "--balanced",
  455. dest="balanced_wrapping",
  456. action="store_true",
  457. help="Balances wrapping to produce the most consistent line length possible",
  458. )
  459. output_group.add_argument(
  460. "--ff",
  461. "--from-first",
  462. dest="from_first",
  463. action="store_true",
  464. help="Switches the typical ordering preference, "
  465. "showing from imports first then straight ones.",
  466. )
  467. output_group.add_argument(
  468. "--fgw",
  469. "--force-grid-wrap",
  470. nargs="?",
  471. const=2,
  472. type=int,
  473. dest="force_grid_wrap",
  474. help="Force number of from imports (defaults to 2 when passed as CLI flag without value) "
  475. "to be grid wrapped regardless of line "
  476. "length. If 0 is passed in (the global default) only line length is considered.",
  477. )
  478. output_group.add_argument(
  479. "-i",
  480. "--indent",
  481. help='String to place for indents defaults to " " (4 spaces).',
  482. dest="indent",
  483. type=str,
  484. )
  485. output_group.add_argument(
  486. "--lbi", "--lines-before-imports", dest="lines_before_imports", type=int
  487. )
  488. output_group.add_argument(
  489. "--lai", "--lines-after-imports", dest="lines_after_imports", type=int
  490. )
  491. output_group.add_argument(
  492. "--lbt", "--lines-between-types", dest="lines_between_types", type=int
  493. )
  494. output_group.add_argument(
  495. "--le",
  496. "--line-ending",
  497. dest="line_ending",
  498. help="Forces line endings to the specified value. "
  499. "If not set, values will be guessed per-file.",
  500. )
  501. output_group.add_argument(
  502. "--ls",
  503. "--length-sort",
  504. help="Sort imports by their string length.",
  505. dest="length_sort",
  506. action="store_true",
  507. )
  508. output_group.add_argument(
  509. "--lss",
  510. "--length-sort-straight",
  511. help="Sort straight imports by their string length. Similar to `length_sort` "
  512. "but applies only to straight imports and doesn't affect from imports.",
  513. dest="length_sort_straight",
  514. action="store_true",
  515. )
  516. output_group.add_argument(
  517. "-m",
  518. "--multi-line",
  519. dest="multi_line_output",
  520. choices=list(WrapModes.__members__.keys())
  521. + [str(mode.value) for mode in WrapModes.__members__.values()],
  522. type=str,
  523. help="Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, "
  524. "5-vert-grid-grouped, 6-deprecated-alias-for-5, 7-noqa, "
  525. "8-vertical-hanging-indent-bracket, 9-vertical-prefix-from-module-import, "
  526. "10-hanging-indent-with-parentheses).",
  527. )
  528. output_group.add_argument(
  529. "-n",
  530. "--ensure-newline-before-comments",
  531. dest="ensure_newline_before_comments",
  532. action="store_true",
  533. help="Inserts a blank line before a comment following an import.",
  534. )
  535. inline_args_group.add_argument(
  536. "--nis",
  537. "--no-inline-sort",
  538. dest="no_inline_sort",
  539. action="store_true",
  540. help="Leaves `from` imports with multiple imports 'as-is' "
  541. "(e.g. `from foo import a, c ,b`).",
  542. )
  543. output_group.add_argument(
  544. "--ot",
  545. "--order-by-type",
  546. dest="order_by_type",
  547. action="store_true",
  548. help="Order imports by type, which is determined by case, in addition to alphabetically.\n"
  549. "\n**NOTE**: type here refers to the implied type from the import name capitalization.\n"
  550. ' isort does not do type introspection for the imports. These "types" are simply: '
  551. "CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8"
  552. " or a related coding standard and has many imports this is a good default, otherwise you "
  553. "likely will want to turn it off. From the CLI the `--dont-order-by-type` option will turn "
  554. "this off.",
  555. )
  556. output_group.add_argument(
  557. "--dt",
  558. "--dont-order-by-type",
  559. dest="dont_order_by_type",
  560. action="store_true",
  561. help="Don't order imports by type, which is determined by case, in addition to "
  562. "alphabetically.\n\n"
  563. "**NOTE**: type here refers to the implied type from the import name capitalization.\n"
  564. ' isort does not do type introspection for the imports. These "types" are simply: '
  565. "CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8"
  566. " or a related coding standard and has many imports this is a good default. You can turn "
  567. "this on from the CLI using `--order-by-type`.",
  568. )
  569. output_group.add_argument(
  570. "--rr",
  571. "--reverse-relative",
  572. dest="reverse_relative",
  573. action="store_true",
  574. help="Reverse order of relative imports.",
  575. )
  576. output_group.add_argument(
  577. "--reverse-sort",
  578. dest="reverse_sort",
  579. action="store_true",
  580. help="Reverses the ordering of imports.",
  581. )
  582. output_group.add_argument(
  583. "--sort-order",
  584. dest="sort_order",
  585. help="Specify sorting function. Can be built in (natural[default] = force numbers "
  586. "to be sequential, native = Python's built-in sorted function) or an installable plugin.",
  587. )
  588. inline_args_group.add_argument(
  589. "--sl",
  590. "--force-single-line-imports",
  591. dest="force_single_line",
  592. action="store_true",
  593. help="Forces all from imports to appear on their own line",
  594. )
  595. output_group.add_argument(
  596. "--nsl",
  597. "--single-line-exclusions",
  598. help="One or more modules to exclude from the single line rule.",
  599. dest="single_line_exclusions",
  600. action="append",
  601. )
  602. output_group.add_argument(
  603. "--tc",
  604. "--trailing-comma",
  605. dest="include_trailing_comma",
  606. action="store_true",
  607. help="Includes a trailing comma on multi line imports that include parentheses.",
  608. )
  609. output_group.add_argument(
  610. "--up",
  611. "--use-parentheses",
  612. dest="use_parentheses",
  613. action="store_true",
  614. help="Use parentheses for line continuation on length limit instead of slashes."
  615. " **NOTE**: This is separate from wrap modes, and only affects how individual lines that "
  616. " are too long get continued, not sections of multiple imports.",
  617. )
  618. output_group.add_argument(
  619. "-l",
  620. "-w",
  621. "--line-length",
  622. "--line-width",
  623. help="The max length of an import line (used for wrapping long imports).",
  624. dest="line_length",
  625. type=int,
  626. )
  627. output_group.add_argument(
  628. "--wl",
  629. "--wrap-length",
  630. dest="wrap_length",
  631. type=int,
  632. help="Specifies how long lines that are wrapped should be, if not set line_length is used."
  633. "\nNOTE: wrap_length must be LOWER than or equal to line_length.",
  634. )
  635. output_group.add_argument(
  636. "--case-sensitive",
  637. dest="case_sensitive",
  638. action="store_true",
  639. help="Tells isort to include casing when sorting module names",
  640. )
  641. output_group.add_argument(
  642. "--remove-redundant-aliases",
  643. dest="remove_redundant_aliases",
  644. action="store_true",
  645. help=(
  646. "Tells isort to remove redundant aliases from imports, such as `import os as os`."
  647. " This defaults to `False` simply because some projects use these seemingly useless "
  648. " aliases to signify intent and change behaviour."
  649. ),
  650. )
  651. output_group.add_argument(
  652. "--honor-noqa",
  653. dest="honor_noqa",
  654. action="store_true",
  655. help="Tells isort to honor noqa comments to enforce skipping those comments.",
  656. )
  657. output_group.add_argument(
  658. "--treat-comment-as-code",
  659. dest="treat_comments_as_code",
  660. action="append",
  661. help="Tells isort to treat the specified single line comment(s) as if they are code.",
  662. )
  663. output_group.add_argument(
  664. "--treat-all-comment-as-code",
  665. dest="treat_all_comments_as_code",
  666. action="store_true",
  667. help="Tells isort to treat all single line comments as if they are code.",
  668. )
  669. output_group.add_argument(
  670. "--formatter",
  671. dest="formatter",
  672. type=str,
  673. help="Specifies the name of a formatting plugin to use when producing output.",
  674. )
  675. output_group.add_argument(
  676. "--color",
  677. dest="color_output",
  678. action="store_true",
  679. help="Tells isort to use color in terminal output.",
  680. )
  681. output_group.add_argument(
  682. "--ext-format",
  683. dest="ext_format",
  684. help="Tells isort to format the given files according to an extensions formatting rules.",
  685. )
  686. output_group.add_argument(
  687. "--star-first",
  688. help="Forces star imports above others to avoid overriding directly imported variables.",
  689. dest="star_first",
  690. action="store_true",
  691. )
  692. section_group.add_argument(
  693. "--sd",
  694. "--section-default",
  695. dest="default_section",
  696. help="Sets the default section for import options: " + str(sections.DEFAULT),
  697. )
  698. section_group.add_argument(
  699. "--only-sections",
  700. "--os",
  701. dest="only_sections",
  702. action="store_true",
  703. help="Causes imports to be sorted based on their sections like STDLIB, THIRDPARTY, etc. "
  704. "Within sections, the imports are ordered by their import style and the imports with "
  705. "the same style maintain their relative positions.",
  706. )
  707. section_group.add_argument(
  708. "--ds",
  709. "--no-sections",
  710. help="Put all imports into the same section bucket",
  711. dest="no_sections",
  712. action="store_true",
  713. )
  714. section_group.add_argument(
  715. "--fas",
  716. "--force-alphabetical-sort",
  717. action="store_true",
  718. dest="force_alphabetical_sort",
  719. help="Force all imports to be sorted as a single section",
  720. )
  721. section_group.add_argument(
  722. "--fss",
  723. "--force-sort-within-sections",
  724. action="store_true",
  725. dest="force_sort_within_sections",
  726. help="Don't sort straight-style imports (like import sys) before from-style imports "
  727. "(like from itertools import groupby). Instead, sort the imports by module, "
  728. "independent of import style.",
  729. )
  730. section_group.add_argument(
  731. "--hcss",
  732. "--honor-case-in-force-sorted-sections",
  733. action="store_true",
  734. dest="honor_case_in_force_sorted_sections",
  735. help="Honor `--case-sensitive` when `--force-sort-within-sections` is being used. "
  736. "Without this option set, `--order-by-type` decides module name ordering too.",
  737. )
  738. section_group.add_argument(
  739. "--srss",
  740. "--sort-relative-in-force-sorted-sections",
  741. action="store_true",
  742. dest="sort_relative_in_force_sorted_sections",
  743. help="When using `--force-sort-within-sections`, sort relative imports the same "
  744. "way as they are sorted when not using that setting.",
  745. )
  746. section_group.add_argument(
  747. "--fass",
  748. "--force-alphabetical-sort-within-sections",
  749. action="store_true",
  750. dest="force_alphabetical_sort_within_sections",
  751. help="Force all imports to be sorted alphabetically within a section",
  752. )
  753. section_group.add_argument(
  754. "-t",
  755. "--top",
  756. help="Force specific imports to the top of their appropriate section.",
  757. dest="force_to_top",
  758. action="append",
  759. )
  760. section_group.add_argument(
  761. "--combine-straight-imports",
  762. "--csi",
  763. dest="combine_straight_imports",
  764. action="store_true",
  765. help="Combines all the bare straight imports of the same section in a single line. "
  766. "Won't work with sections which have 'as' imports",
  767. )
  768. section_group.add_argument(
  769. "--nlb",
  770. "--no-lines-before",
  771. help="Sections which should not be split with previous by empty lines",
  772. dest="no_lines_before",
  773. action="append",
  774. )
  775. section_group.add_argument(
  776. "--src",
  777. "--src-path",
  778. dest="src_paths",
  779. action="append",
  780. help="Add an explicitly defined source path "
  781. "(modules within src paths have their imports automatically categorized as first_party)."
  782. " Glob expansion (`*` and `**`) is supported for this option.",
  783. )
  784. section_group.add_argument(
  785. "-b",
  786. "--builtin",
  787. dest="known_standard_library",
  788. action="append",
  789. help="Force isort to recognize a module as part of Python's standard library.",
  790. )
  791. section_group.add_argument(
  792. "--extra-builtin",
  793. dest="extra_standard_library",
  794. action="append",
  795. help="Extra modules to be included in the list of ones in Python's standard library.",
  796. )
  797. section_group.add_argument(
  798. "-f",
  799. "--future",
  800. dest="known_future_library",
  801. action="append",
  802. help="Force isort to recognize a module as part of Python's internal future compatibility "
  803. "libraries. WARNING: this overrides the behavior of __future__ handling and therefore"
  804. " can result in code that can't execute. If you're looking to add dependencies such "
  805. "as six, a better option is to create another section below --future using custom "
  806. "sections. See: https://github.com/PyCQA/isort#custom-sections-and-ordering and the "
  807. "discussion here: https://github.com/PyCQA/isort/issues/1463.",
  808. )
  809. section_group.add_argument(
  810. "-o",
  811. "--thirdparty",
  812. dest="known_third_party",
  813. action="append",
  814. help="Force isort to recognize a module as being part of a third party library.",
  815. )
  816. section_group.add_argument(
  817. "-p",
  818. "--project",
  819. dest="known_first_party",
  820. action="append",
  821. help="Force isort to recognize a module as being part of the current python project.",
  822. )
  823. section_group.add_argument(
  824. "--known-local-folder",
  825. dest="known_local_folder",
  826. action="append",
  827. help="Force isort to recognize a module as being a local folder. "
  828. "Generally, this is reserved for relative imports (from . import module).",
  829. )
  830. section_group.add_argument(
  831. "--virtual-env",
  832. dest="virtual_env",
  833. help="Virtual environment to use for determining whether a package is third-party",
  834. )
  835. section_group.add_argument(
  836. "--conda-env",
  837. dest="conda_env",
  838. help="Conda environment to use for determining whether a package is third-party",
  839. )
  840. section_group.add_argument(
  841. "--py",
  842. "--python-version",
  843. action="store",
  844. dest="py_version",
  845. choices=tuple(VALID_PY_TARGETS) + ("auto",),
  846. help="Tells isort to set the known standard library based on the specified Python "
  847. "version. Default is to assume any Python 3 version could be the target, and use a union "
  848. "of all stdlib modules across versions. If auto is specified, the version of the "
  849. "interpreter used to run isort "
  850. f"(currently: {sys.version_info.major}{sys.version_info.minor}) will be used.",
  851. )
  852. # deprecated options
  853. deprecated_group.add_argument(
  854. "--recursive",
  855. dest="deprecated_flags",
  856. action="append_const",
  857. const="--recursive",
  858. help=argparse.SUPPRESS,
  859. )
  860. deprecated_group.add_argument(
  861. "-rc", dest="deprecated_flags", action="append_const", const="-rc", help=argparse.SUPPRESS
  862. )
  863. deprecated_group.add_argument(
  864. "--dont-skip",
  865. dest="deprecated_flags",
  866. action="append_const",
  867. const="--dont-skip",
  868. help=argparse.SUPPRESS,
  869. )
  870. deprecated_group.add_argument(
  871. "-ns", dest="deprecated_flags", action="append_const", const="-ns", help=argparse.SUPPRESS
  872. )
  873. deprecated_group.add_argument(
  874. "--apply",
  875. dest="deprecated_flags",
  876. action="append_const",
  877. const="--apply",
  878. help=argparse.SUPPRESS,
  879. )
  880. deprecated_group.add_argument(
  881. "-k",
  882. "--keep-direct-and-as",
  883. dest="deprecated_flags",
  884. action="append_const",
  885. const="--keep-direct-and-as",
  886. help=argparse.SUPPRESS,
  887. )
  888. return parser
  889. def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
  890. argv = sys.argv[1:] if argv is None else list(argv)
  891. remapped_deprecated_args = []
  892. for index, arg in enumerate(argv):
  893. if arg in DEPRECATED_SINGLE_DASH_ARGS:
  894. remapped_deprecated_args.append(arg)
  895. argv[index] = f"-{arg}"
  896. parser = _build_arg_parser()
  897. arguments = {key: value for key, value in vars(parser.parse_args(argv)).items() if value}
  898. if remapped_deprecated_args:
  899. arguments["remapped_deprecated_args"] = remapped_deprecated_args
  900. if "dont_order_by_type" in arguments:
  901. arguments["order_by_type"] = False
  902. del arguments["dont_order_by_type"]
  903. if "dont_follow_links" in arguments:
  904. arguments["follow_links"] = False
  905. del arguments["dont_follow_links"]
  906. if "dont_float_to_top" in arguments:
  907. del arguments["dont_float_to_top"]
  908. if arguments.get("float_to_top", False):
  909. sys.exit("Can't set both --float-to-top and --dont-float-to-top.")
  910. else:
  911. arguments["float_to_top"] = False
  912. multi_line_output = arguments.get("multi_line_output", None)
  913. if multi_line_output:
  914. if multi_line_output.isdigit():
  915. arguments["multi_line_output"] = WrapModes(int(multi_line_output))
  916. else:
  917. arguments["multi_line_output"] = WrapModes[multi_line_output]
  918. return arguments
  919. def _preconvert(item: Any) -> Union[str, List[Any]]:
  920. """Preconverts objects from native types into JSONifyiable types"""
  921. if isinstance(item, (set, frozenset)):
  922. return list(item)
  923. if isinstance(item, WrapModes):
  924. return str(item.name)
  925. if isinstance(item, Path):
  926. return str(item)
  927. if callable(item) and hasattr(item, "__name__"):
  928. return str(item.__name__)
  929. raise TypeError("Unserializable object {} of type {}".format(item, type(item)))
  930. def identify_imports_main(
  931. argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = None
  932. ) -> None:
  933. parser = argparse.ArgumentParser(
  934. description="Get all import definitions from a given file."
  935. "Use `-` as the first argument to represent stdin."
  936. )
  937. parser.add_argument(
  938. "files", nargs="+", help="One or more Python source files that need their imports sorted."
  939. )
  940. parser.add_argument(
  941. "--top-only",
  942. action="store_true",
  943. default=False,
  944. help="Only identify imports that occur in before functions or classes.",
  945. )
  946. target_group = parser.add_argument_group("target options")
  947. target_group.add_argument(
  948. "--follow-links",
  949. action="store_true",
  950. default=False,
  951. help="Tells isort to follow symlinks that are encountered when running recursively.",
  952. )
  953. uniqueness = parser.add_mutually_exclusive_group()
  954. uniqueness.add_argument(
  955. "--unique",
  956. action="store_true",
  957. default=False,
  958. help="If true, isort will only identify unique imports.",
  959. )
  960. uniqueness.add_argument(
  961. "--packages",
  962. dest="unique",
  963. action="store_const",
  964. const=api.ImportKey.PACKAGE,
  965. default=False,
  966. help="If true, isort will only identify the unique top level modules imported.",
  967. )
  968. uniqueness.add_argument(
  969. "--modules",
  970. dest="unique",
  971. action="store_const",
  972. const=api.ImportKey.MODULE,
  973. default=False,
  974. help="If true, isort will only identify the unique modules imported.",
  975. )
  976. uniqueness.add_argument(
  977. "--attributes",
  978. dest="unique",
  979. action="store_const",
  980. const=api.ImportKey.ATTRIBUTE,
  981. default=False,
  982. help="If true, isort will only identify the unique attributes imported.",
  983. )
  984. arguments = parser.parse_args(argv)
  985. file_names = arguments.files
  986. if file_names == ["-"]:
  987. identified_imports = api.find_imports_in_stream(
  988. sys.stdin if stdin is None else stdin,
  989. unique=arguments.unique,
  990. top_only=arguments.top_only,
  991. follow_links=arguments.follow_links,
  992. )
  993. else:
  994. identified_imports = api.find_imports_in_paths(
  995. file_names,
  996. unique=arguments.unique,
  997. top_only=arguments.top_only,
  998. follow_links=arguments.follow_links,
  999. )
  1000. for identified_import in identified_imports:
  1001. if arguments.unique == api.ImportKey.PACKAGE:
  1002. print(identified_import.module.split(".")[0])
  1003. elif arguments.unique == api.ImportKey.MODULE:
  1004. print(identified_import.module)
  1005. elif arguments.unique == api.ImportKey.ATTRIBUTE:
  1006. print(f"{identified_import.module}.{identified_import.attribute}")
  1007. else:
  1008. print(str(identified_import))
  1009. def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = None) -> None:
  1010. arguments = parse_args(argv)
  1011. if arguments.get("show_version"):
  1012. print(ASCII_ART)
  1013. return
  1014. show_config: bool = arguments.pop("show_config", False)
  1015. show_files: bool = arguments.pop("show_files", False)
  1016. if show_config and show_files:
  1017. sys.exit("Error: either specify show-config or show-files not both.")
  1018. if "settings_path" in arguments:
  1019. if os.path.isfile(arguments["settings_path"]):
  1020. arguments["settings_file"] = os.path.abspath(arguments["settings_path"])
  1021. arguments["settings_path"] = os.path.dirname(arguments["settings_file"])
  1022. else:
  1023. arguments["settings_path"] = os.path.abspath(arguments["settings_path"])
  1024. if "virtual_env" in arguments:
  1025. venv = arguments["virtual_env"]
  1026. arguments["virtual_env"] = os.path.abspath(venv)
  1027. if not os.path.isdir(arguments["virtual_env"]):
  1028. warn(f"virtual_env dir does not exist: {arguments['virtual_env']}")
  1029. file_names = arguments.pop("files", [])
  1030. if not file_names and not show_config:
  1031. print(QUICK_GUIDE)
  1032. if arguments:
  1033. sys.exit("Error: arguments passed in without any paths or content.")
  1034. return
  1035. if "settings_path" not in arguments:
  1036. arguments["settings_path"] = (
  1037. os.path.abspath(file_names[0] if file_names else ".") or os.getcwd()
  1038. )
  1039. if not os.path.isdir(arguments["settings_path"]):
  1040. arguments["settings_path"] = os.path.dirname(arguments["settings_path"])
  1041. config_dict = arguments.copy()
  1042. ask_to_apply = config_dict.pop("ask_to_apply", False)
  1043. jobs = config_dict.pop("jobs", None)
  1044. check = config_dict.pop("check", False)
  1045. show_diff = config_dict.pop("show_diff", False)
  1046. write_to_stdout = config_dict.pop("write_to_stdout", False)
  1047. deprecated_flags = config_dict.pop("deprecated_flags", False)
  1048. remapped_deprecated_args = config_dict.pop("remapped_deprecated_args", False)
  1049. stream_filename = config_dict.pop("filename", None)
  1050. ext_format = config_dict.pop("ext_format", None)
  1051. allow_root = config_dict.pop("allow_root", None)
  1052. resolve_all_configs = config_dict.pop("resolve_all_configs", False)
  1053. wrong_sorted_files = False
  1054. all_attempt_broken = False
  1055. no_valid_encodings = False
  1056. config_trie: Optional[Trie] = None
  1057. if resolve_all_configs:
  1058. config_trie = find_all_configs(config_dict.pop("config_root", "."))
  1059. if "src_paths" in config_dict:
  1060. config_dict["src_paths"] = {
  1061. Path(src_path).resolve() for src_path in config_dict.get("src_paths", ())
  1062. }
  1063. config = Config(**config_dict)
  1064. if show_config:
  1065. print(json.dumps(config.__dict__, indent=4, separators=(",", ": "), default=_preconvert))
  1066. return
  1067. if file_names == ["-"]:
  1068. file_path = Path(stream_filename) if stream_filename else None
  1069. if show_files:
  1070. sys.exit("Error: can't show files for streaming input.")
  1071. input_stream = sys.stdin if stdin is None else stdin
  1072. if check:
  1073. incorrectly_sorted = not api.check_stream(
  1074. input_stream=input_stream,
  1075. config=config,
  1076. show_diff=show_diff,
  1077. file_path=file_path,
  1078. extension=ext_format,
  1079. )
  1080. wrong_sorted_files = incorrectly_sorted
  1081. else:
  1082. try:
  1083. api.sort_stream(
  1084. input_stream=input_stream,
  1085. output_stream=sys.stdout,
  1086. config=config,
  1087. show_diff=show_diff,
  1088. file_path=file_path,
  1089. extension=ext_format,
  1090. raise_on_skip=False,
  1091. )
  1092. except FileSkipped:
  1093. sys.stdout.write(input_stream.read())
  1094. elif "/" in file_names and not allow_root:
  1095. printer = create_terminal_printer(
  1096. color=config.color_output, error=config.format_error, success=config.format_success
  1097. )
  1098. printer.error("it is dangerous to operate recursively on '/'")
  1099. printer.error("use --allow-root to override this failsafe")
  1100. sys.exit(1)
  1101. else:
  1102. if stream_filename:
  1103. printer = create_terminal_printer(
  1104. color=config.color_output, error=config.format_error, success=config.format_success
  1105. )
  1106. printer.error("Filename override is intended only for stream (-) sorting.")
  1107. sys.exit(1)
  1108. skipped: List[str] = []
  1109. broken: List[str] = []
  1110. if config.filter_files:
  1111. filtered_files = []
  1112. for file_name in file_names:
  1113. if config.is_skipped(Path(file_name)):
  1114. skipped.append(file_name)
  1115. else:
  1116. filtered_files.append(file_name)
  1117. file_names = filtered_files
  1118. file_names = files.find(file_names, config, skipped, broken)
  1119. if show_files:
  1120. for file_name in file_names:
  1121. print(file_name)
  1122. return
  1123. num_skipped = 0
  1124. num_broken = 0
  1125. num_invalid_encoding = 0
  1126. if config.verbose:
  1127. print(ASCII_ART)
  1128. if jobs:
  1129. import multiprocessing
  1130. executor = multiprocessing.Pool(jobs if jobs > 0 else multiprocessing.cpu_count())
  1131. attempt_iterator = executor.imap(
  1132. functools.partial(
  1133. sort_imports,
  1134. config=config,
  1135. check=check,
  1136. ask_to_apply=ask_to_apply,
  1137. write_to_stdout=write_to_stdout,
  1138. extension=ext_format,
  1139. config_trie=config_trie,
  1140. ),
  1141. file_names,
  1142. )
  1143. else:
  1144. # https://github.com/python/typeshed/pull/2814
  1145. attempt_iterator = (
  1146. sort_imports( # type: ignore
  1147. file_name,
  1148. config=config,
  1149. check=check,
  1150. ask_to_apply=ask_to_apply,
  1151. show_diff=show_diff,
  1152. write_to_stdout=write_to_stdout,
  1153. extension=ext_format,
  1154. config_trie=config_trie,
  1155. )
  1156. for file_name in file_names
  1157. )
  1158. # If any files passed in are missing considered as error, should be removed
  1159. is_no_attempt = True
  1160. any_encoding_valid = False
  1161. for sort_attempt in attempt_iterator:
  1162. if not sort_attempt:
  1163. continue # pragma: no cover - shouldn't happen, satisfies type constraint
  1164. incorrectly_sorted = sort_attempt.incorrectly_sorted
  1165. if arguments.get("check", False) and incorrectly_sorted:
  1166. wrong_sorted_files = True
  1167. if sort_attempt.skipped:
  1168. num_skipped += (
  1169. 1 # pragma: no cover - shouldn't happen, due to skip in iter_source_code
  1170. )
  1171. if not sort_attempt.supported_encoding:
  1172. num_invalid_encoding += 1
  1173. else:
  1174. any_encoding_valid = True
  1175. is_no_attempt = False
  1176. num_skipped += len(skipped)
  1177. if num_skipped and not config.quiet:
  1178. if config.verbose:
  1179. for was_skipped in skipped:
  1180. print(
  1181. f"{was_skipped} was skipped as it's listed in 'skip' setting, "
  1182. "matches a glob in 'skip_glob' setting, or is in a .gitignore file with "
  1183. "--skip-gitignore enabled."
  1184. )
  1185. print(f"Skipped {num_skipped} files")
  1186. num_broken += len(broken)
  1187. if num_broken and not config.quiet:
  1188. if config.verbose:
  1189. for was_broken in broken:
  1190. warn(f"{was_broken} was broken path, make sure it exists correctly")
  1191. print(f"Broken {num_broken} paths")
  1192. if num_broken > 0 and is_no_attempt:
  1193. all_attempt_broken = True
  1194. if num_invalid_encoding > 0 and not any_encoding_valid:
  1195. no_valid_encodings = True
  1196. if not config.quiet and (remapped_deprecated_args or deprecated_flags):
  1197. if remapped_deprecated_args:
  1198. warn(
  1199. "W0502: The following deprecated single dash CLI flags were used and translated: "
  1200. f"{', '.join(remapped_deprecated_args)}!"
  1201. )
  1202. if deprecated_flags:
  1203. warn(
  1204. "W0501: The following deprecated CLI flags were used and ignored: "
  1205. f"{', '.join(deprecated_flags)}!"
  1206. )
  1207. warn(
  1208. "W0500: Please see the 5.0.0 Upgrade guide: "
  1209. "https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html"
  1210. )
  1211. if wrong_sorted_files:
  1212. sys.exit(1)
  1213. if all_attempt_broken:
  1214. sys.exit(1)
  1215. if no_valid_encodings:
  1216. printer = create_terminal_printer(
  1217. color=config.color_output, error=config.format_error, success=config.format_success
  1218. )
  1219. printer.error("No valid encodings.")
  1220. sys.exit(1)
  1221. if __name__ == "__main__":
  1222. main()