Skip to content

Commit

Permalink
Deprecate hash function from FileDependency (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunkmann00 committed Dec 7, 2022
1 parent 51e625d commit c42f5a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/poetry/core/packages/file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hashlib
import io
import warnings

from pathlib import Path
from typing import Iterable
Expand Down Expand Up @@ -63,6 +64,11 @@ def is_file(self) -> bool:
return True

def hash(self, hash_name: str = "sha256") -> str:
warnings.warn(
"hash() is deprecated. Use poetry.utils.helpers.get_file_hash() instead.",
DeprecationWarning,
stacklevel=2,
)
h = hashlib.new(hash_name)
with self._full_path.open("rb") as fp:
for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""):
Expand Down
16 changes: 9 additions & 7 deletions tests/packages/test_file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def test_file_dependency_dir() -> None:


def test_default_hash() -> None:
path = DIST_PATH / TEST_FILE
dep = FileDependency("demo", path)
sha_256 = "72e8531e49038c5f9c4a837b088bfcb8011f4a9f76335c8f0654df6ac539b3d6"
assert dep.hash() == sha_256
with pytest.warns(DeprecationWarning):
path = DIST_PATH / TEST_FILE
dep = FileDependency("demo", path)
sha_256 = "72e8531e49038c5f9c4a837b088bfcb8011f4a9f76335c8f0654df6ac539b3d6"
assert dep.hash() == sha_256


try:
Expand Down Expand Up @@ -88,9 +89,10 @@ def test_default_hash() -> None:
],
)
def test_guaranteed_hash(hash_name: str, expected: str) -> None:
path = DIST_PATH / TEST_FILE
dep = FileDependency("demo", path)
assert dep.hash(hash_name) == expected
with pytest.warns(DeprecationWarning):
path = DIST_PATH / TEST_FILE
dep = FileDependency("demo", path)
assert dep.hash(hash_name) == expected


def _test_file_dependency_pep_508(
Expand Down

0 comments on commit c42f5a2

Please sign in to comment.