Narrow none_member in JSON schema union handling - #1121
Merged
Conversation
#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.
Merging this PR will not alter performance
Comparing Footnotes
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #1028.
#1028 introduced
none_member: mi.Type | Nonein the union branch of_json_schema.pyand guarded theself.to_schema(none_member)calls with a separatehas_noneboolean. mypy can't narrownone_memberthrough the flag, so it errors:This is not caught by current CI because the msgspec source is not type-checked there (only
tests/typingis); #1116 (adding stubtest) is what surfaces it. Fixing it here so #1116 can go green.The fix drops the redundant
has_noneflag and checksnone_member is not Noneat the call sites, which gives mypy the narrowing. No runtime behavior change (has_noneandnone_memberwere always set together):tests/unit/test_schema.pystill passes (115), and stubtest with mypy 2.2.0 is clean.