Repository: google/yapf

Issue:
Resolve duplicate pre-commit files?
Hi!

There are two pre-commit files in here that have identical copies as both `.yaml` and `.yml`:

```console
# ls -l .pre-commit-* | sed "s,${USER},user,g"
-rw-r--r-- 1 user user 803 Apr 19 00:29 .pre-commit-config.yaml
-rw-r--r-- 1 user user 803 Apr 19 00:29 .pre-commit-config.yml
-rw-r--r-- 1 user user 239 Apr 19 00:29 .pre-commit-hooks.yaml
-rw-r--r-- 1 user user 239 Apr 19 00:29 .pre-commit-hooks.yml

# diff -u .pre-commit-config.y{a,}ml && echo identical
identical

# diff -u .pre-commit-hooks.y{a,}ml && echo identical
identical
```

Is there a reason we need both of these still today? Also, does anyone know what it was that got us into this duplication back then?

Thanks and best, Sebastian

Discussion:

Patch:
diff --git a/.pre-commit-config.yml b/.pre-commit-config.yml
deleted file mode 100644
index 3bd65c2..0000000
--- a/.pre-commit-config.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-# File introduces automated checks triggered on git events
-# to enable run `pip install pre-commit && pre-commit install`
-
-repos:
-  - repo: local
-    hooks:
-      - id: yapf
-        name: yapf
-        language: python
-        entry: yapf
-        args: [-i, -vv]
-        types: [python]
-  - repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v3.2.0
-    hooks:
-      - id: trailing-whitespace
-      - id: check-docstring-first
-      - id: check-json
-      - id: check-added-large-files
-      - id: check-yaml
-      - id: debug-statements
-      - id: requirements-txt-fixer
-      - id: check-merge-conflict
-      - id: double-quote-string-fixer
-      - id: end-of-file-fixer
-      - id: sort-simple-yaml
-  - repo: meta
-    hooks:
-      - id: check-hooks-apply
-      - id: check-useless-excludes
diff --git a/.pre-commit-hooks.yml b/.pre-commit-hooks.yml
deleted file mode 100644
index 3eba1f2..0000000
--- a/.pre-commit-hooks.yml
+++ /dev/null
@@ -1,9 +0,0 @@
-# File configures YAPF to be used as a git hook with https://github.com/pre-commit/pre-commit
-
-- id: yapf
-  name: yapf
-  description: "A formatter for Python files."
-  entry: yapf
-  args: [-i] #inplace
-  language: python
-  types: [python]
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):