Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Create validators not tied to attributes

Đang mở
#647 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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'!")
Ngôn ngữ chính
Python
Star
5.8k
Fork
480
Merge trung bình
2 giờ 15 phút
Pull request đã merge (30 ngày)
2

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-attrs/attrs

Tất cả issue của python-attrs/attrs

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.