This is an automated email from the ASF dual-hosted git repository. jedcunningham pushed a commit to branch v2-8-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 07b32f04fe1dc5b568f954e68960ecba50a71f80 Author: Andrey Anshin <[email protected]> AuthorDate: Wed Feb 28 17:05:58 2024 +0400 Fix hash caching in `ObjectStoragePath` (#37769) (cherry picked from commit 382a9618ed15403766ec7d1bcbe41ed955b3e60d) --- airflow/io/path.py | 6 +++--- tests/io/test_path.py | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/airflow/io/path.py b/airflow/io/path.py index bd7c320653..40a9e49e52 100644 --- a/airflow/io/path.py +++ b/airflow/io/path.py @@ -17,7 +17,6 @@ from __future__ import annotations import contextlib -import functools import os import shutil import typing @@ -149,9 +148,10 @@ class ObjectStoragePath(CloudPath): return cls._from_parts(args_list, url=parsed_url, conn_id=conn_id, **kwargs) # type: ignore - @functools.lru_cache def __hash__(self) -> int: - return hash(str(self)) + if not (_hash := getattr(self, "_hash", None)): + self._hash = _hash = hash(str(self)) + return _hash def __eq__(self, other: typing.Any) -> bool: return self.samestore(other) and str(self) == str(other) diff --git a/tests/io/test_path.py b/tests/io/test_path.py index 7c97c7b2b9..7f250825ce 100644 --- a/tests/io/test_path.py +++ b/tests/io/test_path.py @@ -306,3 +306,12 @@ class TestFs: finally: # Reset the cache to avoid side effects _register_filesystems.cache_clear() + + def test_hash(self): + file_uri_1 = f"file:///tmp/{str(uuid.uuid4())}" + file_uri_2 = f"file:///tmp/{str(uuid.uuid4())}" + s = set() + for _ in range(10): + s.add(ObjectStoragePath(file_uri_1)) + s.add(ObjectStoragePath(file_uri_2)) + assert len(s) == 2
