From 8b38f460b49f06bd5be9b6d52cb3a5fbda1e644b Mon Sep 17 00:00:00 2001 From: Semyon Pupkov Date: Sat, 11 May 2024 13:19:56 +0500 Subject: [PATCH] Optimize has_intersection func --- lib/sqlalchemy/util/_collections.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index 3d092a0223..f5edf275f5 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -401,7 +401,7 @@ def to_list(x: Any, default: Optional[List[Any]] = None) -> List[Any]: return list(x) -def has_intersection(set_, iterable): +def has_intersection(set_: Union[Set[Any], FrozenSet[Any]], iterable: Iterable[Any]) -> bool: r"""return True if any items of set\_ are present in iterable. Goes through special effort to ensure __hash__ is not called @@ -409,7 +409,7 @@ def has_intersection(set_, iterable): """ # TODO: optimize, write in C, etc. - return bool(set_.intersection([i for i in iterable if i.__hash__])) + return any(i in set_ for i in iterable if i.__hash__) def to_set(x): -- 2.47.2