Skip to content

Commit

Permalink
Add native hashability check instead of typecheck one
Browse files Browse the repository at this point in the history
  • Loading branch information
hikariatama committed May 12, 2022
1 parent 9cd320e commit 3ed1980
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions hikka/entity_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
import logging
from telethon.hints import EntityLike
from telethon import TelegramClient
from collections import Hashable

logger = logging.getLogger(__name__)


def hashable(value):
"""Determine whether `value` can be hashed."""
try:
hash(value)
except TypeError:
return False

return True


class CacheRecord:
def __init__(
self,
hashable_entity: Hashable,
hashable_entity: "Hashable", # noqa: F821
resolved_entity: EntityLike,
):
self.entity = resolved_entity
Expand Down Expand Up @@ -40,7 +49,7 @@ def install_entity_caching(client: TelegramClient):
old = client.get_entity

async def new(entity: EntityLike):
if not isinstance(entity, Hashable):
if not hashable(entity):
hashable_entity = next(
getattr(entity, attr)
for attr in {"user_id", "channel_id", "chat_id", "id"}
Expand Down

0 comments on commit 3ed1980

Please sign in to comment.