Create validators not tied to attributes
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 35/100
Línea de trabajo
The issue names no files or tests; start by reviewing the existing attribute-bound validator behavior shown through @x.validator. Compare it with the proposed class-level @validator and determine how cross-attribute checks should run. Done means a generic validator can enforce the relationship between x and y without being attached to only one attribute, with the behavior covered by tests.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Currently if I need to add a validator to check a condition that affects two attributes, I have to make it a validator of one of them. For example:
@attr.s
class C(object):
x = attr.ib()
y = attr.ib()
@x.validator
def x_smaller_than_y(self, _ , value):
if value >= self.y:
raise ValueError("'x' has to be smaller than 'y'!")
This feels a little unintuitive because this validator is not really a validator on just 'x'. It should be a validator on 'x' and 'y'. It made me wonder if the order of writing x and y in the class matters, in case the validator is run right after an attribute is set.
I wonder what folks think about a generic validator of this form:
@attr.s
class C(object):
x = attr.ib()
y = attr.ib()
@validator
def x_smaller_than_y(self):
if self.x >= self.y:
raise ValueError("'x' has to be smaller than 'y'!")
- Lenguaje dominante
- Python
- Estrellas
- 5.8k
- Forks
- 480
- Merge medio
- 2 h 15 min
- PR fusionados (30 d)
- 2
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python-attrs/attrs
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 20/100
python-attrs/attrs#1620 ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
python-attrs/attrs#1596 · 2 comentarios · 1 reacción ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
python-attrs/attrs#1549 · 3 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 38/100
python-attrs/attrs#1543 · 2 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
python-attrs/attrs#1532 ·
Todos los issues de python-attrs/attrs
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
stephrobert/dsoxlab#238 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
sublimehq/package_control#1780 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
nwg-piotr/nwg-displays#145 ·