Debian Codegraph @

docs/configuration/action_comments.md

raw
1 # Action comments
2
3 The most basic way to configure the flow of isort within a single file is action comments. These comments are picked up and interpreted by the isort parser during parsing.
4
5
6 ## isort: skip_file
7
8 Tells isort to skip the entire file.
9
10 Example:
11
12 ```python
13 # !/bin/python3
14 # isort: skip_file
15 import os
16 import sys
17
18 ...
19 ```
20
21 !!! warning
22 This should be placed as high in the file as reasonably possible.
23 Since isort uses a streaming architecture, it may have already completed some work before it reaches the comment. Usually, this is okay - but can be confusing if --diff or any interactive options are used from the command line.
24
25
26 ## isort: skip
27
28 If placed on the same line as (or within the continuation of a) an import statement, isort will not sort this import.
29 More specifically, it prevents the import statement from being recognized by isort as an import. In consequence, this line will be treated as code and be pushed down to below the import section of the file.
30
31 Example:
32
33 ```python
34 import b
35 import a # isort: skip <- this will now stay below b
36 ```
37 !!! note
38 It is recommended to where possible use `# isort: off` and `# isort: on` or `# isort: split` instead as the behavior is more explicit and predictable.
39
40 ## isort: off
41
42 Turns isort parsing off. Every line after an `# isort: off` statement will be passed along unchanged until an `# isort: on` comment or the end of the file.
43
44 Example:
45
46 ```python
47 import e
48 import f
49
50 # isort: off
51
52 import b
53 import a
54 ```
55
56 ## isort: on
57
58 Turns isort parsing back on. This only makes sense if an `# isort: off` comment exists higher in the file! This allows you to have blocks of unsorted imports, around otherwise sorted ones.
59
60 Example:
61
62 ```python
63 import e
64 import f
65
66 # isort: off
67
68 import b
69 import a
70
71 # isort: on
72
73 import c
74 import d
75 ```
76
77 ## isort: split
78
79 Tells isort the current sort section is finished, and all future imports belong to a new sort grouping.
80
81 Example:
82
83 ```python
84 import e
85 import f
86
87 # isort: split
88
89 import a
90 import b
91 import c
92 import d
93 ```
94
95 You can also use it inline to keep an import from having imports above or below it swap position:
96
97 ```python
98 import c
99 import b # isort: split
100 import a
101 ```
102
103 !!! tip
104 isort split is exactly the same as placing an `# isort: on` immediately below an `# isort: off`
105
106
107 ## isort: dont-add-imports
108
109 Tells isort to not automatically add imports to this file, even if --add-imports is set.
110
111 ## isort: dont-add-import: [IMPORT_LINE]
112
113 Tells isort to not automatically add a particular import, even if --add-imports says to add it.