[PATCH] import-checker: do not enforce lexical sort accross stdlib/local boundary

Pierre-Yves David pierre-yves.david at ens-lyon.org
Thu Dec 15 15:01:06 EST 2016


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1481828208 -3600
#      Thu Dec 15 19:56:48 2016 +0100
# Node ID 74eecb93c61729802f8c2199241714feb3696c30
# Parent  182cacaa4c32330c0466b7111a75d060830783e8
import-checker: do not enforce lexical sort accross stdlib/local boundary

Before this change, you could get in a start where the checker would either
complain about importing local module before stdlib one or complain about the
local one being wrongly lexically sorted with the stdlib one.

We detect the boundary and avoid complaining about lexical sort across it.

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -391,6 +391,7 @@ def verify_modern_convention(module, roo
     seennonsymbollocal = False
     # The last name to be imported (for sorting).
     lastname = None
+    laststdlib = None
     # Relative import levels encountered so far.
     seenlevels = set()
 
@@ -412,16 +413,18 @@ def verify_modern_convention(module, roo
             name = node.names[0].name
             asname = node.names[0].asname
 
+            stdlib = name in stdlib_modules
+
             # Ignore sorting rules on imports inside blocks.
             if node.col_offset == root_col_offset:
-                if lastname and name < lastname:
+                if lastname and name < lastname and laststdlib == stdlib:
                     yield msg('imports not lexically sorted: %s < %s',
                               name, lastname)
 
-                lastname = name
+            lastname = name
+            laststdlib = stdlib
 
             # stdlib imports should be before local imports.
-            stdlib = name in stdlib_modules
             if stdlib and seenlocal and node.col_offset == root_col_offset:
                 yield msg('stdlib import "%s" follows local import: %s',
                           name, seenlocal)


More information about the Mercurial-devel mailing list