mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
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
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:
parent
925616ff79
commit
658f1fc082
3 changed files with 11 additions and 2 deletions
1
CHANGES/1493.bugfix.rst
Normal file
1
CHANGES/1493.bugfix.rst
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Fix handling of default empty string ("") in CallbackData filter
|
||||||
|
|
@ -22,6 +22,7 @@ from uuid import UUID
|
||||||
from magic_filter import MagicFilter
|
from magic_filter import MagicFilter
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic.fields import FieldInfo
|
from pydantic.fields import FieldInfo
|
||||||
|
from pydantic_core import PydanticUndefined
|
||||||
|
|
||||||
from aiogram.filters.base import Filter
|
from aiogram.filters.base import Filter
|
||||||
from aiogram.types import CallbackQuery
|
from aiogram.types import CallbackQuery
|
||||||
|
|
@ -130,8 +131,8 @@ class CallbackData(BaseModel):
|
||||||
payload = {}
|
payload = {}
|
||||||
for k, v in zip(names, parts): # type: str, Optional[str]
|
for k, v in zip(names, parts): # type: str, Optional[str]
|
||||||
if field := cls.model_fields.get(k):
|
if field := cls.model_fields.get(k):
|
||||||
if v == "" and _check_field_is_nullable(field):
|
if v == "" and _check_field_is_nullable(field) and field.default != "":
|
||||||
v = None
|
v = field.default if field.default is not PydanticUndefined else None
|
||||||
payload[k] = v
|
payload[k] = v
|
||||||
return cls(**payload)
|
return cls(**payload)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,6 +160,13 @@ class TestCallbackData:
|
||||||
assert MyCallback3.unpack("test3:experiment:42") == MyCallback3(bar=42)
|
assert MyCallback3.unpack("test3:experiment:42") == MyCallback3(bar=42)
|
||||||
assert MyCallback3.unpack("test3:spam:42") == MyCallback3(foo="spam", 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(
|
@pytest.mark.parametrize(
|
||||||
"hint",
|
"hint",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue