Add --print-modified to print modified files (#1054)
Prints modified files when in place mode is used. This helps debugging
formatting issues and makes developers more aware of the changes made by
yapf.
diff --git a/CHANGELOG b/CHANGELOG
index a76e19a..ec69b5c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -5,6 +5,8 @@
## [0.40.0] UNRELEASED
### Added
- Support for Python 3.11
+- Add the `--print-modified` flag to print out file names of modified files when
+ running in in-place mode.
### Removed
- Support for Python <3.7
diff --git a/README.rst b/README.rst
index c4c56fb..8b6e781 100644
--- a/README.rst
+++ b/README.rst
@@ -116,6 +116,7 @@
--no-local-style don't search for local style definition
-p, --parallel run YAPF in parallel when formatting multiple
files. Requires concurrent.futures in Python 2.X
+ -m, --print-modified print out file names of modified files
-vv, --verbose print out file names while processing
diff --git a/yapf/__init__.py b/yapf/__init__.py
index aa5c37b..ce183e9 100644
--- a/yapf/__init__.py
+++ b/yapf/__init__.py
@@ -145,7 +145,8 @@
verify=args.verify,
parallel=args.parallel,
quiet=args.quiet,
- verbose=args.verbose)
+ verbose=args.verbose,
+ print_modified=args.print_modified)
return 1 if changed and (args.diff or args.quiet) else 0
@@ -175,7 +176,8 @@
verify=False,
parallel=False,
quiet=False,
- verbose=False):
+ verbose=False,
+ print_modified=False):
"""Format a list of files.
Arguments:
@@ -194,6 +196,7 @@
parallel: (bool) True if should format multiple files in parallel.
quiet: (bool) True if should output nothing.
verbose: (bool) True if should print out filenames while processing.
+ print_modified: (bool) True if should print out filenames of modified files.
Returns:
True if the source code changed in any of the files being formatted.
@@ -207,14 +210,15 @@
future_formats = [
executor.submit(_FormatFile, filename, lines, style_config,
no_local_style, in_place, print_diff, verify, quiet,
- verbose) for filename in filenames
+ verbose, print_modified) for filename in filenames
]
for future in concurrent.futures.as_completed(future_formats):
changed |= future.result()
else:
for filename in filenames:
changed |= _FormatFile(filename, lines, style_config, no_local_style,
- in_place, print_diff, verify, quiet, verbose)
+ in_place, print_diff, verify, quiet, verbose,
+ print_modified)
return changed
@@ -226,10 +230,11 @@
print_diff=False,
verify=False,
quiet=False,
- verbose=False):
+ verbose=False,
+ print_modified=False):
"""Format an individual file."""
if verbose and not quiet:
- print('Reformatting %s' % filename)
+ print(f'Reformatting {filename}')
if style_config is None and not no_local_style:
style_config = file_resources.GetDefaultStyleForDir(
@@ -252,6 +257,8 @@
if not in_place and not quiet and reformatted_code:
file_resources.WriteReformattedCode(filename, reformatted_code, encoding,
in_place)
+ if print_modified and has_change and in_place and not quiet:
+ print(f'Formatted {filename}')
return has_change
@@ -359,6 +366,11 @@
help=('run YAPF in parallel when formatting multiple files. Requires '
'concurrent.futures in Python 2.X'))
parser.add_argument(
+ '-m',
+ '--print-modified',
+ action='store_true',
+ help='print out file names of modified files')
+ parser.add_argument(
'-vv',
'--verbose',
action='store_true',
diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py
index 09659e7..7c241bd 100644
--- a/yapftests/yapf_test.py
+++ b/yapftests/yapf_test.py
@@ -505,6 +505,16 @@
reformatted_code = fd.read()
self.assertEqual(reformatted_code, expected_formatted_code)
+ def testPrintModified(self):
+ for unformatted_code, has_change in [('1==2', True), ('1 == 2', False)]:
+ with utils.TempFileContents(
+ self.test_tmpdir, unformatted_code, suffix='.py') as filepath:
+ output = subprocess.check_output(
+ YAPF_BINARY + ['--in-place', '--print-modified', filepath],
+ text=True)
+ check = self.assertIn if has_change else self.assertNotIn
+ check(f'Formatted {filepath}', output)
+
def testReadFromStdin(self):
unformatted_code = textwrap.dedent("""\
def foo():