From 5d9c33a12cdd64eb337aaf5a362116949268bade Mon Sep 17 00:00:00 2001 From: deadzilla Date: Tue, 7 Jul 2026 21:24:04 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20H1=20=E2=80=94=20IndexError=20=D0=B2=20f?= =?UTF-8?q?ormat=5Fweather=5Fdata=5Ffor=5Fconsole=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D1=83=D1=81=D1=82=D0=BE=D0=BC=20current=5Fcondition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Защита от пустого списка [] и пустого dict [{}] в current_condition - Возврат None вместо краша, команда !pg показывает fallback-сообщение - Обновлён тест test_pg_empty_current_condition (graceful fallback вместо IndexError) 230 тестов проходят. --- tests/test_commands_pg.py | 7 ++++--- utils/pogoda.py | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_commands_pg.py b/tests/test_commands_pg.py index c2c8457..ff5edbe 100644 --- a/tests/test_commands_pg.py +++ b/tests/test_commands_pg.py @@ -73,14 +73,15 @@ class TestPgCommand: @pytest.mark.asyncio async def test_pg_empty_current_condition(self): - """current_condition пустой список — код выбрасывает IndexError (баг в коде).""" + """current_condition пустой список — graceful fallback.""" cog = self._make_cog() ctx = self._make_ctx() weather = {"current_condition": []} with patch("commands.pg.fetch_weather", new=AsyncMock(return_value=weather)): - with pytest.raises(IndexError): - await cog.pg.callback(cog, ctx) + await cog.pg.callback(cog, ctx) + ctx.send.assert_called_once() + assert "Не удалось получить данные о погоде" in ctx.send.call_args[0][0] @pytest.mark.asyncio async def test_pg_current_condition_none(self): diff --git a/utils/pogoda.py b/utils/pogoda.py index baae06a..880485c 100644 --- a/utils/pogoda.py +++ b/utils/pogoda.py @@ -155,7 +155,10 @@ def format_weather_data_for_console(data): """ if data is None: return None - current = data.get("current_condition", [{}])[0] + current_condition_list = data.get("current_condition", []) + if not current_condition_list: + return None + current = current_condition_list[0] if not current: return None