Skip to content

Drop __dict__ from Dispatcher slots #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion telegram/ext/_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class Dispatcher(Generic[BT, CCT, UD, CD, BD, JQ, PT]):
'__async_queue',
'__async_threads',
'bot',
'__dict__',
'__weakref__',
'context_types',
)
Expand Down
17 changes: 8 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -199,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')
Expand Down
9 changes: 5 additions & 4 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion tests/test_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
included = { # These modules/classes intentionally have __dict__.
'CallbackContext',
'BasePersistence',
'Dispatcher',
}


Expand Down