Try to use contextvars (partial)

This commit is contained in:
Alex Root Junior 2018-06-23 17:39:24 +03:00
parent c88ee7ed52
commit 8086a120c2
7 changed files with 54 additions and 7 deletions

View file

@ -1,4 +1,7 @@
from __future__ import annotations
import typing
from contextvars import ContextVar
from .base import BaseBot, api
from .. import types
@ -30,6 +33,14 @@ class Bot(BaseBot):
if hasattr(self, '_me'):
delattr(self, '_me')
@classmethod
def current(cls) -> Bot:
"""
Return active bot instance from the current context or None
:return: Bot or None
"""
return bot.get()
async def download_file_by_id(self, file_id: base.String, destination=None,
timeout: base.Integer = 30, chunk_size: base.Integer = 65536,
seek: base.Boolean = True):
@ -1863,3 +1874,6 @@ class Bot(BaseBot):
result = await self.request(api.Methods.GET_GAME_HIGH_SCORES, payload)
return [types.GameHighScore(**gamehighscore) for gamehighscore in result]
bot: ContextVar[Bot] = ContextVar('bot_instance', default=None)