changeset 48868:a52f5bfc9358

import-checker: assume absolute and use modern import checker Since we require Python 3 now, we can assume we always use absolute imports and the modern import checker should be used. Differential Revision: https://phab.mercurial-scm.org/D12246
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 20 Feb 2022 13:43:44 -0700
parents f547a5dee234
children 57b58413dad1
files contrib/import-checker.py
diffstat 1 files changed, 3 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/import-checker.py	Sun Feb 20 13:29:47 2022 -0700
+++ b/contrib/import-checker.py	Sun Feb 20 13:43:44 2022 -0700
@@ -59,21 +59,6 @@
 }
 
 
-def usingabsolute(root):
-    """Whether absolute imports are being used."""
-    if sys.version_info[0] >= 3:
-        return True
-
-    for node in ast.walk(root):
-        if isinstance(node, ast.ImportFrom):
-            if node.module == '__future__':
-                for n in node.names:
-                    if n.name == 'absolute_import':
-                        return True
-
-    return False
-
-
 def walklocal(root):
     """Recursively yield all descendant nodes but not in a different scope"""
     todo = collections.deque(ast.iter_child_nodes(root))
@@ -403,21 +388,10 @@
 
 
 def verify_import_convention(module, source, localmods):
-    """Verify imports match our established coding convention.
-
-    We have 2 conventions: legacy and modern. The modern convention is in
-    effect when using absolute imports.
+    """Verify imports match our established coding convention."""
+    root = ast.parse(source)
 
-    The legacy convention only looks for mixed imports. The modern convention
-    is much more thorough.
-    """
-    root = ast.parse(source)
-    absolute = usingabsolute(root)
-
-    if absolute:
-        return verify_modern_convention(module, root, localmods)
-    else:
-        return verify_stdlib_on_own_line(root)
+    return verify_modern_convention(module, root, localmods)
 
 
 def verify_modern_convention(module, root, localmods, root_col_offset=0):
@@ -618,33 +592,6 @@
                     )
 
 
-def verify_stdlib_on_own_line(root):
-    """Given some python source, verify that stdlib imports are done
-    in separate statements from relative local module imports.
-
-    >>> list(verify_stdlib_on_own_line(ast.parse('import sys, foo')))
-    [('mixed imports\\n   stdlib:    sys\\n   relative:  foo', 1)]
-    >>> list(verify_stdlib_on_own_line(ast.parse('import sys, os')))
-    []
-    >>> list(verify_stdlib_on_own_line(ast.parse('import foo, bar')))
-    []
-    """
-    for node in ast.walk(root):
-        if isinstance(node, ast.Import):
-            from_stdlib = {False: [], True: []}
-            for n in node.names:
-                from_stdlib[n.name in stdlib_modules].append(n.name)
-            if from_stdlib[True] and from_stdlib[False]:
-                yield (
-                    'mixed imports\n   stdlib:    %s\n   relative:  %s'
-                    % (
-                        ', '.join(sorted(from_stdlib[True])),
-                        ', '.join(sorted(from_stdlib[False])),
-                    ),
-                    node.lineno,
-                )
-
-
 class CircularImport(Exception):
     pass