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

Abierto
#1,597 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 6 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).

Lenguaje dominante
Python
Estrellas
5k
Forks
741
Merge medio
4 d 17 h
PR fusionados (30 d)
6

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-jsonschema/jsonschema

Todos los issues de python-jsonschema/jsonschema

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.