Fix handling of default empty string ("") in CallbackData filter (#1493)
Some checks failed
Tests / tests (macos-latest, 3.10) (push) Has been cancelled
Tests / tests (macos-latest, 3.11) (push) Has been cancelled
Tests / tests (macos-latest, 3.12) (push) Has been cancelled
Tests / tests (macos-latest, 3.13) (push) Has been cancelled
Tests / tests (macos-latest, 3.9) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.10) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.9) (push) Has been cancelled
Tests / tests (windows-latest, 3.10) (push) Has been cancelled
Tests / tests (windows-latest, 3.11) (push) Has been cancelled
Tests / tests (windows-latest, 3.12) (push) Has been cancelled
Tests / tests (windows-latest, 3.13) (push) Has been cancelled
Tests / tests (windows-latest, 3.9) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.9) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.9) (push) Has been cancelled

* Update callback_data.py

Allows using a default value in the class which is equal to an empty string ("").

Example:
class MyCallbackData(CallbackData, prefix="MyCallbackData"):
    input1: str
    input2: str = ""

* Create 1493.bugfix.rst

* Update callback_data.py

Fixed an issue that prevented unpacking None values.

* Added tests for CallbackData

* Update tests/test_filters/test_callback_data.py

Co-authored-by: Oleg A. <t0rr@mail.ru>

* Update test_callback_data.py

* Update callback_data.py

* Update 1493.bugfix.rst

---------

Co-authored-by: Oleg A. <t0rr@mail.ru>
This commit is contained in:
m-xim 2025-03-11 01:14:13 +03:00 committed by GitHub
parent 925616ff79
commit 658f1fc082
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 2 deletions

1
CHANGES/1493.bugfix.rst Normal file
View file

@ -0,0 +1 @@
Fix handling of default empty string ("") in CallbackData filter

View file

@ -22,6 +22,7 @@ from uuid import UUID
from magic_filter import MagicFilter
from pydantic import BaseModel
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined
from aiogram.filters.base import Filter
from aiogram.types import CallbackQuery
@ -130,8 +131,8 @@ class CallbackData(BaseModel):
payload = {}
for k, v in zip(names, parts): # type: str, Optional[str]
if field := cls.model_fields.get(k):
if v == "" and _check_field_is_nullable(field):
v = None
if v == "" and _check_field_is_nullable(field) and field.default != "":
v = field.default if field.default is not PydanticUndefined else None
payload[k] = v
return cls(**payload)

View file

@ -160,6 +160,13 @@ class TestCallbackData:
assert MyCallback3.unpack("test3:experiment:42") == MyCallback3(bar=42)
assert MyCallback3.unpack("test3:spam:42") == MyCallback3(foo="spam", bar=42)
class MyCallback4(CallbackData, prefix="test4"):
foo: Optional[str] = ""
bar: Optional[str] = None
assert MyCallback4.unpack("test4::") == MyCallback4(foo="", bar=None)
assert MyCallback4.unpack("test4::") == MyCallback4()
@pytest.mark.parametrize(
"hint",
[