Skip to content

Commit

Permalink
Use f-strings almost everywhere, and other cleanups by applying pyupg…
Browse files Browse the repository at this point in the history
…rade (pytorch#4585)

Co-authored-by: Nicolas Hug <[email protected]>
  • Loading branch information
Borda and NicolasHug authored Oct 28, 2021
1 parent 50dfe20 commit d367a01
Show file tree
Hide file tree
Showing 136 changed files with 694 additions and 767 deletions.
27 changes: 13 additions & 14 deletions .circleci/unittest/linux/scripts/run-clang-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import argparse
import difflib
import fnmatch
import io
import multiprocessing
import os
import signal
Expand Down Expand Up @@ -87,20 +86,20 @@ def list_files(files, recursive=False, extensions=None, exclude=None):
def make_diff(file, original, reformatted):
return list(
difflib.unified_diff(
original, reformatted, fromfile="{}\t(original)".format(file), tofile="{}\t(reformatted)".format(file), n=3
original, reformatted, fromfile=f"{file}\t(original)", tofile=f"{file}\t(reformatted)", n=3
)
)


class DiffError(Exception):
def __init__(self, message, errs=None):
super(DiffError, self).__init__(message)
super().__init__(message)
self.errs = errs or []


class UnexpectedError(Exception):
def __init__(self, message, exc=None):
super(UnexpectedError, self).__init__(message)
super().__init__(message)
self.formatted_traceback = traceback.format_exc()
self.exc = exc

Expand All @@ -112,14 +111,14 @@ def run_clang_format_diff_wrapper(args, file):
except DiffError:
raise
except Exception as e:
raise UnexpectedError("{}: {}: {}".format(file, e.__class__.__name__, e), e)
raise UnexpectedError(f"{file}: {e.__class__.__name__}: {e}", e)


def run_clang_format_diff(args, file):
try:
with io.open(file, "r", encoding="utf-8") as f:
with open(file, encoding="utf-8") as f:
original = f.readlines()
except IOError as exc:
except OSError as exc:
raise DiffError(str(exc))
invocation = [args.clang_format_executable, file]

Expand All @@ -145,7 +144,7 @@ def run_clang_format_diff(args, file):
invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, encoding="utf-8"
)
except OSError as exc:
raise DiffError("Command '{}' failed to start: {}".format(subprocess.list2cmdline(invocation), exc))
raise DiffError(f"Command '{subprocess.list2cmdline(invocation)}' failed to start: {exc}")
proc_stdout = proc.stdout
proc_stderr = proc.stderr

Expand Down Expand Up @@ -203,7 +202,7 @@ def print_trouble(prog, message, use_colors):
error_text = "error:"
if use_colors:
error_text = bold_red(error_text)
print("{}: {} {}".format(prog, error_text, message), file=sys.stderr)
print(f"{prog}: {error_text} {message}", file=sys.stderr)


def main():
Expand All @@ -216,7 +215,7 @@ def main():
)
parser.add_argument(
"--extensions",
help="comma separated list of file extensions (default: {})".format(DEFAULT_EXTENSIONS),
help=f"comma separated list of file extensions (default: {DEFAULT_EXTENSIONS})",
default=DEFAULT_EXTENSIONS,
)
parser.add_argument("-r", "--recursive", action="store_true", help="run recursively over directories")
Expand All @@ -227,7 +226,7 @@ def main():
metavar="N",
type=int,
default=0,
help="run N clang-format jobs in parallel" " (default number of cpus + 1)",
help="run N clang-format jobs in parallel (default number of cpus + 1)",
)
parser.add_argument(
"--color", default="auto", choices=["auto", "always", "never"], help="show colored diff (default: auto)"
Expand All @@ -238,7 +237,7 @@ def main():
metavar="PATTERN",
action="append",
default=[],
help="exclude paths matching the given glob-like pattern(s)" " from recursive search",
help="exclude paths matching the given glob-like pattern(s) from recursive search",
)

args = parser.parse_args()
Expand All @@ -263,7 +262,7 @@ def main():
colored_stdout = sys.stdout.isatty()
colored_stderr = sys.stderr.isatty()

version_invocation = [args.clang_format_executable, str("--version")]
version_invocation = [args.clang_format_executable, "--version"]
try:
subprocess.check_call(version_invocation, stdout=DEVNULL)
except subprocess.CalledProcessError as e:
Expand All @@ -272,7 +271,7 @@ def main():
except OSError as e:
print_trouble(
parser.prog,
"Command '{}' failed to start: {}".format(subprocess.list2cmdline(version_invocation), e),
f"Command '{subprocess.list2cmdline(version_invocation)}' failed to start: {e}",
use_colors=colored_stderr,
)
return ExitStatus.TROUBLE
Expand Down
25 changes: 17 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-docstring-first
- id: check-toml
- id: check-yaml
exclude: packaging/.*
- id: end-of-file-fixer

# - repo: https://github.com/asottile/pyupgrade
# rev: v2.29.0
# hooks:
# - id: pyupgrade
# args: [--py36-plus]
# name: Upgrade code

- repo: https://github.com/omnilib/ufmt
rev: v1.3.0
hooks:
- id: ufmt
additional_dependencies:
- black == 21.9b0
- usort == 0.6.4

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
args: [--config=setup.cfg]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-docstring-first
- id: check-toml
- id: check-yaml
exclude: packaging/.*
- id: end-of-file-fixer
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# PyTorch documentation build configuration file, created by
# sphinx-quickstart on Fri Dec 23 13:31:47 2016.
Expand Down
2 changes: 1 addition & 1 deletion gallery/plot_scripted_tensor_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

import json

with open(Path('assets') / 'imagenet_class_index.json', 'r') as labels_file:
with open(Path('assets') / 'imagenet_class_index.json') as labels_file:
labels = json.load(labels_file)

for i, (pred, pred_scripted) in enumerate(zip(res, res_scripted)):
Expand Down
2 changes: 1 addition & 1 deletion gallery/plot_video_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def example_read_video(video_object, start=0, end=None, read_video=True, read_au
if end < start:
raise ValueError(
"end time should be larger than start time, got "
"start time={} and end time={}".format(start, end)
f"start time={start} and end time={end}"
)

video_frames = torch.empty(0)
Expand Down
42 changes: 18 additions & 24 deletions packaging/wheel/relocate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Helper script to package wheels and relocate binaries."""

import glob
Expand Down Expand Up @@ -157,7 +155,7 @@ def relocate_elf_library(patchelf, output_dir, output_library, binary):
rename and copy them into the wheel while updating their respective rpaths.
"""

print("Relocating {0}".format(binary))
print(f"Relocating {binary}")
binary_path = osp.join(output_library, binary)

ld_tree = lddtree(binary_path)
Expand All @@ -173,12 +171,12 @@ def relocate_elf_library(patchelf, output_dir, output_library, binary):
print(library)

if library_info["path"] is None:
print("Omitting {0}".format(library))
print(f"Omitting {library}")
continue

if library in ALLOWLIST:
# Omit glibc/gcc/system libraries
print("Omitting {0}".format(library))
print(f"Omitting {library}")
continue

parent_dependencies = binary_dependencies.get(parent, [])
Expand All @@ -201,7 +199,7 @@ def relocate_elf_library(patchelf, output_dir, output_library, binary):
if library != binary:
library_path = binary_paths[library]
new_library_path = patch_new_path(library_path, new_libraries_path)
print("{0} -> {1}".format(library, new_library_path))
print(f"{library} -> {new_library_path}")
shutil.copyfile(library_path, new_library_path)
new_names[library] = new_library_path

Expand All @@ -214,7 +212,7 @@ def relocate_elf_library(patchelf, output_dir, output_library, binary):
new_library_name = new_names[library]
for dep in library_dependencies:
new_dep = osp.basename(new_names[dep])
print("{0}: {1} -> {2}".format(library, dep, new_dep))
print(f"{library}: {dep} -> {new_dep}")
subprocess.check_output(
[patchelf, "--replace-needed", dep, new_dep, new_library_name], cwd=new_libraries_path
)
Expand All @@ -228,7 +226,7 @@ def relocate_elf_library(patchelf, output_dir, output_library, binary):
library_dependencies = binary_dependencies[binary]
for dep in library_dependencies:
new_dep = osp.basename(new_names[dep])
print("{0}: {1} -> {2}".format(binary, dep, new_dep))
print(f"{binary}: {dep} -> {new_dep}")
subprocess.check_output([patchelf, "--replace-needed", dep, new_dep, binary], cwd=output_library)

print("Update library rpath")
Expand All @@ -244,7 +242,7 @@ def relocate_dll_library(dumpbin, output_dir, output_library, binary):
Given a shared library, find the transitive closure of its dependencies,
rename and copy them into the wheel.
"""
print("Relocating {0}".format(binary))
print(f"Relocating {binary}")
binary_path = osp.join(output_library, binary)

library_dlls = find_dll_dependencies(dumpbin, binary_path)
Expand All @@ -255,18 +253,18 @@ def relocate_dll_library(dumpbin, output_dir, output_library, binary):
while binary_queue != []:
library, parent = binary_queue.pop(0)
if library in WINDOWS_ALLOWLIST or library.startswith("api-ms-win"):
print("Omitting {0}".format(library))
print(f"Omitting {library}")
continue

library_path = find_program(library)
if library_path is None:
print("{0} not found".format(library))
print(f"{library} not found")
continue

if osp.basename(osp.dirname(library_path)) == "system32":
continue

print("{0}: {1}".format(library, library_path))
print(f"{library}: {library_path}")
parent_dependencies = binary_dependencies.get(parent, [])
parent_dependencies.append(library)
binary_dependencies[parent] = parent_dependencies
Expand All @@ -284,7 +282,7 @@ def relocate_dll_library(dumpbin, output_dir, output_library, binary):
if library != binary:
library_path = binary_paths[library]
new_library_path = osp.join(package_dir, library)
print("{0} -> {1}".format(library, new_library_path))
print(f"{library} -> {new_library_path}")
shutil.copyfile(library_path, new_library_path)


Expand All @@ -300,26 +298,24 @@ def compress_wheel(output_dir, wheel, wheel_dir, wheel_name):
full_file = osp.join(root, this_file)
rel_file = osp.relpath(full_file, output_dir)
if full_file == record_file:
f.write("{0},,\n".format(rel_file))
f.write(f"{rel_file},,\n")
else:
digest, size = rehash(full_file)
f.write("{0},{1},{2}\n".format(rel_file, digest, size))
f.write(f"{rel_file},{digest},{size}\n")

print("Compressing wheel")
base_wheel_name = osp.join(wheel_dir, wheel_name)
shutil.make_archive(base_wheel_name, "zip", output_dir)
os.remove(wheel)
shutil.move("{0}.zip".format(base_wheel_name), wheel)
shutil.move(f"{base_wheel_name}.zip", wheel)
shutil.rmtree(output_dir)


def patch_linux():
# Get patchelf location
patchelf = find_program("patchelf")
if patchelf is None:
raise FileNotFoundError(
"Patchelf was not found in the system, please" " make sure that is available on the PATH."
)
raise FileNotFoundError("Patchelf was not found in the system, please make sure that is available on the PATH.")

# Find wheel
print("Finding wheels...")
Expand All @@ -338,7 +334,7 @@ def patch_linux():
print("Unzipping wheel...")
wheel_file = osp.basename(wheel)
wheel_dir = osp.dirname(wheel)
print("{0}".format(wheel_file))
print(f"{wheel_file}")
wheel_name, _ = osp.splitext(wheel_file)
unzip_file(wheel, output_dir)

Expand All @@ -355,9 +351,7 @@ def patch_win():
# Get dumpbin location
dumpbin = find_program("dumpbin")
if dumpbin is None:
raise FileNotFoundError(
"Dumpbin was not found in the system, please" " make sure that is available on the PATH."
)
raise FileNotFoundError("Dumpbin was not found in the system, please make sure that is available on the PATH.")

# Find wheel
print("Finding wheels...")
Expand All @@ -376,7 +370,7 @@ def patch_win():
print("Unzipping wheel...")
wheel_file = osp.basename(wheel)
wheel_dir = osp.dirname(wheel)
print("{0}".format(wheel_file))
print(f"{wheel_file}")
wheel_name, _ = osp.splitext(wheel_file)
unzip_file(wheel, output_dir)

Expand Down
Loading

0 comments on commit d367a01

Please sign in to comment.