fix: type narrowing (#28673)

This commit is contained in:
David Arnold 2024-12-04 17:58:37 +01:00 committed by GitHub
parent a390992408
commit d3cbd2d4be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 9 deletions

View file

@ -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:

View file

@ -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",
]