Skip to content

Commit aaf0362

Browse files
committed
Rename compute_md5_hash, explain why we don't use hash(...)
1 parent cdd6150 commit aaf0362

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

mypy/build.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,10 @@ def random_string() -> str:
794794
return binascii.hexlify(os.urandom(8)).decode('ascii')
795795

796796

797-
def compute_md5_hash(text: str) -> str:
797+
def compute_hash(text: str) -> str:
798+
# We use md5 instead of the builtin hash(...) function because the output of hash(...)
799+
# can differ between runs due to hash randomization (enabled by default in Python 3.3).
800+
# See the note in https://docs.python.org/3/reference/datamodel.html#object.__hash__.
798801
return hashlib.md5(text.encode('utf-8')).hexdigest()
799802

800803

@@ -837,7 +840,7 @@ def write_cache(id: str, path: str, tree: MypyFile,
837840
# Serialize data and analyze interface
838841
data = tree.serialize()
839842
data_str = json.dumps(data, indent=2, sort_keys=True)
840-
interface_hash = compute_md5_hash(data_str)
843+
interface_hash = compute_hash(data_str)
841844

842845
# Write data cache file, if applicable
843846
if old_interface_hash == interface_hash:

0 commit comments

Comments
 (0)