api.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. __all__ = (
  2. "ImportKey",
  3. "check_code_string",
  4. "check_file",
  5. "check_stream",
  6. "find_imports_in_code",
  7. "find_imports_in_file",
  8. "find_imports_in_paths",
  9. "find_imports_in_stream",
  10. "place_module",
  11. "place_module_with_reason",
  12. "sort_code_string",
  13. "sort_file",
  14. "sort_stream",
  15. )
  16. import contextlib
  17. import shutil
  18. import sys
  19. from enum import Enum
  20. from io import StringIO
  21. from itertools import chain
  22. from pathlib import Path
  23. from typing import Any, Iterator, Optional, Set, TextIO, Union, cast
  24. from warnings import warn
  25. from isort import core
  26. from . import files, identify, io
  27. from .exceptions import (
  28. ExistingSyntaxErrors,
  29. FileSkipComment,
  30. FileSkipSetting,
  31. IntroducedSyntaxErrors,
  32. )
  33. from .format import ask_whether_to_apply_changes_to_file, create_terminal_printer, show_unified_diff
  34. from .io import Empty, File
  35. from .place import module as place_module # noqa: F401
  36. from .place import module_with_reason as place_module_with_reason # noqa: F401
  37. from .settings import CYTHON_EXTENSIONS, DEFAULT_CONFIG, Config
  38. class ImportKey(Enum):
  39. """Defines how to key an individual import, generally for deduping.
  40. Import keys are defined from less to more specific:
  41. from x.y import z as a
  42. ______| | | |
  43. | | | |
  44. PACKAGE | | |
  45. ________| | |
  46. | | |
  47. MODULE | |
  48. _________________| |
  49. | |
  50. ATTRIBUTE |
  51. ______________________|
  52. |
  53. ALIAS
  54. """
  55. PACKAGE = 1
  56. MODULE = 2
  57. ATTRIBUTE = 3
  58. ALIAS = 4
  59. def sort_code_string(
  60. code: str,
  61. extension: Optional[str] = None,
  62. config: Config = DEFAULT_CONFIG,
  63. file_path: Optional[Path] = None,
  64. disregard_skip: bool = False,
  65. show_diff: Union[bool, TextIO] = False,
  66. **config_kwargs: Any,
  67. ) -> str:
  68. """Sorts any imports within the provided code string, returning a new string with them sorted.
  69. - **code**: The string of code with imports that need to be sorted.
  70. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  71. - **config**: The config object to use when sorting imports.
  72. - **file_path**: The disk location where the code string was pulled from.
  73. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  74. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  75. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  76. - ****config_kwargs**: Any config modifications.
  77. """
  78. input_stream = StringIO(code)
  79. output_stream = StringIO()
  80. config = _config(path=file_path, config=config, **config_kwargs)
  81. sort_stream(
  82. input_stream,
  83. output_stream,
  84. extension=extension,
  85. config=config,
  86. file_path=file_path,
  87. disregard_skip=disregard_skip,
  88. show_diff=show_diff,
  89. )
  90. output_stream.seek(0)
  91. return output_stream.read()
  92. def check_code_string(
  93. code: str,
  94. show_diff: Union[bool, TextIO] = False,
  95. extension: Optional[str] = None,
  96. config: Config = DEFAULT_CONFIG,
  97. file_path: Optional[Path] = None,
  98. disregard_skip: bool = False,
  99. **config_kwargs: Any,
  100. ) -> bool:
  101. """Checks the order, format, and categorization of imports within the provided code string.
  102. Returns `True` if everything is correct, otherwise `False`.
  103. - **code**: The string of code with imports that need to be sorted.
  104. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  105. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  106. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  107. - **config**: The config object to use when sorting imports.
  108. - **file_path**: The disk location where the code string was pulled from.
  109. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  110. - ****config_kwargs**: Any config modifications.
  111. """
  112. config = _config(path=file_path, config=config, **config_kwargs)
  113. return check_stream(
  114. StringIO(code),
  115. show_diff=show_diff,
  116. extension=extension,
  117. config=config,
  118. file_path=file_path,
  119. disregard_skip=disregard_skip,
  120. )
  121. def sort_stream(
  122. input_stream: TextIO,
  123. output_stream: TextIO,
  124. extension: Optional[str] = None,
  125. config: Config = DEFAULT_CONFIG,
  126. file_path: Optional[Path] = None,
  127. disregard_skip: bool = False,
  128. show_diff: Union[bool, TextIO] = False,
  129. raise_on_skip: bool = True,
  130. **config_kwargs: Any,
  131. ) -> bool:
  132. """Sorts any imports within the provided code stream, outputs to the provided output stream.
  133. Returns `True` if anything is modified from the original input stream, otherwise `False`.
  134. - **input_stream**: The stream of code with imports that need to be sorted.
  135. - **output_stream**: The stream where sorted imports should be written to.
  136. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  137. - **config**: The config object to use when sorting imports.
  138. - **file_path**: The disk location where the code string was pulled from.
  139. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  140. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  141. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  142. - ****config_kwargs**: Any config modifications.
  143. """
  144. extension = extension or (file_path and file_path.suffix.lstrip(".")) or "py"
  145. if show_diff:
  146. _output_stream = StringIO()
  147. _input_stream = StringIO(input_stream.read())
  148. changed = sort_stream(
  149. input_stream=_input_stream,
  150. output_stream=_output_stream,
  151. extension=extension,
  152. config=config,
  153. file_path=file_path,
  154. disregard_skip=disregard_skip,
  155. raise_on_skip=raise_on_skip,
  156. **config_kwargs,
  157. )
  158. _output_stream.seek(0)
  159. _input_stream.seek(0)
  160. show_unified_diff(
  161. file_input=_input_stream.read(),
  162. file_output=_output_stream.read(),
  163. file_path=file_path,
  164. output=output_stream if show_diff is True else cast(TextIO, show_diff),
  165. color_output=config.color_output,
  166. )
  167. return changed
  168. config = _config(path=file_path, config=config, **config_kwargs)
  169. content_source = str(file_path or "Passed in content")
  170. if not disregard_skip and file_path and config.is_skipped(file_path):
  171. raise FileSkipSetting(content_source)
  172. _internal_output = output_stream
  173. if config.atomic:
  174. try:
  175. file_content = input_stream.read()
  176. compile(file_content, content_source, "exec", 0, 1)
  177. except SyntaxError:
  178. if extension not in CYTHON_EXTENSIONS:
  179. raise ExistingSyntaxErrors(content_source)
  180. if config.verbose:
  181. warn(
  182. f"{content_source} Python AST errors found but ignored due to Cython extension"
  183. )
  184. input_stream = StringIO(file_content)
  185. if not output_stream.readable():
  186. _internal_output = StringIO()
  187. try:
  188. changed = core.process(
  189. input_stream,
  190. _internal_output,
  191. extension=extension,
  192. config=config,
  193. raise_on_skip=raise_on_skip,
  194. )
  195. except FileSkipComment:
  196. raise FileSkipComment(content_source)
  197. if config.atomic:
  198. _internal_output.seek(0)
  199. try:
  200. compile(_internal_output.read(), content_source, "exec", 0, 1)
  201. _internal_output.seek(0)
  202. except SyntaxError: # pragma: no cover
  203. if extension not in CYTHON_EXTENSIONS:
  204. raise IntroducedSyntaxErrors(content_source)
  205. if config.verbose:
  206. warn(
  207. f"{content_source} Python AST errors found but ignored due to Cython extension"
  208. )
  209. if _internal_output != output_stream:
  210. output_stream.write(_internal_output.read())
  211. return changed
  212. def check_stream(
  213. input_stream: TextIO,
  214. show_diff: Union[bool, TextIO] = False,
  215. extension: Optional[str] = None,
  216. config: Config = DEFAULT_CONFIG,
  217. file_path: Optional[Path] = None,
  218. disregard_skip: bool = False,
  219. **config_kwargs: Any,
  220. ) -> bool:
  221. """Checks any imports within the provided code stream, returning `False` if any unsorted or
  222. incorrectly imports are found or `True` if no problems are identified.
  223. - **input_stream**: The stream of code with imports that need to be sorted.
  224. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  225. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  226. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  227. - **config**: The config object to use when sorting imports.
  228. - **file_path**: The disk location where the code string was pulled from.
  229. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  230. - ****config_kwargs**: Any config modifications.
  231. """
  232. config = _config(path=file_path, config=config, **config_kwargs)
  233. if show_diff:
  234. input_stream = StringIO(input_stream.read())
  235. changed: bool = sort_stream(
  236. input_stream=input_stream,
  237. output_stream=Empty,
  238. extension=extension,
  239. config=config,
  240. file_path=file_path,
  241. disregard_skip=disregard_skip,
  242. )
  243. printer = create_terminal_printer(
  244. color=config.color_output, error=config.format_error, success=config.format_success
  245. )
  246. if not changed:
  247. if config.verbose and not config.only_modified:
  248. printer.success(f"{file_path or ''} Everything Looks Good!")
  249. return True
  250. printer.error(f"{file_path or ''} Imports are incorrectly sorted and/or formatted.")
  251. if show_diff:
  252. output_stream = StringIO()
  253. input_stream.seek(0)
  254. file_contents = input_stream.read()
  255. sort_stream(
  256. input_stream=StringIO(file_contents),
  257. output_stream=output_stream,
  258. extension=extension,
  259. config=config,
  260. file_path=file_path,
  261. disregard_skip=disregard_skip,
  262. )
  263. output_stream.seek(0)
  264. show_unified_diff(
  265. file_input=file_contents,
  266. file_output=output_stream.read(),
  267. file_path=file_path,
  268. output=None if show_diff is True else cast(TextIO, show_diff),
  269. color_output=config.color_output,
  270. )
  271. return False
  272. def check_file(
  273. filename: Union[str, Path],
  274. show_diff: Union[bool, TextIO] = False,
  275. config: Config = DEFAULT_CONFIG,
  276. file_path: Optional[Path] = None,
  277. disregard_skip: bool = True,
  278. extension: Optional[str] = None,
  279. **config_kwargs: Any,
  280. ) -> bool:
  281. """Checks any imports within the provided file, returning `False` if any unsorted or
  282. incorrectly imports are found or `True` if no problems are identified.
  283. - **filename**: The name or Path of the file to check.
  284. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  285. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  286. - **config**: The config object to use when sorting imports.
  287. - **file_path**: The disk location where the code string was pulled from.
  288. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  289. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  290. - ****config_kwargs**: Any config modifications.
  291. """
  292. file_config: Config = config
  293. if "config_trie" in config_kwargs:
  294. config_trie = config_kwargs.pop("config_trie", None)
  295. if config_trie:
  296. config_info = config_trie.search(filename)
  297. if config.verbose:
  298. print(f"{config_info[0]} used for file {filename}")
  299. file_config = Config(**config_info[1])
  300. with io.File.read(filename) as source_file:
  301. return check_stream(
  302. source_file.stream,
  303. show_diff=show_diff,
  304. extension=extension,
  305. config=file_config,
  306. file_path=file_path or source_file.path,
  307. disregard_skip=disregard_skip,
  308. **config_kwargs,
  309. )
  310. def _tmp_file(source_file: File) -> Path:
  311. return source_file.path.with_suffix(source_file.path.suffix + ".isorted")
  312. @contextlib.contextmanager
  313. def _in_memory_output_stream_context() -> Iterator[TextIO]:
  314. yield StringIO(newline=None)
  315. @contextlib.contextmanager
  316. def _file_output_stream_context(filename: Union[str, Path], source_file: File) -> Iterator[TextIO]:
  317. tmp_file = _tmp_file(source_file)
  318. with tmp_file.open("w+", encoding=source_file.encoding, newline="") as output_stream:
  319. shutil.copymode(filename, tmp_file)
  320. yield output_stream
  321. def sort_file(
  322. filename: Union[str, Path],
  323. extension: Optional[str] = None,
  324. config: Config = DEFAULT_CONFIG,
  325. file_path: Optional[Path] = None,
  326. disregard_skip: bool = True,
  327. ask_to_apply: bool = False,
  328. show_diff: Union[bool, TextIO] = False,
  329. write_to_stdout: bool = False,
  330. output: Optional[TextIO] = None,
  331. **config_kwargs: Any,
  332. ) -> bool:
  333. """Sorts and formats any groups of imports imports within the provided file or Path.
  334. Returns `True` if the file has been changed, otherwise `False`.
  335. - **filename**: The name or Path of the file to format.
  336. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  337. - **config**: The config object to use when sorting imports.
  338. - **file_path**: The disk location where the code string was pulled from.
  339. - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
  340. - **ask_to_apply**: If `True`, prompt before applying any changes.
  341. - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
  342. TextIO stream is provided results will be written to it, otherwise no diff will be computed.
  343. - **write_to_stdout**: If `True`, write to stdout instead of the input file.
  344. - **output**: If a TextIO is provided, results will be written there rather than replacing
  345. the original file content.
  346. - ****config_kwargs**: Any config modifications.
  347. """
  348. file_config: Config = config
  349. if "config_trie" in config_kwargs:
  350. config_trie = config_kwargs.pop("config_trie", None)
  351. if config_trie:
  352. config_info = config_trie.search(filename)
  353. if config.verbose:
  354. print(f"{config_info[0]} used for file {filename}")
  355. file_config = Config(**config_info[1])
  356. with io.File.read(filename) as source_file:
  357. actual_file_path = file_path or source_file.path
  358. config = _config(path=actual_file_path, config=file_config, **config_kwargs)
  359. changed: bool = False
  360. try:
  361. if write_to_stdout:
  362. changed = sort_stream(
  363. input_stream=source_file.stream,
  364. output_stream=sys.stdout,
  365. config=config,
  366. file_path=actual_file_path,
  367. disregard_skip=disregard_skip,
  368. extension=extension,
  369. )
  370. else:
  371. if output is None:
  372. try:
  373. if config.overwrite_in_place:
  374. output_stream_context = _in_memory_output_stream_context()
  375. else:
  376. output_stream_context = _file_output_stream_context(
  377. filename, source_file
  378. )
  379. with output_stream_context as output_stream:
  380. changed = sort_stream(
  381. input_stream=source_file.stream,
  382. output_stream=output_stream,
  383. config=config,
  384. file_path=actual_file_path,
  385. disregard_skip=disregard_skip,
  386. extension=extension,
  387. )
  388. output_stream.seek(0)
  389. if changed:
  390. if show_diff or ask_to_apply:
  391. source_file.stream.seek(0)
  392. show_unified_diff(
  393. file_input=source_file.stream.read(),
  394. file_output=output_stream.read(),
  395. file_path=actual_file_path,
  396. output=None
  397. if show_diff is True
  398. else cast(TextIO, show_diff),
  399. color_output=config.color_output,
  400. )
  401. if show_diff or (
  402. ask_to_apply
  403. and not ask_whether_to_apply_changes_to_file(
  404. str(source_file.path)
  405. )
  406. ):
  407. return False
  408. source_file.stream.close()
  409. if config.overwrite_in_place:
  410. output_stream.seek(0)
  411. with source_file.path.open("w") as fs:
  412. shutil.copyfileobj(output_stream, fs)
  413. if changed:
  414. if not config.overwrite_in_place:
  415. tmp_file = _tmp_file(source_file)
  416. tmp_file.replace(source_file.path)
  417. if not config.quiet:
  418. print(f"Fixing {source_file.path}")
  419. finally:
  420. try: # Python 3.8+: use `missing_ok=True` instead of try except.
  421. if not config.overwrite_in_place: # pragma: no branch
  422. tmp_file = _tmp_file(source_file)
  423. tmp_file.unlink()
  424. except FileNotFoundError:
  425. pass # pragma: no cover
  426. else:
  427. changed = sort_stream(
  428. input_stream=source_file.stream,
  429. output_stream=output,
  430. config=config,
  431. file_path=actual_file_path,
  432. disregard_skip=disregard_skip,
  433. extension=extension,
  434. )
  435. if changed and show_diff:
  436. source_file.stream.seek(0)
  437. output.seek(0)
  438. show_unified_diff(
  439. file_input=source_file.stream.read(),
  440. file_output=output.read(),
  441. file_path=actual_file_path,
  442. output=None if show_diff is True else cast(TextIO, show_diff),
  443. color_output=config.color_output,
  444. )
  445. source_file.stream.close()
  446. except ExistingSyntaxErrors:
  447. warn(f"{actual_file_path} unable to sort due to existing syntax errors")
  448. except IntroducedSyntaxErrors: # pragma: no cover
  449. warn(f"{actual_file_path} unable to sort as isort introduces new syntax errors")
  450. return changed
  451. def find_imports_in_code(
  452. code: str,
  453. config: Config = DEFAULT_CONFIG,
  454. file_path: Optional[Path] = None,
  455. unique: Union[bool, ImportKey] = False,
  456. top_only: bool = False,
  457. **config_kwargs: Any,
  458. ) -> Iterator[identify.Import]:
  459. """Finds and returns all imports within the provided code string.
  460. - **code**: The string of code with imports that need to be sorted.
  461. - **config**: The config object to use when sorting imports.
  462. - **file_path**: The disk location where the code string was pulled from.
  463. - **unique**: If True, only the first instance of an import is returned.
  464. - **top_only**: If True, only return imports that occur before the first function or class.
  465. - ****config_kwargs**: Any config modifications.
  466. """
  467. yield from find_imports_in_stream(
  468. input_stream=StringIO(code),
  469. config=config,
  470. file_path=file_path,
  471. unique=unique,
  472. top_only=top_only,
  473. **config_kwargs,
  474. )
  475. def find_imports_in_stream(
  476. input_stream: TextIO,
  477. config: Config = DEFAULT_CONFIG,
  478. file_path: Optional[Path] = None,
  479. unique: Union[bool, ImportKey] = False,
  480. top_only: bool = False,
  481. _seen: Optional[Set[str]] = None,
  482. **config_kwargs: Any,
  483. ) -> Iterator[identify.Import]:
  484. """Finds and returns all imports within the provided code stream.
  485. - **input_stream**: The stream of code with imports that need to be sorted.
  486. - **config**: The config object to use when sorting imports.
  487. - **file_path**: The disk location where the code string was pulled from.
  488. - **unique**: If True, only the first instance of an import is returned.
  489. - **top_only**: If True, only return imports that occur before the first function or class.
  490. - **_seen**: An optional set of imports already seen. Generally meant only for internal use.
  491. - ****config_kwargs**: Any config modifications.
  492. """
  493. config = _config(config=config, **config_kwargs)
  494. identified_imports = identify.imports(
  495. input_stream, config=config, file_path=file_path, top_only=top_only
  496. )
  497. if not unique:
  498. yield from identified_imports
  499. seen: Set[str] = set() if _seen is None else _seen
  500. for identified_import in identified_imports:
  501. if unique in (True, ImportKey.ALIAS):
  502. key = identified_import.statement()
  503. elif unique == ImportKey.ATTRIBUTE:
  504. key = f"{identified_import.module}.{identified_import.attribute}"
  505. elif unique == ImportKey.MODULE:
  506. key = identified_import.module
  507. elif unique == ImportKey.PACKAGE: # pragma: no branch # type checking ensures this
  508. key = identified_import.module.split(".")[0]
  509. if key and key not in seen:
  510. seen.add(key)
  511. yield identified_import
  512. def find_imports_in_file(
  513. filename: Union[str, Path],
  514. config: Config = DEFAULT_CONFIG,
  515. file_path: Optional[Path] = None,
  516. unique: Union[bool, ImportKey] = False,
  517. top_only: bool = False,
  518. **config_kwargs: Any,
  519. ) -> Iterator[identify.Import]:
  520. """Finds and returns all imports within the provided source file.
  521. - **filename**: The name or Path of the file to look for imports in.
  522. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  523. - **config**: The config object to use when sorting imports.
  524. - **file_path**: The disk location where the code string was pulled from.
  525. - **unique**: If True, only the first instance of an import is returned.
  526. - **top_only**: If True, only return imports that occur before the first function or class.
  527. - ****config_kwargs**: Any config modifications.
  528. """
  529. with io.File.read(filename) as source_file:
  530. yield from find_imports_in_stream(
  531. input_stream=source_file.stream,
  532. config=config,
  533. file_path=file_path or source_file.path,
  534. unique=unique,
  535. top_only=top_only,
  536. **config_kwargs,
  537. )
  538. def find_imports_in_paths(
  539. paths: Iterator[Union[str, Path]],
  540. config: Config = DEFAULT_CONFIG,
  541. file_path: Optional[Path] = None,
  542. unique: Union[bool, ImportKey] = False,
  543. top_only: bool = False,
  544. **config_kwargs: Any,
  545. ) -> Iterator[identify.Import]:
  546. """Finds and returns all imports within the provided source paths.
  547. - **paths**: A collection of paths to recursively look for imports within.
  548. - **extension**: The file extension that contains imports. Defaults to filename extension or py.
  549. - **config**: The config object to use when sorting imports.
  550. - **file_path**: The disk location where the code string was pulled from.
  551. - **unique**: If True, only the first instance of an import is returned.
  552. - **top_only**: If True, only return imports that occur before the first function or class.
  553. - ****config_kwargs**: Any config modifications.
  554. """
  555. config = _config(config=config, **config_kwargs)
  556. seen: Optional[Set[str]] = set() if unique else None
  557. yield from chain(
  558. *(
  559. find_imports_in_file(
  560. file_name, unique=unique, config=config, top_only=top_only, _seen=seen
  561. )
  562. for file_name in files.find(map(str, paths), config, [], [])
  563. )
  564. )
  565. def _config(
  566. path: Optional[Path] = None, config: Config = DEFAULT_CONFIG, **config_kwargs: Any
  567. ) -> Config:
  568. if path and (
  569. config is DEFAULT_CONFIG
  570. and "settings_path" not in config_kwargs
  571. and "settings_file" not in config_kwargs
  572. ):
  573. config_kwargs["settings_path"] = path
  574. if config_kwargs:
  575. if config is not DEFAULT_CONFIG:
  576. raise ValueError(
  577. "You can either specify custom configuration options using kwargs or "
  578. "passing in a Config object. Not Both!"
  579. )
  580. config = Config(**config_kwargs)
  581. return config