From ea115e04818002a2908bf5d0436845874e71d060 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:15:01 +0200 Subject: [PATCH] Fix telegram_bot tests for Python 3.13 (#127293) --- tests/components/telegram_bot/conftest.py | 33 +++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/components/telegram_bot/conftest.py b/tests/components/telegram_bot/conftest.py index 1afe70dcb8a..93137c3815e 100644 --- a/tests/components/telegram_bot/conftest.py +++ b/tests/components/telegram_bot/conftest.py @@ -6,7 +6,7 @@ from typing import Any from unittest.mock import patch import pytest -from telegram import Chat, Message, User +from telegram import Bot, Chat, Message, User from telegram.constants import ChatType from homeassistant.components.telegram_bot import ( @@ -89,23 +89,22 @@ def mock_external_calls() -> Generator[None]: date=datetime.now(), chat=Chat(id=123456, type=ChatType.PRIVATE), ) + + class BotMock(Bot): + """Mock bot class.""" + + __slots__ = () + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize BotMock instance.""" + super().__init__(*args, **kwargs) + self._bot_user = test_user + with ( - patch( - "telegram.Bot.get_me", - return_value=test_user, - ), - patch( - "telegram.Bot._bot_user", - test_user, - ), - patch( - "telegram.Bot.bot", - test_user, - ), - patch( - "telegram.Bot.send_message", - return_value=message, - ), + patch("homeassistant.components.telegram_bot.Bot", BotMock), + patch.object(BotMock, "get_me", return_value=test_user), + patch.object(BotMock, "bot", test_user), + patch.object(BotMock, "send_message", return_value=message), patch("telegram.ext.Updater._bootstrap"), ): yield