mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
fix: CallbackData set optional as None
This commit is contained in:
parent
ebade3d51f
commit
edee03ffe4
2 changed files with 11 additions and 3 deletions
|
|
@ -120,9 +120,8 @@ class CallbackData(BaseModel):
|
|||
raise ValueError(f"Bad prefix ({prefix!r} != {cls.__prefix__!r})")
|
||||
payload = {}
|
||||
for k, v in zip(names, parts): # type: str, Optional[str]
|
||||
if field := cls.model_fields.get(k):
|
||||
if v == "" and not field.is_required():
|
||||
v = None
|
||||
if v == "":
|
||||
v = None
|
||||
payload[k] = v
|
||||
return cls(**payload)
|
||||
|
||||
|
|
|
|||
|
|
@ -147,6 +147,15 @@ class TestCallbackData:
|
|||
assert MyCallback3.unpack("test3:experiment:42") == MyCallback3(bar=42)
|
||||
assert MyCallback3.unpack("test3:spam:42") == MyCallback3(foo="spam", bar=42)
|
||||
|
||||
def test_unpack_optional_wo_default(self):
|
||||
"""Test CallbackData without default optional."""
|
||||
|
||||
class TgData(CallbackData, prefix="tg"):
|
||||
chat_id: int
|
||||
thread_id: Optional[int]
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue