Skip to content

Commit

Permalink
Fixed error overlapping when validation error is caused by remove_uns…
Browse files Browse the repository at this point in the history
…et root validator in base types and methods. (#1290)

* Ensure base type validation can handle non-dictionary values

The update introduces a condition to verify whether the values being validated are a dictionary before attempting to handle UNSET_TYPE in the aiogram base type. This adjustment helps to prevent potential errors or incorrect validation when non-dictionary values are faced.

* Added a test case for non-dictionary input in remove_unset method

* Added changelog

* Fixed tests
  • Loading branch information
JrooTJunior committed Aug 28, 2023
1 parent e1be9dd commit 04bd0c9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/1290.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed error overlapping when validation error is caused by remove_unset root validator in base types and methods.
2 changes: 2 additions & 0 deletions aiogram/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def remove_unset(cls, values: Dict[str, Any]) -> Dict[str, Any]:
but UNSET might be passing to a model initialization from `Bot.method_name`,
so we must take care of it and remove it before fields validation.
"""
if not isinstance(values, dict):
return values
return {k: v for k, v in values.items() if not isinstance(v, UNSET_TYPE)}

if TYPE_CHECKING:
Expand Down
2 changes: 2 additions & 0 deletions aiogram/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def remove_unset(cls, values: Dict[str, Any]) -> Dict[str, Any]:
but UNSET might be passed to a model initialization from `Bot.method_name`,
so we must take care of it and remove it before fields validation.
"""
if not isinstance(values, dict):
return values
return {k: v for k, v in values.items() if not isinstance(v, UNSET_TYPE)}


Expand Down
11 changes: 8 additions & 3 deletions tests/test_api/test_methods/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from aiogram.methods import GetMe, TelegramMethod
from aiogram.types import User
from aiogram.types import User, TelegramObject
from tests.mocked_bot import MockedBot


Expand All @@ -16,10 +16,15 @@ class TestTelegramMethodRemoveUnset:
[{"foo": "bar", "baz": sentinel.DEFAULT}, {"foo"}],
],
)
def test_remove_unset(self, values, names):
validated = TelegramMethod.remove_unset(values)
@pytest.mark.parametrize("obj", [TelegramMethod, TelegramObject])
def test_remove_unset(self, values, names, obj):
validated = obj.remove_unset(values)
assert set(validated.keys()) == names

@pytest.mark.parametrize("obj", [TelegramMethod, TelegramObject])
def test_remove_unset_non_dict(self, obj):
assert obj.remove_unset("") == ""


class TestTelegramMethodCall:
async def test_async_emit_unsuccessful(self, bot: MockedBot):
Expand Down

0 comments on commit 04bd0c9

Please sign in to comment.