From fe845ffcbae1f85e14a944fdf29f2acb2afd8ae4 Mon Sep 17 00:00:00 2001 From: Andrey Anshin Date: Wed, 28 Feb 2024 13:40:57 +0400 Subject: [PATCH] Fix hash caching in `ObjectStoragePath` --- 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 d65d837e7e5db..bbe63be5e9b4d 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 @@ -152,9 +151,10 @@ def __new__( 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 deb8d412cc700..b631d2ba780c6 100644 --- a/tests/io/test_path.py +++ b/tests/io/test_path.py @@ -318,3 +318,12 @@ def test_dataset(self): o = ObjectStoragePath(i) assert o.protocol == p assert o.path == f + + 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