1212from .settings import FILE_SKIP_COMMENTS
1313
1414CIMPORT_IDENTIFIERS = ("cimport " , "cimport*" , "from.cimport" )
15- IMPORT_START_IDENTIFIERS = ("from " , "from.import" , "import " , "import*" , * CIMPORT_IDENTIFIERS )
15+ IMPORT_START_IDENTIFIERS = (
16+ "from " ,
17+ "from.import" ,
18+ "import " ,
19+ "import*" ,
20+ * CIMPORT_IDENTIFIERS ,
21+ )
1622DOCSTRING_INDICATORS = ('"""' , "'''" )
1723COMMENT_INDICATORS = (* DOCSTRING_INDICATORS , "'" , '"' , "#" )
1824CODE_SORT_COMMENTS = (
@@ -53,7 +59,9 @@ def process(
5359 was provided in the input_stream, otherwise `False`.
5460 """
5561 line_separator : str = config .line_ending
56- add_imports : list [str ] = [format_natural (addition ) for addition in config .add_imports ]
62+ add_imports : list [str ] = [
63+ format_natural (addition ) for addition in config .add_imports
64+ ]
5765 import_section : str = ""
5866 next_import_section : str = ""
5967 next_cimports : bool = False
@@ -88,15 +96,17 @@ def process(
8896 if line == "# isort: on\n " :
8997 isort_off = False
9098 new_input += line
91- elif line in ("# isort: split\n " , "# isort: off\n " , None ) or str (line ). endswith (
92- "# isort: split \n "
93- ):
99+ elif line in ("# isort: split\n " , "# isort: off\n " , None ) or str (
100+ line
101+ ). endswith ( "# isort: split \n " ) :
94102 if line == "# isort: off\n " :
95103 isort_off = True
96104 if current :
97105 if add_imports :
98106 add_line_separator = line_separator or "\n "
99- current += add_line_separator + add_line_separator .join (add_imports )
107+ current += add_line_separator + add_line_separator .join (
108+ add_imports
109+ )
100110 add_imports = []
101111 parsed = parse .file_contents (current , config = config )
102112 verbose_output += parsed .verbose_output
@@ -160,7 +170,10 @@ def process(
160170 stripped_line = line .strip ()
161171 if stripped_line and not line_separator :
162172 line_separator = (
163- line [len (line .rstrip ()) :].replace (" " , "" ).replace ("\t " , "" ).replace ("\f " , "" )
173+ line [len (line .rstrip ()) :]
174+ .replace (" " , "" )
175+ .replace ("\t " , "" )
176+ .replace ("\f " , "" )
164177 )
165178
166179 for file_skip_comment in FILE_SKIP_COMMENTS :
@@ -176,9 +189,9 @@ def process(
176189 elif stripped_line .startswith ("# isort: dont-add-imports" ):
177190 add_imports = []
178191 elif stripped_line .startswith ("# isort: dont-add-import:" ):
179- import_not_to_add = stripped_line .split ("# isort: dont-add-import:" , 1 )[
180- 1
181- ].strip ()
192+ import_not_to_add = stripped_line .split (
193+ "# isort: dont-add-import:" , 1
194+ )[ 1 ].strip ()
182195 add_imports = [
183196 import_to_add
184197 for import_to_add in add_imports
@@ -201,7 +214,9 @@ def process(
201214 first_comment_index_end = index - 1
202215
203216 was_in_quote = bool (in_quote )
204- if ((not stripped_line .startswith ("#" ) or in_quote ) and '"' in line ) or "'" in line :
217+ if (
218+ (not stripped_line .startswith ("#" ) or in_quote ) and '"' in line
219+ ) or "'" in line :
205220 char_index = 0
206221 if first_comment_index_start == - 1 and line .startswith (('"' , "'" )):
207222 first_comment_index_start = index
@@ -311,7 +326,9 @@ def process(
311326 line = input_stream .readline ()
312327
313328 if not line : # end of file without closing parenthesis
314- raise ExistingSyntaxErrors ("Parenthesis is not closed" )
329+ raise ExistingSyntaxErrors (
330+ "Parenthesis is not closed"
331+ )
315332
316333 stripped_line = line .strip ().split ("#" )[0 ]
317334 import_statement += line
@@ -342,7 +359,9 @@ def process(
342359 if cimport_statement != cimports or (
343360 new_indent != indent
344361 and import_section
345- and (not did_contain_imports or len (new_indent ) < len (indent ))
362+ and (
363+ not did_contain_imports or len (new_indent ) < len (indent )
364+ )
346365 ):
347366 indent = new_indent
348367 if import_section :
@@ -356,7 +375,9 @@ def process(
356375 else :
357376 if new_indent != indent :
358377 if import_section and did_contain_imports :
359- import_statement = indent + import_statement .lstrip ()
378+ import_statement = (
379+ indent + import_statement .lstrip ()
380+ )
360381 else :
361382 indent = new_indent
362383 import_section += import_statement
@@ -381,10 +402,14 @@ def process(
381402 and not was_in_quote
382403 and not import_section
383404 and not line .lstrip ().startswith (COMMENT_INDICATORS )
384- and not (line .rstrip ().endswith (DOCSTRING_INDICATORS ) and "=" not in line )
405+ and not (
406+ line .rstrip ().endswith (DOCSTRING_INDICATORS ) and "=" not in line
407+ )
385408 ):
386409 add_line_separator = line_separator or "\n "
387- import_section = add_line_separator .join (add_imports ) + add_line_separator
410+ import_section = (
411+ add_line_separator .join (add_imports ) + add_line_separator
412+ )
388413 if end_of_file and index != 0 :
389414 output_stream .write (add_line_separator )
390415 contains_imports = True
@@ -395,9 +420,15 @@ def process(
395420 next_import_section = ""
396421
397422 if import_section :
398- if add_imports and (contains_imports or not config .append_only ) and not indent :
423+ if (
424+ add_imports
425+ and (contains_imports or not config .append_only )
426+ and not indent
427+ ):
399428 import_section = (
400- line_separator .join (add_imports ) + line_separator + import_section
429+ line_separator .join (add_imports )
430+ + line_separator
431+ + import_section
401432 )
402433 contains_imports = True
403434 add_imports = []
@@ -420,7 +451,8 @@ def process(
420451
421452 if indent :
422453 import_section = "" .join (
423- line [len (indent ) :] for line in import_section .splitlines (keepends = True )
454+ line [len (indent ) :]
455+ for line in import_section .splitlines (keepends = True )
424456 )
425457
426458 parsed_content = parse .file_contents (import_section , config = config )
@@ -466,7 +498,12 @@ def process(
466498 output_stream .write (line )
467499 not_imports = False
468500
469- if stripped_line and not in_quote and not import_section and not next_import_section :
501+ if (
502+ stripped_line
503+ and not in_quote
504+ and not import_section
505+ and not next_import_section
506+ ):
470507 if stripped_line == "yield" :
471508 while not stripped_line or stripped_line == "yield" :
472509 new_line = input_stream .readline ()
@@ -501,12 +538,16 @@ def _indented_config(config: Config, indent: str) -> Config:
501538 line_length = max (config .line_length - len (indent ), 0 ),
502539 wrap_length = max (config .wrap_length - len (indent ), 0 ),
503540 lines_after_imports = 1 ,
504- import_headings = config .import_headings if config .indented_import_headings else {},
541+ import_headings = (
542+ config .import_headings if config .indented_import_headings else {}
543+ ),
505544 import_footers = config .import_footers if config .indented_import_headings else {},
506545 )
507546
508547
509- def _has_changed (before : str , after : str , line_separator : str , ignore_whitespace : bool ) -> bool :
548+ def _has_changed (
549+ before : str , after : str , line_separator : str , ignore_whitespace : bool
550+ ) -> bool :
510551 if ignore_whitespace :
511552 return (
512553 remove_whitespace (before , line_separator = line_separator ).strip ()
0 commit comments