Skip to content

Commit 627ec8b

Browse files
malfetfacebook-github-bot
authored andcommitted
Type-annotate tools/generate_torch_version (pytorch#51637)
Summary: And add it to mypy.ini Pull Request resolved: pytorch#51637 Reviewed By: janeyx99 Differential Revision: D26225123 Pulled By: malfet fbshipit-source-id: d70d539ae58a14321e82f4592aaa44b3ce6b6358
1 parent 50d903f commit 627ec8b

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

mypy.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ files =
3131
test/test_torch.py,
3232
test/test_type_hints.py,
3333
test/test_type_info.py,
34-
test/test_utils.py
34+
test/test_utils.py,
35+
tools/generate_torch_version.py,
36+
tools/clang_format_utils.py
3537

3638

3739
# Minimum version supported - variable annotations were introduced

tools/clang_format_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
CLANG_FORMAT_DIR = os.path.join(PYTORCH_ROOT, ".clang-format-bin")
2929
CLANG_FORMAT_PATH = os.path.join(CLANG_FORMAT_DIR, "clang-format")
3030

31-
def compute_file_sha1(path):
31+
def compute_file_sha1(path: str) -> str:
3232
"""Compute the SHA1 hash of a file and return it as a hex string."""
3333
# If the file doesn't exist, return an empty string.
3434
if not os.path.exists(path):
@@ -90,7 +90,7 @@ def get_and_check_clang_format(verbose=False):
9090
# If the directory doesn't exist, try to create it.
9191
try:
9292
os.mkdir(CLANG_FORMAT_DIR)
93-
except os.OSError as e:
93+
except OSError as e:
9494
print("Unable to create directory for clang-format binary: {}".format(CLANG_FORMAT_DIR))
9595
return False
9696
finally:
@@ -142,7 +142,7 @@ def get_and_check_clang_format(verbose=False):
142142
# Err on the side of caution and try to delete the downloaded binary.
143143
try:
144144
os.unlink(CLANG_FORMAT_PATH)
145-
except os.OSError as e:
145+
except OSError as e:
146146
print("Failed to delete binary: {}".format(str(e)))
147147
print("Delete this binary as soon as possible and do not execute it!")
148148

tools/generate_torch_version.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
import subprocess
44
from pathlib import Path
55
from distutils.util import strtobool
6+
from typing import Optional, Union
67

7-
def get_sha(pytorch_root):
8+
def get_sha(pytorch_root: Union[str, Path]) -> str:
89
try:
910
return subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=pytorch_root).decode('ascii').strip()
1011
except Exception:
1112
return 'Unknown'
1213

13-
def get_torch_version(sha=None):
14+
def get_torch_version(sha: Optional[str] = None) -> str:
1415
pytorch_root = Path(__file__).parent.parent
1516
version = open('version.txt', 'r').read().strip()
1617

1718
if os.getenv('PYTORCH_BUILD_VERSION'):
1819
assert os.getenv('PYTORCH_BUILD_NUMBER') is not None
19-
build_number = int(os.getenv('PYTORCH_BUILD_NUMBER'))
20-
version = os.getenv('PYTORCH_BUILD_VERSION')
20+
build_number = int(os.getenv('PYTORCH_BUILD_NUMBER', ""))
21+
version = os.getenv('PYTORCH_BUILD_VERSION', "")
2122
if build_number > 1:
2223
version += '.post' + str(build_number)
2324
elif sha != 'Unknown':

0 commit comments

Comments
 (0)