Repository: google/yapf
Pull request #1088 (2023-05-02 20:26:44)

Resolve py3compat.py
Follow-up to #1084, in reaction to https://github.com/google/yapf/pull/1084#issuecomment-1532071006

CC @bwendling


Patch:
diff --git a/yapf/__init__.py b/yapf/__init__.py
index 14c693d..39b15c9 100644
--- a/yapf/__init__.py
+++ b/yapf/__init__.py
@@ -27,19 +27,34 @@ If no filenames are specified, YAPF reads the code from stdin.
 """
 
 import argparse
+import codecs
+import io
 import logging
 import os
 import sys
 
 from yapf.yapflib import errors
 from yapf.yapflib import file_resources
-from yapf.yapflib import py3compat
 from yapf.yapflib import style
 from yapf.yapflib import yapf_api
 
 __version__ = '0.33.0'
 
 
+def _raw_input():
+  wrapper = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
+  return wrapper.buffer.raw.readall().decode('utf-8')
+
+
+def _removeBOM(source):
+  """Remove any Byte-order-Mark bytes from the beginning of a file."""
+  bom = codecs.BOM_UTF8
+  bom = bom.decode('utf-8')
+  if source.startswith(bom):
+    return source[len(bom):]
+  return source
+
+
 def main(argv):
   """Main program.
 
@@ -83,7 +98,7 @@ def main(argv):
         # user will need to hit 'Ctrl-D' more than once if they're inputting
         # the program by hand. 'raw_input' throws an EOFError exception if
         # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.
-        original_source.append(py3compat.raw_input())
+        original_source.append(_raw_input())
       except EOFError:
         break
       except KeyboardInterrupt:
@@ -93,7 +108,7 @@ def main(argv):
       style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
 
     source = [line.rstrip() for line in original_source]
-    source[0] = py3compat.removeBOM(source[0])
+    source[0] = _removeBOM(source[0])
 
     try:
       reformatted_source, _ = yapf_api.FormatCode(
diff --git a/yapf/yapflib/py3compat.py b/yapf/yapflib/py3compat.py
deleted file mode 100644
index c95d4b5..0000000
--- a/yapf/yapflib/py3compat.py
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright 2015 Google Inc. All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-"""Utilities for Python2 / Python3 compatibility."""
-
-import codecs
-import io
-import sys
-
-PY38 = sys.version_info[0] >= 3 and sys.version_info[1] >= 8
-
-
-def raw_input():
-  wrapper = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
-  return wrapper.buffer.raw.readall().decode('utf-8')
-
-
-def removeBOM(source):
-  """Remove any Byte-order-Mark bytes from the beginning of a file."""
-  bom = codecs.BOM_UTF8
-  bom = bom.decode('utf-8')
-  if source.startswith(bom):
-    return source[len(bom):]
-  return source


Test patch:
diff --git a/yapftests/main_test.py b/yapftests/main_test.py
index 5b73476..7241a02 100644
--- a/yapftests/main_test.py
+++ b/yapftests/main_test.py
@@ -20,8 +20,6 @@ import sys
 import unittest
 import yapf
 
-from yapf.yapflib import py3compat
-
 from yapftests import yapf_test_helper
 
 
@@ -79,11 +77,11 @@ def patched_input(code):
     return next(lines)
 
   try:
-    orig_raw_import = yapf.py3compat.raw_input
-    yapf.py3compat.raw_input = patch_raw_input
+    orig_raw_import = yapf._raw_input
+    yapf._raw_input = patch_raw_input
     yield
   finally:
-    yapf.py3compat.raw_input = orig_raw_import
+    yapf._raw_input = orig_raw_import
 
 
 class RunMainTest(yapf_test_helper.YAPFTest):
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index a8b2fdd..2edbc31 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -13,15 +13,17 @@
 # limitations under the License.
 """Basic tests for yapf.reformatter."""
 
+import sys
 import textwrap
 import unittest
 
-from yapf.yapflib import py3compat
 from yapf.yapflib import reformatter
 from yapf.yapflib import style
 
 from yapftests import yapf_test_helper
 
+PY38 = sys.version_info[0] >= 3 and sys.version_info[1] >= 8
+
 
 class BasicReformatterTest(yapf_test_helper.YAPFTest):
 
@@ -3162,7 +3164,7 @@ my_dict = {
     finally:
       style.SetGlobalStyle(style.CreateYapfStyle())
 
-  @unittest.skipUnless(py3compat.PY38, 'Requires Python 3.8')
+  @unittest.skipUnless(PY38, 'Requires Python 3.8')
   def testWalrus(self):
     unformatted_code = textwrap.dedent("""\
       if (x  :=  len([1]*1000)>100):