Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Create validators not tied to attributes

Abierto
#647 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
tooling

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

Feature

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

Abrir la guía de contribución

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-attrs/attrs

Todos los issues de python-attrs/attrs

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.