mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +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
|
|
@ -14,9 +14,10 @@ from .utils.text_decorations import html_decoration as html
|
|||
from .utils.text_decorations import markdown_decoration as md
|
||||
|
||||
with suppress(ImportError):
|
||||
import uvloop as _uvloop
|
||||
import asyncio
|
||||
|
||||
import uvloop as _uvloop
|
||||
|
||||
asyncio.set_event_loop_policy(_uvloop.EventLoopPolicy())
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
from decimal import Decimal
|
||||
from enum import Enum
|
||||
from fractions import Fraction
|
||||
|
|
@ -18,6 +19,7 @@ from uuid import UUID
|
|||
|
||||
from magic_filter import MagicFilter
|
||||
from pydantic import BaseModel
|
||||
from pydantic.fields import FieldInfo
|
||||
|
||||
from aiogram.filters.base import Filter
|
||||
from aiogram.types import CallbackQuery
|
||||
|
|
@ -121,7 +123,7 @@ 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 not field.is_required():
|
||||
if v == "" and _check_field_is_nullable(field):
|
||||
v = None
|
||||
payload[k] = v
|
||||
return cls(**payload)
|
||||
|
|
@ -180,3 +182,19 @@ class CallbackQueryFilter(Filter):
|
|||
if self.rule is None or self.rule.resolve(callback_data):
|
||||
return {"callback_data": callback_data}
|
||||
return False
|
||||
|
||||
|
||||
def _check_field_is_nullable(field: FieldInfo) -> bool:
|
||||
"""
|
||||
Check if the given field is nullable.
|
||||
|
||||
:param field: The FieldInfo object representing the field to check.
|
||||
:return: True if the field is nullable, False otherwise.
|
||||
|
||||
"""
|
||||
if not field.is_required():
|
||||
return True
|
||||
|
||||
return typing.get_origin(field.annotation) is typing.Union and type(None) in typing.get_args(
|
||||
field.annotation
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue