#944 Rename "extract()" to "extract_from()" for entities (#945)

* #944 Rename "extract()" to "extract_from()" for entities
This commit is contained in:
Alex Root Junior 2022-06-26 01:36:45 +03:00 committed by GitHub
parent fadb11515e
commit b8893c0971
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

1
CHANGES/944.misc.rst Normal file
View file

@ -0,0 +1 @@
`MessageEntity` method `get_text` was removed and `extract` was renamed to `extract_from`

View file

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import warnings
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from ..utils.text_decorations import add_surrogates, remove_surrogates from ..utils.text_decorations import add_surrogates, remove_surrogates
@ -29,7 +30,15 @@ class MessageEntity(MutableTelegramObject):
language: Optional[str] = None language: Optional[str] = None
"""*Optional*. For 'pre' only, the programming language of the entity text""" """*Optional*. For 'pre' only, the programming language of the entity text"""
def extract(self, text: str) -> str: def extract_from(self, text: str) -> str:
return remove_surrogates( return remove_surrogates(
add_surrogates(text)[self.offset * 2 : (self.offset + self.length) * 2] add_surrogates(text)[self.offset * 2 : (self.offset + self.length) * 2]
) )
def extract(self, text: str) -> str:
warnings.warn(
"Method `MessageEntity.extract(...)` deprecated and will be removed in 3.0b5.\n"
" Use `MessageEntity.extract_from(...)` instead.",
DeprecationWarning,
)
return self.extract_from(text=text)

View file

@ -1,7 +1,13 @@
from aiogram.types import MessageEntity from aiogram.types import MessageEntity
from tests.deprecated import check_deprecated
class TestMessageEntity: class TestMessageEntity:
def test_extract_from(self):
entity = MessageEntity(type="hashtag", length=4, offset=5)
assert entity.extract_from("#foo #bar #baz") == "#bar"
def test_extract(self): def test_extract(self):
entity = MessageEntity(type="hashtag", length=4, offset=5) entity = MessageEntity(type="hashtag", length=4, offset=5)
with check_deprecated("3.0b5", exception=AttributeError):
assert entity.extract("#foo #bar #baz") == "#bar" assert entity.extract("#foo #bar #baz") == "#bar"