Skip to content

Add structure hook for optional notebook filter union - #466

Open
Shubham Padkonde (Shubham-Padkonde) wants to merge 1 commit into
microsoft:mainfrom
Shubham-Padkonde:fix-optional-notebook-filter-hook
Open

Shubham Padkonde (Shubham-Padkonde) wants to merge 1 commit into
microsoft:mainfrom
Shubham-Padkonde:fix-optional-notebook-filter-hook

Conversation

@Shubham-Padkonde

@Shubham-Padkonde Shubham Padkonde (Shubham-Padkonde) commented Sep 19, 2026 •

Copy link
Copy Markdown

Fixes #462
Fixes #430

NotebookDocumentFilterWithCells.notebook is typed Optional[Union[str, NotebookDocumentFilter]]. _hooks.py registers _notebook_filter_hook only for the non-optional union (NotebookCellTextDocumentFilter.notebook), so the optional form has no structure hook.

This happens to work with the pinned cattrs 24.1.3. With cattrs 26.x, structuring any notebookSelector entry fails with StructureHandlerNotFoundError, even the cells-only form where notebook is absent. That's the form ty server sends in the issue. The repo's own test_notebook_sync_options fails as well once cattrs is upgraded.

This registers a hook for the optional union (taken from attrs.fields(NotebookDocumentFilterWithCells).notebook.type, like NotebookSelectorItem). It returns None for None and otherwise defers to _notebook_filter_hook.

Tests

New tests in tests/python/test_cattrs_special_cases.py:

  • the InitializeResponse from the issue (cells-only selector), checking that notebook is None;
  • a notebook + cells selector round-trip for each variant: plain string, notebookType, scheme and pattern.

Results for pytest tests/python:

cattrs / attrs without this change with this change
24.1.3 / 25.3.0 (pinned) passes 124 passed, 1 skipped
26.2.0 / 26.1.0 (latest) 2 failed: the existing test_notebook_sync_options and the new InitializeResponse test 124 passed, 1 skipped

ruff format --check is clean on both files, and mypy --strict on packages/python/lsprotocol reports no issues.

This change was written with help from an AI coding assistant (Claude Code). I reviewed and tested it as described above.

🤖 Generated with Claude Code

NotebookDocumentFilterWithCells.notebook is declared as
Optional[Union[str, NotebookDocumentFilter]], but only the non-optional
union had a structure hook. Recent cattrs releases (26.x) build a
structurer for every field type up front, so structuring any
notebookSelector entry, including the cells-only form, fails with
StructureHandlerNotFoundError. Register a hook for the optional union
that passes None through and delegates to the existing notebook filter
hook.

Fixes microsoft#462

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant