From 3e3d77fdaff8874bdbcae32811af5de0f3602ac6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 24 Aug 2020 09:20:10 +0200 Subject: [PATCH] Implement short-circuit evaluation for filters AND and OR operations will not evaluate the second operand in case the first one is, respectively, False and True. --- pyrogram/filters.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyrogram/filters.py b/pyrogram/filters.py index 45762aee..e9b177ae 100644 --- a/pyrogram/filters.py +++ b/pyrogram/filters.py @@ -70,6 +70,10 @@ class AndFilter(Filter): client, update ) + # short circuit + if not x: + return False + if inspect.iscoroutinefunction(self.other.__call__): y = await self.other(client, update) else: @@ -97,6 +101,10 @@ class OrFilter(Filter): client, update ) + # short circuit + if x: + return True + if inspect.iscoroutinefunction(self.other.__call__): y = await self.other(client, update) else: