From c41887420d7de34c7399161199686021d5e5bc7a Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 24 Oct 2021 15:45:13 +0200 Subject: [PATCH 1/2] Drop `__dict__` from `Dispatcher.__slots__` --- telegram/ext/_dispatcher.py | 1 - tests/conftest.py | 6 +++++- tests/test_dispatcher.py | 9 +++++---- tests/test_slots.py | 1 - 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/telegram/ext/_dispatcher.py b/telegram/ext/_dispatcher.py index 6ad1ec3dd12..e5b48acebd0 100644 --- a/telegram/ext/_dispatcher.py +++ b/telegram/ext/_dispatcher.py @@ -157,7 +157,6 @@ class Dispatcher(Generic[BT, CCT, UD, CD, BD, JQ, PT]): '__async_queue', '__async_threads', 'bot', - '__dict__', '__weakref__', 'context_types', ) diff --git a/tests/conftest.py b/tests/conftest.py index e92937362c7..482d4282b70 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -110,6 +110,10 @@ class DictBot(Bot): pass +class DictDispatcher(Dispatcher): + pass + + @pytest.fixture(scope='session') def bot(bot_info): return DictExtBot(bot_info['token'], private_key=PRIVATE_KEY, request=DictRequest(8)) @@ -170,7 +174,7 @@ def provider_token(bot_info): def create_dp(bot): # Dispatcher is heavy to init (due to many threads and such) so we have a single session # scoped one here, but before each test, reset it (dp fixture below) - dispatcher = DispatcherBuilder().bot(bot).workers(2).build() + dispatcher = DispatcherBuilder().bot(bot).workers(2).dispatcher_class(DictDispatcher).build() thr = Thread(target=dispatcher.start) thr.start() sleep(2) diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index b759421be30..b04c8171bb7 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -101,11 +101,12 @@ def callback_context(self, update, context): ): self.received = context.error.message - def test_slot_behaviour(self, dp2, mro_slots): - for at in dp2.__slots__: + def test_slot_behaviour(self, bot, mro_slots): + dp = DispatcherBuilder().bot(bot).build() + for at in dp.__slots__: at = f"_Dispatcher{at}" if at.startswith('__') and not at.endswith('__') else at - assert getattr(dp2, at, 'err') != 'err', f"got extra slot '{at}'" - assert len(mro_slots(dp2)) == len(set(mro_slots(dp2))), "duplicate slot" + assert getattr(dp, at, 'err') != 'err', f"got extra slot '{at}'" + assert len(mro_slots(dp)) == len(set(mro_slots(dp))), "duplicate slot" def test_manual_init_warning(self, recwarn): Dispatcher( diff --git a/tests/test_slots.py b/tests/test_slots.py index adba1f8b700..885426418fe 100644 --- a/tests/test_slots.py +++ b/tests/test_slots.py @@ -27,7 +27,6 @@ included = { # These modules/classes intentionally have __dict__. 'CallbackContext', 'BasePersistence', - 'Dispatcher', } From c5128a7ad797acd5443fd377679a712999cdd86a Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 24 Oct 2021 16:20:43 +0200 Subject: [PATCH 2/2] Simplify test setup --- tests/conftest.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 482d4282b70..a2d0f378531 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -203,16 +203,11 @@ def dp(_dp): _dp.groups = [] _dp.error_handlers = {} _dp.exception_event = Event() - # For some reason if we setattr with the name mangled, then some tests(like async) run forever, - # due to threads not acquiring, (blocking). This adds these attributes to the __dict__. - object.__setattr__(_dp, '__stop_event', Event()) - object.__setattr__(_dp, '__async_queue', Queue()) - object.__setattr__(_dp, '__async_threads', set()) + _dp.__stop_event = Event() + _dp.__async_queue = Queue() + _dp.__async_threads = set() _dp.persistence = None - if _dp._Dispatcher__singleton_semaphore.acquire(blocking=0): - Dispatcher._set_singleton(_dp) yield _dp - Dispatcher._Dispatcher__singleton_semaphore.release() @pytest.fixture(scope='function')