Run the black formatter on the specified files.
(written_files, black_path=(sentinel := object()), stderr=sys.stderr)
| 164 | |
| 165 | |
| 166 | def run_formatters(written_files, black_path=(sentinel := object()), stderr=sys.stderr): |
| 167 | """ |
| 168 | Run the black formatter on the specified files. |
| 169 | """ |
| 170 | # Use a sentinel rather than None, as which() returns None when not found. |
| 171 | if black_path is sentinel: |
| 172 | black_path = shutil.which("black") |
| 173 | if black_path: |
| 174 | try: |
| 175 | subprocess.run( |
| 176 | [black_path, "--fast", "--", *written_files], |
| 177 | capture_output=True, |
| 178 | ) |
| 179 | except OSError: |
| 180 | stderr.write("Formatters failed to launch:") |
| 181 | traceback.print_exc(file=stderr) |