Debian Codegraph @

README.rst

raw
1 flake8-import-order
2 ===================
3
4 |Build Status|
5
6 A `flake8 <http://flake8.readthedocs.org/en/latest/>`__ and `Pylama
7 <https://github.com/klen/pylama>`__ plugin that checks the ordering of
8 your imports. It does not check anything else about the
9 imports. Merely that they are grouped and ordered correctly.
10
11 In general stdlib comes first, then 3rd party, then local packages,
12 and that each group is individually alphabetized, however this depends
13 on the style used. Flake8-Import-Order supports a number of `styles
14 <#styles>`_ and is extensible allowing for `custom styles
15 <#extending-styles>`_.
16
17 This plugin was originally developed to match the style preferences of
18 the `cryptography <https://github.com/pyca/cryptography>`__ project,
19 with this style remaining the default.
20
21 Warnings
22 --------
23
24 This package adds 4 new flake8 warnings
25
26 - ``I100``: Your import statements are in the wrong order.
27 - ``I101``: The names in your from import are in the wrong order.
28 - ``I201``: Missing newline between import groups.
29 - ``I202``: Additional newline in a group of imports.
30
31 Styles
32 ------
33
34 The following styles are directly supported,
35
36 * ``cryptography`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_cryptography.py>`__
37 * ``google`` - style described in `Google Style Guidelines <https://google.github.io/styleguide/pyguide.html?showone=Imports_formatting#313-imports-formatting>`__, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_google.py>`__
38 * ``smarkets`` - style as ``google`` only with `import` statements before `from X import ...` statements, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_smarkets.py>`__
39 * ``appnexus`` - style as ``google`` only with `import` statements for packages local to your company or organisation coming after `import` statements for third-party packages, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_appnexus.py>`__
40 * ``edited`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_edited.py>`__
41 * ``pycharm`` - style as ``smarkets`` only with case sensitive sorting imported names
42 * ``pep8`` - style that only enforces groups without enforcing the order within the groups
43
44 You can also `add your own style <#extending-styles>`_ by extending ``Style``
45 class.
46
47 Configuration
48 -------------
49
50 You will want to set the ``application-import-names`` option to a
51 comma separated list of names that should be considered local to your
52 application. These will be used to help categorise your import
53 statements into the correct groups. Note that relative imports are
54 always considered local.
55
56 You will want to set the ``application-package-names`` option to a
57 comma separated list of names that should be considered local to your
58 company or organisation, but which are obtained using some sort of
59 package manager like Pip, Apt, or Yum. Typically, code representing
60 the values listed in this option is located in a different repository
61 than the code being developed. This option is only accepted in the
62 supported ``appnexus`` or ``edited`` styles or in any style that
63 accepts application package names.
64
65 The ``application-import-names`` and ``application-package-names`` can
66 contain namespaced packages or even exact nested module names. (This
67 is possible with 0.16 onwards).
68
69 ``import-order-style`` controls what style the plugin follows
70 (``cryptography`` is the default).
71
72 Limitations
73 -----------
74
75 Currently these checks are limited to module scope imports only.
76 Conditional imports in module scope will be ignored except imports
77 under ```if TYPE_CHECKING:``` block.
78
79 Classification of an imported module is achieved by checking the
80 module against a stdlib list and then if there is no match against the
81 ``application-import-names`` list and ``application-package-names`` if
82 the style accepts application-package names. Only if none of these
83 lists contain the imported module will it be classified as third
84 party.
85
86 These checks only consider an import against its previous import,
87 rather than considering all the imports together. This means that
88 ``I100`` errors are only raised for the latter of adjacent imports out
89 of order. For example,
90
91 .. code-block:: python
92
93 import X.B
94 import X # I100
95 import X.A
96
97 only ``import X`` raises an ``I100`` error, yet ``import X.A`` is also
98 out of order compared with the ``import X.B``.
99
100 Imported modules are classified as stdlib if the module is in a
101 vendored list of stdlib modules. This list is based on the latest
102 release of Python and hence the results can be misleading. This list
103 is also the same for all Python versions because otherwise it would
104 be impossible to write programs that work under both Python 2 and 3
105 *and* pass the import order check.
106
107 The ``I202`` check will consider any blank line between imports to
108 count, even if the line is not contextually related to the
109 imports. For example,
110
111 .. code-block:: python
112
113 import logging
114 try:
115 from logging import NullHandler
116 except ImportError:
117 class NullHandler(logging.Handler):
118 """Shim for version of Python < 2.7."""
119
120 def emit(self, record):
121 pass
122 import sys # I202 due to the blank line before the 'def emit'
123
124 will trigger a ``I202`` error despite the blank line not being
125 contextually related.
126
127 Extending styles
128 ----------------
129
130 You can add your own style by extending ``flake8_import_order.styles.Style``
131 class. Here's an example:
132
133 .. code-block:: python
134
135 from flake8_import_order.styles import Cryptography
136
137
138 class ReversedCryptography(Cryptography):
139 # Note that Cryptography is a subclass of Style.
140
141 @staticmethod
142 def sorted_names(names):
143 return reversed(Cryptography.sorted_names(names))
144
145 By default there are five import groupings or sections; future,
146 stdlib, third party, application, and relative imports. A style can
147 choose to accept another grouping, application-package, by setting the
148 ``Style`` class variable ``accepts_application_package_names`` to
149 True, e.g.
150
151 .. code-block:: python
152
153 class PackageNameCryptography(Cryptography):
154 accepts_application_package_names = True
155
156 To make flake8-import-order able to discover your extended style, you need to
157 register it as ``flake8_import_order.styles`` using setuptools' `entry points
158 <https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points>`__
159 mechanism:
160
161 .. code-block:: python
162
163 # setup.py of your style package
164 setup(
165 name='flake8-import-order-reversed-cryptography',
166 ...,
167 entry_points={
168 'flake8_import_order.styles': [
169 'reversed = reversedcryptography:ReversedCryptography',
170 # 'reversed' is a style name. You can pass it to
171 # --import-order-style option
172 # 'reversedcryptography:ReversedCryptography' is an import path
173 # of your extended style class.
174 ]
175 }
176 )
177
178 .. |Build Status| image:: https://travis-ci.org/PyCQA/flake8-import-order.svg?branch=master
179 :target: https://travis-ci.org/PyCQA/flake8-import-order