Skip to content

Commit

Permalink
hotfix(updates): CHOSEN_INLINE_RESULT is a correct API-term (aiogram#415
Browse files Browse the repository at this point in the history
)

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

* feat(utils): deprecated descriptor

deprecate CHOSEN_INLINE_QUERY and always return CHOSEN_INLINE_RESULT instead of incorrect value

* fix(tests): remove example from test

* fix(utils): use stacklevel=3

level on which descriptor is being called
  • Loading branch information
uwinx authored and uburuntu committed Oct 3, 2020
1 parent 5fd51dc commit 9a7a5ad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
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

0 comments on commit 9a7a5ad

Please sign in to comment.