Repository navigation
[jsonschema] Annotate keyword validators as returning iterators - #16722
Open
Dhairya-007 wants to merge 1 commit into
Open
Dhairya-007 wants to merge 1 commit into
Dhairya-007 wants to merge 1 commit into
Conversation
The keyword validation functions in `_keywords` and `_legacy_keywords`
are generators that yield `ValidationError`, but 49 of them were
declared `-> None`, so iterating their result was a type error:
for error in additionalProperties(validator, False, instance, schema):
...
# error: "None" has no attribute "__iter__" (not iterable)
# error: "additionalProperties" does not return a value
`_legacy_keywords.pyi` already annotates two of its own functions,
`unevaluatedItems_draft2019` and `unevaluatedProperties_draft2019`, as
`Iterator[ValidationError]`, and `protocols.pyi` uses the same type for
`iter_errors`, so this makes the rest consistent with both.
Every function was checked against jsonschema 4.26.0: all 49 changed
here contain a `yield` or `yield from` outside any nested function. The
three non-generators in `_legacy_keywords` — `ignore_ref_siblings`,
`find_evaluated_item_indexes_by_schema` and
`find_evaluated_property_keys_by_schema` — already return list types and
are untouched.
stubtest reports the same 12 pre-existing `_format` errors before and
after (optional format dependencies, unrelated). mypy --strict and
pyright are clean on the stubs.
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This branch has not been deployed
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.
The keyword validation functions in
_keywordsand_legacy_keywordsare generators that yieldValidationError, but 49 of them were declared-> None. Iterating the result — the only way to use them — was a type error:A representative usage file reported 7 errors before this change and none after.
The correct type is already in these files
_legacy_keywords.pyiannotates two of its own functions,unevaluatedItems_draft2019andunevaluatedProperties_draft2019, asIterator[ValidationError], andprotocols.pyitypesiter_errorsthe same way. This just makes the remaining 49 consistent with both, and_legacy_keywords.pyialready importsIteratorandValidationErrorfor that reason.Scope
Checked against jsonschema 4.26.0, matching the pinned
~=4.26.0. Every one of the 49 contains ayieldoryield fromoutside any nested function, and at runtimeinspect.isgeneratorfunctionisTrue:_keywords.pyi— all 36 functions, plus the two imports the file now needs_legacy_keywords.pyi— 13 of the 18The three non-generators in
_legacy_keywordsare deliberately untouched:ignore_ref_siblings,find_evaluated_item_indexes_by_schemaandfind_evaluated_property_keys_by_schemagenuinely return list types and are already annotated as such.Why stubtest did not catch this
stubtest does not compare return types. It reports the same 12 pre-existing
_formaterrors before and after this change (optional format dependencies, unrelated) — the error sets diff identically.mypy --strictandpyrightare both clean on the stubs, andruff/blackpass.