feat(utils): deprecated descriptor

deprecate CHOSEN_INLINE_QUERY and always return CHOSEN_INLINE_RESULT instead of incorrect value
This commit is contained in:
mpa 2020-09-04 23:15:59 +04:00
parent 4603961321
commit 4f6cb405c8
No known key found for this signature in database
GPG key ID: BCCFBFCCC9B754A8
3 changed files with 60 additions and 2 deletions

View file

@ -9,7 +9,7 @@ from .message import Message
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):
@ -61,3 +61,9 @@ class AllowedUpdates(helper.Helper):
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,
)

View file

@ -2,7 +2,7 @@ import asyncio
import inspect
import warnings
import functools
from typing import Callable
from typing import Callable, Generic, TypeVar, Type, Optional
def deprecated(reason, stacklevel=2) -> Callable:
@ -129,3 +129,36 @@ def renamed_argument(old_name: str, new_name: str, until_version: str, stackleve
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)
return self._new_value_getter(owner)

View file

@ -0,0 +1,19 @@
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
class MyClass:
some_attribute: DeprecatedReadOnlyClassVar["MyClass", int] = \
DeprecatedReadOnlyClassVar(
"Warning message.", lambda owner: 15)