Skip to content

Commit 64c7e38

Browse files
committed
Rename compute_md5_hash, explain why we don't use hash(...)
1 parent 8e448f9 commit 64c7e38

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
@@ -785,7 +785,10 @@ def random_string() -> str:
785785
return binascii.hexlify(os.urandom(8)).decode('ascii')
786786

787787

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

791794

@@ -828,7 +831,7 @@ def write_cache(id: str, path: str, tree: MypyFile,
828831
# Serialize data and analyze interface
829832
data = tree.serialize()
830833
data_str = json.dumps(data, indent=2, sort_keys=True)
831-
interface_hash = compute_md5_hash(data_str)
834+
interface_hash = compute_hash(data_str)
832835

833836
# Write data cache file, if applicable
834837
if old_interface_hash == interface_hash:

0 commit comments

Comments
 (0)