Type-annotate tools/generate_torch_version (#51637)

Summary:
And add it to mypy.ini

Pull Request resolved: https://github.com/pytorch/pytorch/pull/51637

Reviewed By: janeyx99

Differential Revision: D26225123

Pulled By: malfet

fbshipit-source-id: d70d539ae58a14321e82f4592aaa44b3ce6b6358
This commit is contained in:
Nikita Shulga 2021-02-03 17:58:56 -08:00 committed by Facebook GitHub Bot
parent 50d903f19f
commit 627ec8badf
3 changed files with 11 additions and 8 deletions

View File

@ -31,7 +31,9 @@ files =
test/test_torch.py,
test/test_type_hints.py,
test/test_type_info.py,
test/test_utils.py
test/test_utils.py,
tools/generate_torch_version.py,
tools/clang_format_utils.py
# Minimum version supported - variable annotations were introduced

View File

@ -28,7 +28,7 @@ PLATFORM_TO_HASH = {
CLANG_FORMAT_DIR = os.path.join(PYTORCH_ROOT, ".clang-format-bin")
CLANG_FORMAT_PATH = os.path.join(CLANG_FORMAT_DIR, "clang-format")
def compute_file_sha1(path):
def compute_file_sha1(path: str) -> str:
"""Compute the SHA1 hash of a file and return it as a hex string."""
# If the file doesn't exist, return an empty string.
if not os.path.exists(path):
@ -90,7 +90,7 @@ def get_and_check_clang_format(verbose=False):
# If the directory doesn't exist, try to create it.
try:
os.mkdir(CLANG_FORMAT_DIR)
except os.OSError as e:
except OSError as e:
print("Unable to create directory for clang-format binary: {}".format(CLANG_FORMAT_DIR))
return False
finally:
@ -142,7 +142,7 @@ def get_and_check_clang_format(verbose=False):
# Err on the side of caution and try to delete the downloaded binary.
try:
os.unlink(CLANG_FORMAT_PATH)
except os.OSError as e:
except OSError as e:
print("Failed to delete binary: {}".format(str(e)))
print("Delete this binary as soon as possible and do not execute it!")

View File

@ -3,21 +3,22 @@ import os
import subprocess
from pathlib import Path
from distutils.util import strtobool
from typing import Optional, Union
def get_sha(pytorch_root):
def get_sha(pytorch_root: Union[str, Path]) -> str:
try:
return subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=pytorch_root).decode('ascii').strip()
except Exception:
return 'Unknown'
def get_torch_version(sha=None):
def get_torch_version(sha: Optional[str] = None) -> str:
pytorch_root = Path(__file__).parent.parent
version = open('version.txt', 'r').read().strip()
if os.getenv('PYTORCH_BUILD_VERSION'):
assert os.getenv('PYTORCH_BUILD_NUMBER') is not None
build_number = int(os.getenv('PYTORCH_BUILD_NUMBER'))
version = os.getenv('PYTORCH_BUILD_VERSION')
build_number = int(os.getenv('PYTORCH_BUILD_NUMBER', ""))
version = os.getenv('PYTORCH_BUILD_VERSION', "")
if build_number > 1:
version += '.post' + str(build_number)
elif sha != 'Unknown':