##// END OF EJS Templates
import-checker: extend check of symbol-import order to all local modules...
Yuya Nishihara -
r29208:cba8bc11 default
parent child Browse files
Show More
@@ -370,7 +370,7 b' def verify_modern_convention(module, roo'
370 * Symbols can only be imported from specific modules (see
370 * Symbols can only be imported from specific modules (see
371 `allowsymbolimports`). For other modules, first import the module then
371 `allowsymbolimports`). For other modules, first import the module then
372 assign the symbol to a module-level variable. In addition, these imports
372 assign the symbol to a module-level variable. In addition, these imports
373 must be performed before other relative imports. This rule only
373 must be performed before other local imports. This rule only
374 applies to import statements outside of any blocks.
374 applies to import statements outside of any blocks.
375 * Relative imports from the standard library are not allowed.
375 * Relative imports from the standard library are not allowed.
376 * Certain modules must be aliased to alternate names to avoid aliasing
376 * Certain modules must be aliased to alternate names to avoid aliasing
@@ -381,8 +381,8 b' def verify_modern_convention(module, roo'
381
381
382 # Whether a local/non-stdlib import has been performed.
382 # Whether a local/non-stdlib import has been performed.
383 seenlocal = None
383 seenlocal = None
384 # Whether a relative, non-symbol import has been seen.
384 # Whether a local/non-stdlib, non-symbol import has been seen.
385 seennonsymbolrelative = False
385 seennonsymbollocal = False
386 # The last name to be imported (for sorting).
386 # The last name to be imported (for sorting).
387 lastname = None
387 lastname = None
388 # Relative import levels encountered so far.
388 # Relative import levels encountered so far.
@@ -468,13 +468,14 b' def verify_modern_convention(module, roo'
468 yield msg('direct symbol import %s from %s',
468 yield msg('direct symbol import %s from %s',
469 ', '.join(symbols), fullname)
469 ', '.join(symbols), fullname)
470
470
471 if symbols and seennonsymbolrelative:
471 if symbols and seennonsymbollocal:
472 yield msg('symbol import follows non-symbol import: %s',
472 yield msg('symbol import follows non-symbol import: %s',
473 fullname)
473 fullname)
474 if not symbols and fullname not in stdlib_modules:
475 seennonsymbollocal = True
474
476
475 if not node.module:
477 if not node.module:
476 assert node.level
478 assert node.level
477 seennonsymbolrelative = True
478
479
479 # Only allow 1 group per level.
480 # Only allow 1 group per level.
480 if (node.level in seenlevels
481 if (node.level in seenlevels
@@ -114,7 +114,16 b' Run additional tests for the import chec'
114 > from testpackage.unsorted import foo
114 > from testpackage.unsorted import foo
115 > EOF
115 > EOF
116
116
117 $ python "$import_checker" testpackage/*.py testpackage/subpackage/*.py
117 $ mkdir testpackage2
118 $ touch testpackage2/__init__.py
119
120 $ cat > testpackage2/latesymbolimport.py << EOF
121 > from __future__ import absolute_import
122 > from testpackage import unsorted
123 > from mercurial.node import hex
124 > EOF
125
126 $ python "$import_checker" testpackage*/*.py testpackage/subpackage/*.py
118 testpackage/importalias.py:2: ui module must be "as" aliased to uimod
127 testpackage/importalias.py:2: ui module must be "as" aliased to uimod
119 testpackage/importfromalias.py:2: ui from testpackage must be "as" aliased to uimod
128 testpackage/importfromalias.py:2: ui from testpackage must be "as" aliased to uimod
120 testpackage/importfromrelative.py:2: import should be relative: testpackage.unsorted
129 testpackage/importfromrelative.py:2: import should be relative: testpackage.unsorted
@@ -132,6 +141,7 b' Run additional tests for the import chec'
132 testpackage/subpackage/localimport.py:8: import should be relative: testpackage.subpackage.levelpriority
141 testpackage/subpackage/localimport.py:8: import should be relative: testpackage.subpackage.levelpriority
133 testpackage/symbolimport.py:2: direct symbol import foo from testpackage.unsorted
142 testpackage/symbolimport.py:2: direct symbol import foo from testpackage.unsorted
134 testpackage/unsorted.py:3: imports not lexically sorted: os < sys
143 testpackage/unsorted.py:3: imports not lexically sorted: os < sys
144 testpackage2/latesymbolimport.py:3: symbol import follows non-symbol import: mercurial.node
135 [1]
145 [1]
136
146
137 $ cd "$TESTDIR"/..
147 $ cd "$TESTDIR"/..
General Comments 0
You need to be logged in to leave comments. Login now