ValidationError.validator is None for a false schema, but annotated as str

Aperta
#1,597 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 6 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start with the two false-schema branches in jsonschema/validators.py and _Error.__init__ in jsonschema/exceptions.py; compare their runtime behavior with the validator annotation and its documentation. Decide whether None should be allowed or the branches should supply another value, then check the related tests and update them to cover errors from a false schema. Done means runtime behavior, annotations, and documentation agree on the chosen behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A ValidationError produced by a false (sub)schema has validator=None and validator_value=None:

>>> import jsonschema
>>> e = next(jsonschema.Draft202012Validator({"properties": {"a": False}}).iter_errors({"a": 1}))
>>> e.validator, e.validator_value, e.schema
(None, None, False)

That comes from the two False-schema branches, which pass validator=None
(validators.py#L377,
#L412),
while _Error.__init__ annotates the parameter as validator: str
(exceptions.py#L63),
and the docs describe validator as "the name of the failed keyword". There is no keyword for a boolean schema,
so None is a reasonable value at runtime -- but the annotation does not allow it.

This matters downstream because typeshed's types-jsonschema mirrors the annotation (validator: str | Unset, since
python/typeshed#15353), so type checkers treat error.validator as always a string, and code that relies on that
(e.g. error.validator.upper()) passes type checking and fails on errors from false schemas.

Which side would you consider correct?

  • the annotation (and docs) should admit None for errors raised by a boolean false schema, or
  • the False-schema branches should pass something else.

Happy to send a PR for whichever you prefer, and to follow up in typeshed accordingly.

Seen on jsonschema 4.25.1 and on main (c497561).

Lingua principale
Python
Stelle
5k
Fork
741
Merge medio
4g 17h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-jsonschema/jsonschema

Tutte le issue di python-jsonschema/jsonschema

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.