Skip to content

Keep explicit None in CallbackData optional fields with a default - #2007

Merged
JrooTJunior merged 2 commits into
aiogram:dev-3.xfrom
bardrop-oss:fix-callback-data-explicit-none
Nov 15, 2026
Merged

JrooTJunior merged 2 commits into
aiogram:dev-3.xfrom
bardrop-oss:fix-callback-data-explicit-none

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

pack() writes None as an empty part. But unpack() turned an empty part into the field default whenever the field had one, so PageCallback(page=None) (with page: int | None = 1) came back as page=1, and the .filter() handler got the wrong value.

Now an empty part becomes None when the field type accepts None. Other cases stay as they were:

Fixes #2005

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • New parametrized test test_unpack_explicit_none_with_default (int | None = 1, bool | None = True, str-enum | None, str | None = "experiment", Optional[int] = 1): all 5 cases fail before the fix and pass after. It also checks that the default still round-trips.
  • New test that foo: int = 1 still unpacks an empty part as the default.
  • uv run pytest tests: 2372 passed, 52 skipped.
  • make lint: ruff, both mypy passes and pre-commit are clean.

Test Configuration:

  • Operating System: macOS
  • Python version: 3.13.7

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

pack() writes None as an empty part, but unpack() turned an empty part back into the field default when the default was not None, so the handler got the wrong value.

Fixes #2005
@github-actions github-actions Bot added the 3.x Issue or PR for stable 3.x version label Nov 12, 2026
@JrooTJunior

Copy link
Copy Markdown
Member

@claude, review it

@claude

claude Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Claude finished @JrooTJunior's task in 15s —— View job


Review of PR #2007

  • Read the diff and the surrounding unpack() logic
  • Post review

Verdict: LGTM, with one non-blocking note.

What it does

unpack() now turns an empty part into None when the field annotation accepts None. This applies even if the field has a non-None default. Both existing cases are kept:

Correctness

  • The fix matches pack(), which writes None as an empty part. That makes pack/unpack a true round trip for X | None = <non-None default>.
  • Splitting the annotation check into _annotation_allows_none is clean. _check_field_is_nullable behaves as before: it still returns True for any field that isn't required.
  • The or field.default is PydanticUndefined branch keeps the old result for required fields. For example, foo: int | None has no default and still gets None. A required foo: int gets None too and then fails validation, as it did before.
  • _UNION_TYPES already covers X | None and Optional[X], and the parametrized test exercises both.

Tests

The tests are well targeted. The parametrized test covers int, bool, a str-enum and str with a default, plus Optional[int]. It also asserts that the default still round-trips. A separate test pins the foo: int = 1 fallback.

Notes (non-blocking)

  1. Behaviour change for hand-built or legacy payloads. Strings like "test:" that were produced outside pack(), or by an older bot version, used to unpack to the default (page=1) and will now unpack to None for int | None = 1. This is the correct trade-off for the bug, but the changelog could say so. A short sentence would help, for example "an empty part now unpacks to None for fields whose type accepts None, regardless of default".
  2. The existing CallbackData docs may mention the empty-part-to-default behaviour. If they do, they should be updated; the PR checklist leaves docs unchecked. I didn't verify this.
  3. Minor: a test for a None-accepting field with a default_factory would pin the PydanticUndefined branch. This is optional.

I didn't run the test suite in this review. The PR reports 2372 passed and make lint clean.

@bardrop

bardrop commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Took the review notes:

  • The changelog entry now says that an empty part gives None for any field whose type accepts None, whatever its default, and that hand-built or older data like "test:" changes with it.
  • Added a test for int | None = Field(default_factory=...): an empty part gives None, and the default still round-trips.
  • Docs: docs/dispatcher/filters/callback_data.rst does not describe the empty-part-to-default rule, so nothing to change there.

Full suite: 2373 passed, ruff clean.

@JrooTJunior

Copy link
Copy Markdown
Member

@claude, re-review

@claude

claude Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Claude finished @JrooTJunior's task in 12s —— View job


Re-review of PR #2007

Verdict: LGTM. All three notes from the first review are addressed, and I have no new blocking issues.

  • Read the diff against origin/dev-3.x
  • Check the follow-ups from the first review
  • Post review

Follow-ups from the first review

  1. Changelog: CHANGES/2005.bugfix.rst now says that an empty part unpacks to None for any field whose type accepts None, whatever its default. It also says that hand-built or older data such as "test:" changes with it. The fragment uses the right category and describes user-visible behaviour.
  2. Docs: I did not re-check docs/dispatcher/filters/callback_data.rst myself. I'm relying on your statement that it doesn't describe the empty-part-to-default rule.
  3. default_factory test: test_unpack_empty_part_optional_with_default_factory is added. It covers the PydanticUndefined branch: a field with a default_factory has field.default set to PydanticUndefined, so it unpacks to None here. It also checks that the default still round-trips.

Code (aiogram/filters/callback_data.py:133-141)

  • The logic is correct and matches pack().
    • If the field is nullable and its default isn't "", an empty part gives None when the annotation accepts None or the field has no default value.
    • Otherwise the field's default is used.
  • Splitting out _annotation_allows_none leaves _check_field_is_nullable behaving as before.
  • The field.default == "" case from Fix handling of default empty string ("") in CallbackData filter #1493 is still handled by the guard above this branch.
  • foo: int = 1 still falls back to the default, and test_unpack_empty_part_not_optional_uses_default pins that.

Minor, non-blocking

  • A field annotated Annotated[int | None, ...] is not a case I checked. Pydantic strips Annotated from field.annotation, so it should be fine, and the previous code made the same assumption.

I did not run the test suite in this review. The PR reports the full suite passing (2373) and ruff clean.

@github-actions

Copy link
Copy Markdown

✔️ Changelog found.

Thank you for adding a description of the changes

@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e164475) to head (ff0fe51).
⚠️ Report is 5 commits behind head on dev-3.x.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           dev-3.x     #2007    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          750       787    +37     
  Lines        16800     17472   +672     
==========================================
+ Hits         16800     17472   +672     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
aiogram/filters/callback_data.py 100.00% <100.00%> (ø)

... and 75 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JrooTJunior
JrooTJunior merged commit 44303fc into aiogram:dev-3.x Nov 15, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Issue or PR for stable 3.x version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CallbackData: an explicit None in an optional field with a default unpacks as the default

2 participants