changeset 48963:968b29a5a7fc

check-py3-compat: drop support for Python 2 We no longer support Python 2 so we can drop support for linting code for Python 2 support. This gets rid of the check for `from __future__`, enabling us to delete those imports. Differential Revision: https://phab.mercurial-scm.org/D12251
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 20 Feb 2022 14:52:40 -0700
parents 79009cca491e
children 5aafc3c5bdec
files contrib/check-py3-compat.py
diffstat 1 files changed, 10 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/check-py3-compat.py	Thu Mar 03 07:59:42 2022 -0800
+++ b/contrib/check-py3-compat.py	Sun Feb 20 14:52:40 2022 -0700
@@ -17,31 +17,6 @@
 import warnings
 
 
-def check_compat_py2(f):
-    """Check Python 3 compatibility for a file with Python 2"""
-    with open(f, 'rb') as fh:
-        content = fh.read()
-    root = ast.parse(content)
-
-    # Ignore empty files.
-    if not root.body:
-        return
-
-    futures = set()
-    haveprint = False
-    for node in ast.walk(root):
-        if isinstance(node, ast.ImportFrom):
-            if node.module == '__future__':
-                futures |= {n.name for n in node.names}
-        elif isinstance(node, ast.Print):
-            haveprint = True
-
-    if 'absolute_import' not in futures:
-        print('%s not using absolute_import' % f)
-    if haveprint and 'print_function' not in futures:
-        print('%s requires print_function' % f)
-
-
 def check_compat_py3(f):
     """Check Python 3 compatibility of a file with Python 3."""
     with open(f, 'rb') as fh:
@@ -94,23 +69,19 @@
 
 
 if __name__ == '__main__':
-    if sys.version_info[0] == 2:
-        fn = check_compat_py2
-    else:
-        # check_compat_py3 will import every filename we specify as long as it
-        # starts with one of a few prefixes. It does this by converting
-        # specified filenames like 'mercurial/foo.py' to 'mercurial.foo' and
-        # importing that. When running standalone (not as part of a test), this
-        # means we actually import the installed versions, not the files we just
-        # specified. When running as test-check-py3-compat.t, we technically
-        # would import the correct paths, but it's cleaner to have both cases
-        # use the same import logic.
-        sys.path.insert(0, '.')
-        fn = check_compat_py3
+    # check_compat_py3 will import every filename we specify as long as it
+    # starts with one of a few prefixes. It does this by converting
+    # specified filenames like 'mercurial/foo.py' to 'mercurial.foo' and
+    # importing that. When running standalone (not as part of a test), this
+    # means we actually import the installed versions, not the files we just
+    # specified. When running as test-check-py3-compat.t, we technically
+    # would import the correct paths, but it's cleaner to have both cases
+    # use the same import logic.
+    sys.path.insert(0, '.')
 
     for f in sys.argv[1:]:
         with warnings.catch_warnings(record=True) as warns:
-            fn(f)
+            check_compat_py3(f)
 
         for w in warns:
             print(