mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-14 19:00:23 +00:00
Fix CallbackData without default Optional (#1370)
* fix: CallbackData set optional as None * docs: add fix changelog * Add support for nullable fields in callback data This update extends the callback data handling by adding support for nullable fields. The code now uses the Python typing structures `Optional` and `Union` to parse such fields correctly. A helper function `_check_field_is_nullable` has been added to assist in efficiently checking if a given field is nullable. * Add support for nullable fields in callback data This update extends the callback data handling by adding support for nullable fields. The code now uses the Python typing structures `Optional` and `Union` to parse such fields correctly. A helper function `_check_field_is_nullable` has been added to assist in efficiently checking if a given field is nullable. --------- Co-authored-by: JRoot Junior <jroot.junior@gmail.com>
This commit is contained in:
parent
ebade3d51f
commit
e17e3bc71c
5 changed files with 41 additions and 4 deletions
|
|
@ -1,7 +1,7 @@
|
|||
from decimal import Decimal
|
||||
from enum import Enum, auto
|
||||
from fractions import Fraction
|
||||
from typing import Optional
|
||||
from typing import Optional, Union
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
|
@ -147,6 +147,22 @@ class TestCallbackData:
|
|||
assert MyCallback3.unpack("test3:experiment:42") == MyCallback3(bar=42)
|
||||
assert MyCallback3.unpack("test3:spam:42") == MyCallback3(foo="spam", bar=42)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"hint",
|
||||
[
|
||||
Union[int, None],
|
||||
Optional[int],
|
||||
],
|
||||
)
|
||||
def test_unpack_optional_wo_default(self, hint):
|
||||
"""Test CallbackData without default optional."""
|
||||
|
||||
class TgData(CallbackData, prefix="tg"):
|
||||
chat_id: int
|
||||
thread_id: hint
|
||||
|
||||
assert TgData.unpack("tg:123:") == TgData(chat_id=123, thread_id=None)
|
||||
|
||||
def test_build_filter(self):
|
||||
filter_object = MyCallback.filter(F.foo == "test")
|
||||
assert isinstance(filter_object.rule, MagicFilter)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import pytest
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.fsm.state import State, StatesGroup, any_state
|
||||
|
||||
PY312_OR_GREATER = sys.version_info >= (3, 12)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue