Skip to content
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

hotfix(updates): CHOSEN_INLINE_RESULT is a correct API-term #415

Merged
merged 4 commits into from Sep 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions aiogram/types/update.py
Expand Up @@ -9,7 +9,7 @@
from .poll import Poll, PollAnswer
from .pre_checkout_query import PreCheckoutQuery
from .shipping_query import ShippingQuery
from ..utils import helper
from ..utils import helper, deprecated


class Update(base.TelegramObject):
Expand Down Expand Up @@ -55,9 +55,15 @@ class AllowedUpdates(helper.Helper):
CHANNEL_POST = helper.ListItem() # channel_post
EDITED_CHANNEL_POST = helper.ListItem() # edited_channel_post
INLINE_QUERY = helper.ListItem() # inline_query
CHOSEN_INLINE_QUERY = helper.ListItem() # chosen_inline_result
CHOSEN_INLINE_RESULT = helper.ListItem() # chosen_inline_result
CALLBACK_QUERY = helper.ListItem() # callback_query
SHIPPING_QUERY = helper.ListItem() # shipping_query
PRE_CHECKOUT_QUERY = helper.ListItem() # pre_checkout_query
POLL = helper.ListItem() # poll
POLL_ANSWER = helper.ListItem() # poll_answer

CHOSEN_INLINE_QUERY = deprecated.DeprecatedReadOnlyClassVar(
"`CHOSEN_INLINE_QUERY` is a deprecated value for allowed update. "
"Use `CHOSEN_INLINE_RESULT`",
new_value_getter=lambda cls: cls.CHOSEN_INLINE_RESULT,
)
35 changes: 34 additions & 1 deletion aiogram/utils/deprecated.py
Expand Up @@ -2,7 +2,7 @@
import inspect
import warnings
import functools
from typing import Callable
from typing import Callable, Generic, TypeVar, Type, Optional


def deprecated(reason, stacklevel=2) -> Callable:
Expand Down Expand Up @@ -129,3 +129,36 @@ def wrapped(*args, **kwargs):
return wrapped

return decorator


_VT = TypeVar("_VT")
_OwnerCls = TypeVar("_OwnerCls")


class DeprecatedReadOnlyClassVar(Generic[_OwnerCls, _VT]):
"""
DeprecatedReadOnlyClassVar[Owner, ValueType]

:param warning_message: Warning message when getter gets called
:param new_value_getter: Any callable with (owner_class: Type[Owner]) -> ValueType
signature that will be executed

Usage example:

>>> class MyClass:
... some_attribute: DeprecatedReadOnlyClassVar[MyClass, int] = \
... DeprecatedReadOnlyClassVar(
... "Warning message.", lambda owner: 15)
...
>>> MyClass.some_attribute # does warning.warn with `Warning message` and returns 15 in the current case
"""

__slots__ = "_new_value_getter", "_warning_message"

def __init__(self, warning_message: str, new_value_getter: Callable[[_OwnerCls], _VT]):
self._warning_message = warning_message
self._new_value_getter = new_value_getter

def __get__(self, instance: Optional[_OwnerCls], owner: Type[_OwnerCls]):
warn_deprecated(self._warning_message, stacklevel=3)
return self._new_value_getter(owner)
14 changes: 14 additions & 0 deletions tests/test_utils/test_deprecated.py
@@ -0,0 +1,14 @@
import pytest

from aiogram.utils.deprecated import DeprecatedReadOnlyClassVar


def test_DeprecatedReadOnlyClassVarCD():
assert DeprecatedReadOnlyClassVar.__slots__ == ("_new_value_getter", "_warning_message")

new_value_of_deprecated_cls_cd = "mpa"
pseudo_owner_cls = type("OpekaCla$$", (), {})
deprecated_cd = DeprecatedReadOnlyClassVar("mopekaa", lambda owner: new_value_of_deprecated_cls_cd)

with pytest.warns(DeprecationWarning):
assert deprecated_cd.__get__(None, pseudo_owner_cls) == new_value_of_deprecated_cls_cd