changeset 34038:bc2535238de2

import-checker: allow relative import a module being checked This would make the checker more friendly for 3rd-party code. For example, In remotefilelog/x.py, it may have: from . import shallowutils That could trigger "relative import of stdlib module" if "remotefilelog" was installed in the system. If the module being checked conflicts with the system module, it makes sense to not treat that module as system module. This patch makes it so. Differential Revision: https://phab.mercurial-scm.org/D552
author Jun Wu <quark@fb.com>
date Mon, 28 Aug 2017 13:43:25 -0700
parents 65ae54582713
children 72b23c9452d6
files contrib/import-checker.py tests/test-imports-checker.t
diffstat 2 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/import-checker.py	Thu Aug 31 11:28:59 2017 -0700
+++ b/contrib/import-checker.py	Mon Aug 28 13:43:25 2017 -0700
@@ -411,7 +411,8 @@
       assign the symbol to a module-level variable. In addition, these imports
       must be performed before other local imports. This rule only
       applies to import statements outside of any blocks.
-    * Relative imports from the standard library are not allowed.
+    * Relative imports from the standard library are not allowed, unless that
+      library is also a local module.
     * Certain modules must be aliased to alternate names to avoid aliasing
       and readability problems. See `requirealias`.
     """
@@ -493,7 +494,10 @@
             # __future__ is special since it needs to come first and use
             # symbol import.
             if fullname != '__future__':
-                if not fullname or fullname in stdlib_modules:
+                if not fullname or (
+                    fullname in stdlib_modules
+                    and fullname not in localmods
+                    and fullname + '.__init__' not in localmods):
                     yield msg('relative import of stdlib module')
                 else:
                     seenlocal = fullname
--- a/tests/test-imports-checker.t	Thu Aug 31 11:28:59 2017 -0700
+++ b/tests/test-imports-checker.t	Mon Aug 28 13:43:25 2017 -0700
@@ -125,7 +125,19 @@
   > from mercurial.node import hex
   > EOF
 
-  $ $PYTHON "$import_checker" testpackage*/*.py testpackage/subpackage/*.py
+# Shadowing a stdlib module to test "relative import of stdlib module" is
+# allowed if the module is also being checked
+
+  $ mkdir email
+  $ touch email/__init__.py
+  $ touch email/errors.py
+  $ cat > email/utils.py << EOF
+  > from __future__ import absolute_import
+  > from . import errors
+  > EOF
+
+  $ $PYTHON "$import_checker" testpackage*/*.py testpackage/subpackage/*.py \
+  >   email/*.py
   testpackage/importalias.py:2: ui module must be "as" aliased to uimod
   testpackage/importfromalias.py:2: ui from testpackage must be "as" aliased to uimod
   testpackage/importfromrelative.py:2: import should be relative: testpackage.unsorted