Skip to content

Narrow none_member in JSON schema union handling - #1121

Merged
Siyet merged 1 commit into
mainfrom
fix-json-schema-none-narrowing
Jul 13, 2026
Merged

Narrow none_member in JSON schema union handling#1121
Siyet merged 1 commit into
mainfrom
fix-json-schema-none-narrowing

Conversation

@Siyet

@Siyet Siyet commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #1028.

#1028 introduced none_member: mi.Type | None in the union branch of _json_schema.py and guarded the self.to_schema(none_member) calls with a separate has_none boolean. mypy can't narrow none_member through the flag, so it errors:

_json_schema.py:363: error: Argument 1 to "to_schema" of "_SchemaGenerator" has incompatible type "Type | None"; expected "Type"  [arg-type]

This is not caught by current CI because the msgspec source is not type-checked there (only tests/typing is); #1116 (adding stubtest) is what surfaces it. Fixing it here so #1116 can go green.

The fix drops the redundant has_none flag and checks none_member is not None at the call sites, which gives mypy the narrowing. No runtime behavior change (has_none and none_member were always set together): tests/unit/test_schema.py still passes (115), and stubtest with mypy 2.2.0 is clean.

#1028 left none_member typed as `mi.Type | None` and gated the
`to_schema(none_member)` calls on a separate `has_none` flag, which mypy
can't use to narrow the argument, so mypy (as run by stubtest) reports an
incompatible-type error. Drop the flag and check `none_member is not None`
directly, which narrows the type and removes a redundant variable. No
behavior change.
@codspeed-hq

codspeed-hq Bot commented Jul 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 139 untouched benchmarks
⏩ 135 skipped benchmarks1


Comparing fix-json-schema-none-narrowing (c68b10a) with main (e1c6f23)

Open in CodSpeed

Footnotes

  1. 135 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Siyet
Siyet added this pull request to the merge queue Jul 13, 2026
Merged via the queue into main with commit ceb01c1 Jul 13, 2026
27 checks passed
@Siyet
Siyet deleted the fix-json-schema-none-narrowing branch July 13, 2026 10:08
@Siyet Siyet mentioned this pull request Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant