From d3cbd2d4be1340379f01118079a77e3b0cc577f8 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Wed, 4 Dec 2024 17:58:37 +0100 Subject: [PATCH] fix: type narrowing (#28673) --- frappe/types/docref.py | 19 +++++++++++-------- pyproject.toml | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/frappe/types/docref.py b/frappe/types/docref.py index 09adc2b779..37d236f4f7 100644 --- a/frappe/types/docref.py +++ b/frappe/types/docref.py @@ -16,14 +16,17 @@ class DocRef: @override def __hash__(self: Union[type, "DocRef"]) -> int: - if isinstance(self, DocRef): - if self.name: - return hash(self.doctype + self.name) - else: - raise TypeError( - f"Only named documents can be hashed; maybe the document ({self.doctype}) is unsaved." - ) - raise TypeError("Only document instances can be hashed.") + if isinstance(self, type): + raise TypeError("Only document instances can be hashed.") + try: + name = self.name + except AttributeError: + raise TypeError("Partially instantiated document instances can't be hashed.") + if name: + return hash(self.doctype + name) + raise TypeError( + f"Only named documents can be hashed; maybe the document ({self.doctype}) is unsaved." + ) @override def __str__(self) -> str: diff --git a/pyproject.toml b/pyproject.toml index dcacda8bc4..dee710b240 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -130,7 +130,7 @@ test = [ "coverage~=6.5.0", "Faker~=18.10.1", "hypothesis~=6.77.0", - "freezegun~=1.2.2", + "freezegun~=1.5.1", "pdbpp~=0.10.3", ]