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

Traitlets validates default even if overrided

Đang mở
#548 3 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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ 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

Bắt đầu trong traitlets.py, tại khu vực quanh HasTraits.setup_instance, instance_init và lệnh gọi _validate được hiển thị trong traceback. Tái hiện vấn đề với ví dụ Foo bằng cả Foo() và Foo(bar='test'), sau đó kiểm tra các bài kiểm thử khởi tạo trait hiện có. Được xem là hoàn tất khi giá trị được cung cấp được chấp nhận mà không xác thực giá trị mặc định không hợp lệ và không được sử dụng, trong khi trường hợp thiếu giá trị vẫn phát sinh lỗi thích hợp.

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

Mô tả

To make a required trait, I thought I could use:

from traitlets import HasTraits, Unicode

class Foo(HasTraits): 
    bar = Unicode(default_value=None, allow_none=False, required=True) 

As expected, this raises a trait error, if Foo is instantiated without arguments:

f = Foo()

But: this also raises, when bar is provided:

f = Foo(bar='test')

This error is raised:

TraitError                                Traceback (most recent call last)
<ipython-input-8-a25b82aa7a35> in <module>
----> 1 Foo(bar='test')

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in __new__(cls, *args, **kwargs)
    956         else:
    957             inst = new_meth(cls, *args, **kwargs)
--> 958         inst.setup_instance(*args, **kwargs)
    959         return inst
    960 

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
    984         self._trait_notifiers = {}
    985         self._trait_validators = {}
--> 986         super(HasTraits, self).setup_instance(*args, **kwargs)
    987 
    988     def __init__(self, *args, **kwargs):

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
    975             else:
    976                 if isinstance(value, BaseDescriptor):
--> 977                     value.instance_init(self)
    978 
    979 

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in instance_init(self, obj)
    520             if (self._dynamic_default_callable(obj) is None) \
    521                     and (self.default_value is not Undefined):
--> 522                 v = self._validate(obj, self.default_value)
    523                 if self.name is not None:
    524                     obj._trait_values[self.name] = v

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in _validate(self, obj, value)
    589             return value
    590         if hasattr(self, 'validate'):
--> 591             value = self.validate(obj, value)
    592         if obj._cross_validation_lock is False:
    593             value = self._cross_validate(obj, value)

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in validate(self, obj, value)
   2052                 msg = "Could not decode {!r} for unicode trait '{}' of {} instance."
   2053                 raise TraitError(msg.format(value, self.name, class_of(obj)))
-> 2054         self.error(obj, value)
   2055 
   2056 

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in error(self, obj, value)
    623             e = "The '%s' trait must be %s, but a value of %r was specified." \
    624                 % (self.name, self.info(), repr_type(value))
--> 625         raise TraitError(e)
    626 
    627     def get_metadata(self, key, default=None):

TraitError: The 'bar' trait of a Foo instance must be a unicode string, but a value of None <class 'NoneType'> was specified.
Ngôn ngữ chính
Python
Star
653
Fork
217
Merge trung bình
2 ngày 21 giờ
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 ipython/traitlets

Tất cả issue của ipython/traitlets

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.