Merge remote-tracking branch 'origin/dev-2.x' into dev-2.x

This commit is contained in:
Alex Root Junior 2019-01-13 00:13:13 +02:00
commit 718a663036
2 changed files with 12 additions and 4 deletions

View file

@ -6,7 +6,7 @@ from typing import Any, Dict, Iterable, Optional, Union
from aiogram import types from aiogram import types
from aiogram.dispatcher.filters.filters import BoundFilter, Filter from aiogram.dispatcher.filters.filters import BoundFilter, Filter
from aiogram.types import CallbackQuery, Message from aiogram.types import CallbackQuery, Message, InlineQuery
from aiogram.utils.deprecated import warn_deprecated from aiogram.utils.deprecated import warn_deprecated
@ -159,11 +159,13 @@ class Text(Filter):
elif 'text_endswith' in full_config: elif 'text_endswith' in full_config:
return {'endswith': full_config.pop('text_endswith')} return {'endswith': full_config.pop('text_endswith')}
async def check(self, obj: Union[Message, CallbackQuery]): async def check(self, obj: Union[Message, CallbackQuery, InlineQuery]):
if isinstance(obj, Message): if isinstance(obj, Message):
text = obj.text or obj.caption or '' text = obj.text or obj.caption or ''
elif isinstance(obj, CallbackQuery): elif isinstance(obj, CallbackQuery):
text = obj.data text = obj.data
elif isinstance(obj, InlineQuery):
text = obj.query
else: else:
return False return False

View file

@ -1,4 +1,7 @@
import contextvars import contextvars
from typing import TypeVar, Type
__all__ = ('DataMixin', 'ContextInstanceMixin')
class DataMixin: class DataMixin:
@ -23,19 +26,22 @@ class DataMixin:
return self.data.get(key, default) return self.data.get(key, default)
T = TypeVar('T')
class ContextInstanceMixin: class ContextInstanceMixin:
def __init_subclass__(cls, **kwargs): def __init_subclass__(cls, **kwargs):
cls.__context_instance = contextvars.ContextVar('instance_' + cls.__name__) cls.__context_instance = contextvars.ContextVar('instance_' + cls.__name__)
return cls return cls
@classmethod @classmethod
def get_current(cls, no_error=True): def get_current(cls: Type[T], no_error=True) -> T:
if no_error: if no_error:
return cls.__context_instance.get(None) return cls.__context_instance.get(None)
return cls.__context_instance.get() return cls.__context_instance.get()
@classmethod @classmethod
def set_current(cls, value): def set_current(cls: Type[T], value: T):
if not isinstance(value, cls): if not isinstance(value, cls):
raise TypeError(f"Value should be instance of '{cls.__name__}' not '{type(value).__name__}'") raise TypeError(f"Value should be instance of '{cls.__name__}' not '{type(value).__name__}'")
cls.__context_instance.set(value) cls.__context_instance.set(value)