From 44bd8fe1037318183631db232ffadc63291e191a Mon Sep 17 00:00:00 2001 From: "Oleg A." Date: Sat, 4 Jan 2025 11:37:26 +0300 Subject: [PATCH 1/5] Remove path to string convertion (#1612) * Remove path to string conversion * docs: changelog added --- CHANGES/1612.misc.rst | 1 + aiogram/client/bot.py | 6 +++--- aiogram/client/telegram.py | 2 +- tests/test_api/test_client/test_api_server.py | 12 ++++++++---- tests/test_api/test_client/test_bot.py | 6 ++++-- 5 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 CHANGES/1612.misc.rst diff --git a/CHANGES/1612.misc.rst b/CHANGES/1612.misc.rst new file mode 100644 index 00000000..e34e2023 --- /dev/null +++ b/CHANGES/1612.misc.rst @@ -0,0 +1 @@ +Removed redundant :code:`Path` to :code:`str` convertion on file download. diff --git a/aiogram/client/bot.py b/aiogram/client/bot.py index a09dda28..9848d7bb 100644 --- a/aiogram/client/bot.py +++ b/aiogram/client/bot.py @@ -391,7 +391,7 @@ class Bot: @classmethod async def __aiofiles_reader( - cls, file: str, chunk_size: int = 65536 + cls, file: Union[str, pathlib.Path], chunk_size: int = 65536 ) -> AsyncGenerator[bytes, None]: async with aiofiles.open(file, "rb") as f: while chunk := await f.read(chunk_size): @@ -399,7 +399,7 @@ class Bot: async def download_file( self, - file_path: str, + file_path: Union[str, pathlib.Path], destination: Optional[Union[BinaryIO, pathlib.Path, str]] = None, timeout: int = 30, chunk_size: int = 65536, @@ -423,7 +423,7 @@ class Bot: close_stream = False if self.session.api.is_local: stream = self.__aiofiles_reader( - str(self.session.api.wrap_local_file.to_local(file_path)), chunk_size=chunk_size + self.session.api.wrap_local_file.to_local(file_path), chunk_size=chunk_size ) close_stream = True else: diff --git a/aiogram/client/telegram.py b/aiogram/client/telegram.py index 5e29722f..cfb3c49d 100644 --- a/aiogram/client/telegram.py +++ b/aiogram/client/telegram.py @@ -67,7 +67,7 @@ class TelegramAPIServer: """ return self.base.format(token=token, method=method) - def file_url(self, token: str, path: str) -> str: + def file_url(self, token: str, path: Union[str, Path]) -> str: """ Generate URL for downloading files diff --git a/tests/test_api/test_client/test_api_server.py b/tests/test_api/test_client/test_api_server.py index 118ea630..36fd5140 100644 --- a/tests/test_api/test_client/test_api_server.py +++ b/tests/test_api/test_client/test_api_server.py @@ -1,5 +1,7 @@ from pathlib import Path +import pytest + from aiogram.client.telegram import ( PRODUCTION, BareFilesPathWrapper, @@ -13,15 +15,17 @@ class TestAPIServer: method_url = PRODUCTION.api_url(token="42:TEST", method="apiMethod") assert method_url == "https://api.telegram.org/bot42:TEST/apiMethod" - def test_file_url(self): - file_url = PRODUCTION.file_url(token="42:TEST", path="path") + @pytest.mark.parametrize("path", ["path", Path("path")]) + def test_file_url(self, path): + file_url = PRODUCTION.file_url(token="42:TEST", path=path) assert file_url == "https://api.telegram.org/file/bot42:TEST/path" - def test_from_base(self): + @pytest.mark.parametrize("path", ["path", Path("path")]) + def test_from_base(self, path): local_server = TelegramAPIServer.from_base("http://localhost:8081", is_local=True) method_url = local_server.api_url("42:TEST", method="apiMethod") - file_url = local_server.file_url(token="42:TEST", path="path") + file_url = local_server.file_url(token="42:TEST", path=path) assert method_url == "http://localhost:8081/bot42:TEST/apiMethod" assert file_url == "http://localhost:8081/file/bot42:TEST/path" diff --git a/tests/test_api/test_client/test_bot.py b/tests/test_api/test_client/test_bot.py index 34593864..11a11c34 100644 --- a/tests/test_api/test_client/test_bot.py +++ b/tests/test_api/test_client/test_bot.py @@ -1,5 +1,6 @@ import io import os +from pathlib import Path from tempfile import mkstemp from unittest.mock import AsyncMock, MagicMock, patch @@ -112,7 +113,8 @@ class TestBot: mocked_close.assert_not_awaited() await session.close() - async def test_download_file(self, aresponses: ResponsesMockServer): + @pytest.mark.parametrize("file_path", ["file.png", Path("file.png")]) + async def test_download_file(self, aresponses: ResponsesMockServer, file_path): aresponses.add( method_pattern="get", response=aresponses.Response(status=200, body=b"\f" * 10), @@ -127,7 +129,7 @@ class TestBot: async with Bot("42:TEST").context() as bot: with patch("aiofiles.threadpool.sync_open", return_value=mock_file): - await bot.download_file("TEST", "file.png") + await bot.download_file("TEST", file_path) mock_file.write.assert_called_once_with(b"\f" * 10) async def test_download_file_default_destination( From 75fbe544c326b3aa1551ae0fa2ac5edba413bf58 Mon Sep 17 00:00:00 2001 From: VoVcHiC <74859502+vovchic17@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:03:25 +0300 Subject: [PATCH 2/5] fix typo in ukrainian docs (#1629) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix typo in ukrainian docs * remove "якісь" --- docs/locale/uk_UA/LC_MESSAGES/index.po | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/locale/uk_UA/LC_MESSAGES/index.po b/docs/locale/uk_UA/LC_MESSAGES/index.po index 496a6c50..fbfb3e14 100644 --- a/docs/locale/uk_UA/LC_MESSAGES/index.po +++ b/docs/locale/uk_UA/LC_MESSAGES/index.po @@ -173,7 +173,7 @@ msgstr "" #: ../../../README.rst:71 msgid "If you have any questions, you can visit our community chats on Telegram:" -msgstr "Якщо є якість додаткові запитання, ласкаво просимо до онлайн-спільнот:" +msgstr "Якщо є додаткові запитання, ласкаво просимо до онлайн-спільнот:" #: ../../../README.rst:73 msgid "🇺🇸 `@aiogram `_" From a516b7f03f6a32796f5f3721f0cab8bcf78e4821 Mon Sep 17 00:00:00 2001 From: Anton Trotsenko <37223374+antos07@users.noreply.github.com> Date: Fri, 17 Jan 2025 00:29:50 +0100 Subject: [PATCH 3/5] Bump redis max version to <5.3.0 (#1631) * Bump redis max version to <5.3.0 * Describe changes --- CHANGES/1631.misc.rst | 1 + pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1631.misc.rst diff --git a/CHANGES/1631.misc.rst b/CHANGES/1631.misc.rst new file mode 100644 index 00000000..76e2fa47 --- /dev/null +++ b/CHANGES/1631.misc.rst @@ -0,0 +1 @@ + Increased max :code:`redis` version support from “<5.1.0” to “<5.3.0” diff --git a/pyproject.toml b/pyproject.toml index 1e2f2572..a5b7cd8d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ fast = [ "aiodns>=3.0.0", ] redis = [ - "redis[hiredis]>=5.0.1,<5.1.0", + "redis[hiredis]>=5.0.1,<5.3.0", ] mongo = [ "motor>=3.3.2,<3.7.0", From bbdc43b737d4cefaa7eed2ff3c1971339ecbbd66 Mon Sep 17 00:00:00 2001 From: AmirSoroush Date: Fri, 17 Jan 2025 02:35:23 +0300 Subject: [PATCH 4/5] Fix `BAD_PATTERN` regex in deeplinking process. (#1630) * add test payload in `test_deep_linking` to demonstrate the bug * fix the `BAD_PATTERN` regex * add changes file --- CHANGES/1630.bugfix.rst | 1 + aiogram/utils/deep_linking.py | 2 +- tests/test_utils/test_deep_linking.py | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1630.bugfix.rst diff --git a/CHANGES/1630.bugfix.rst b/CHANGES/1630.bugfix.rst new file mode 100644 index 00000000..e083dc1f --- /dev/null +++ b/CHANGES/1630.bugfix.rst @@ -0,0 +1 @@ +Fix the regex pattern that finds the "bad characters" for deeplink payload. \ No newline at end of file diff --git a/aiogram/utils/deep_linking.py b/aiogram/utils/deep_linking.py index 19cc64c6..fe2764f7 100644 --- a/aiogram/utils/deep_linking.py +++ b/aiogram/utils/deep_linking.py @@ -18,7 +18,7 @@ from aiogram.utils.payload import decode_payload, encode_payload if TYPE_CHECKING: from aiogram import Bot -BAD_PATTERN = re.compile(r"[^A-z0-9-]") +BAD_PATTERN = re.compile(r"[^a-zA-Z0-9-_]") async def create_start_link( diff --git a/tests/test_utils/test_deep_linking.py b/tests/test_utils/test_deep_linking.py index 85a6027b..c5f5259a 100644 --- a/tests/test_utils/test_deep_linking.py +++ b/tests/test_utils/test_deep_linking.py @@ -10,12 +10,14 @@ PAYLOADS = [ "aaBBccDDeeFF5544332211", -12345678901234567890, 12345678901234567890, + "underscore_and-dash", ] WRONG_PAYLOADS = [ "@BotFather", "Some:special$characters#=", "spaces spaces spaces", 1234567890123456789.0, + "has`backtick", ] From 6e812946853e4ecb9b4c502dd71e6e78cd482ce4 Mon Sep 17 00:00:00 2001 From: monosans Date: Sun, 19 Jan 2025 01:35:44 +0000 Subject: [PATCH 5/5] fix: change the `Downloadable` protocol to be non-writable to shut up type checking that checks code that uses the `bot.download(...)` method (#1628) * fix: don't require Downloadable.file_id to be writable * docs: add changelog * Update CHANGES/1628.bugfix.rst Co-authored-by: Alex Root Junior --------- Co-authored-by: Alex Root Junior --- CHANGES/1628.bugfix.rst | 1 + aiogram/types/downloadable.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1628.bugfix.rst diff --git a/CHANGES/1628.bugfix.rst b/CHANGES/1628.bugfix.rst new file mode 100644 index 00000000..ee5d7299 --- /dev/null +++ b/CHANGES/1628.bugfix.rst @@ -0,0 +1 @@ +Change the :code:`Downloadable` protocol to be non-writable to shut up type checking that checks code that uses the :code:`bot.download(...)` method diff --git a/aiogram/types/downloadable.py b/aiogram/types/downloadable.py index be808293..080caf64 100644 --- a/aiogram/types/downloadable.py +++ b/aiogram/types/downloadable.py @@ -2,4 +2,5 @@ from typing import Protocol class Downloadable(Protocol): - file_id: str + @property + def file_id(self) -> str: ...