This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new bc4ca9d879 Fix typing in telegram provider (#40255)
bc4ca9d879 is described below
commit bc4ca9d87975065497e55fcd21a31d784f8fdf05
Author: Jarek Potiuk <[email protected]>
AuthorDate: Sat Jun 15 12:28:19 2024 +0200
Fix typing in telegram provider (#40255)
The python-telegram-bot new version has typing added and we should
pass the right dict type to it.
---
airflow/providers/telegram/hooks/telegram.py | 7 ++++---
tests/providers/telegram/hooks/test_telegram.py | 14 +++++++-------
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/airflow/providers/telegram/hooks/telegram.py
b/airflow/providers/telegram/hooks/telegram.py
index 143574bf52..3f7e0176b9 100644
--- a/airflow/providers/telegram/hooks/telegram.py
+++ b/airflow/providers/telegram/hooks/telegram.py
@@ -142,17 +142,18 @@ class TelegramHook(BaseHook):
:param api_params: params for telegram_instance.send_message. It can
also be used to override chat_id
"""
- kwargs = {
- "chat_id": self.chat_id,
+ kwargs: dict[str, int | str | bool] = {
"parse_mode": telegram.constants.ParseMode.HTML,
"disable_web_page_preview": True,
}
+ if self.chat_id is not None:
+ kwargs["chat_id"] = self.chat_id
kwargs.update(api_params)
if "text" not in kwargs or kwargs["text"] is None:
raise AirflowException("'text' must be provided for telegram
message")
- if kwargs["chat_id"] is None:
+ if kwargs.get("chat_id") is None:
raise AirflowException("'chat_id' must be provided for telegram
message")
response = asyncio.run(self.connection.send_message(**kwargs))
diff --git a/tests/providers/telegram/hooks/test_telegram.py
b/tests/providers/telegram/hooks/test_telegram.py
index 2de2df7441..56ee385042 100644
--- a/tests/providers/telegram/hooks/test_telegram.py
+++ b/tests/providers/telegram/hooks/test_telegram.py
@@ -93,21 +93,21 @@ class TestTelegramHook:
hook = TelegramHook(telegram_conn_id="telegram_default")
error_message = "'text' must be provided for telegram message"
with pytest.raises(airflow.exceptions.AirflowException,
match=error_message):
- hook.send_message({"chat_id": -420913222})
+ hook.send_message({"chat_id": "-420913222"})
@mock.patch("airflow.providers.telegram.hooks.telegram.TelegramHook.get_conn")
def
test_should_send_message_if_all_parameters_are_correctly_provided(self,
mock_get_conn):
mock_get_conn.return_value = AsyncMock(password="some_token")
hook = TelegramHook(telegram_conn_id="telegram_default")
- hook.send_message({"chat_id": -420913222, "text": "test telegram
message"})
+ hook.send_message({"chat_id": "-420913222", "text": "test telegram
message"})
mock_get_conn.return_value.send_message.return_value = "OK."
mock_get_conn.assert_called_once()
mock_get_conn.return_value.send_message.assert_called_once_with(
**{
- "chat_id": -420913222,
+ "chat_id": "-420913222",
"parse_mode": "HTML",
"disable_web_page_preview": True,
"text": "test telegram message",
@@ -118,7 +118,7 @@ class TestTelegramHook:
def
test_should_send_message_if_chat_id_is_provided_through_constructor(self,
mock_get_conn):
mock_get_conn.return_value = AsyncMock(password="some_token")
- hook = TelegramHook(telegram_conn_id="telegram_default",
chat_id=-420913222)
+ hook = TelegramHook(telegram_conn_id="telegram_default",
chat_id="-420913222")
hook.send_message({"text": "test telegram message"})
mock_get_conn.return_value.send_message.return_value = "OK."
@@ -126,7 +126,7 @@ class TestTelegramHook:
mock_get_conn.assert_called_once()
mock_get_conn.return_value.send_message.assert_called_once_with(
**{
- "chat_id": -420913222,
+ "chat_id": "-420913222",
"parse_mode": "HTML",
"disable_web_page_preview": True,
"text": "test telegram message",
@@ -182,7 +182,7 @@ class TestTelegramHook:
def test_should_send_message_if_token_is_provided(self, mock_get_conn):
mock_get_conn.return_value = AsyncMock(password="some_token")
- hook = TelegramHook(token=TELEGRAM_TOKEN, chat_id=-420913222)
+ hook = TelegramHook(token=TELEGRAM_TOKEN, chat_id="-420913222")
hook.send_message({"text": "test telegram message"})
mock_get_conn.return_value.send_message.return_value = "OK."
@@ -190,7 +190,7 @@ class TestTelegramHook:
mock_get_conn.assert_called_once()
mock_get_conn.return_value.send_message.assert_called_once_with(
**{
- "chat_id": -420913222,
+ "chat_id": "-420913222",
"parse_mode": "HTML",
"disable_web_page_preview": True,
"text": "test telegram message",