Fix Validator
protocol init to match runtime
#1396
Open
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.
Addresses #1382
The runtime init signature of a validator can be seen with
inspect
:This aligns the protocol's declared signature with that behavior more exactly with the following changes:
registry
is now keyword-only, not keyword-or-positional, and has adefault.
resolver
is added to the declared signature, so users who are usingit won't see typing-time discrepancies with the runtime. It is marked
as
Any
and commented inline as deprecated, since it's unclear whatelse we could do to indicate its status.
This means that code passing a resolver will continue to type check
(previously it would not).
resolver
is the second keyword-or-positional andformat_checker
isthe third, meaning that a positional-only caller who passes, for
example:
Draft202012Validator(foo, None, bar)
will havefoo
slotted as the schema and
bar
as theformat_checker
This would primarily impact callers with positional-args calling
conventions, but is more reflective of what they'll see at runtime.
In order to remove
resolver
from the protocol signature, but match the runtime signatures well, some kind of placeholder is needed to indicateformat_checker
as positional-or-keyword.Or else, a large number of overloads for
__init__
could be declared to try to simulate its removal.I considered such options but decided that they add undue complexity for what is supposed to be a hint for correct usage.
Assuming this or something like it merges, I'll backport to typeshed.
📚 Documentation preview 📚: https://python-jsonschema--1396.org.readthedocs.build/en/1396/