ipython / ipython/traitlets

`UseEnum` does not work with str enum

Đang mở
#827 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
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

Mô tả

Slightly adapting the `UseEnum` example to use a `str` based enum makes it fail to recognize the enum by the value:

```
import enum
from traitlets import HasTraits, UseEnum

class Color(str, enum.Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"

class MyEntity(HasTraits):
color = UseEnum(Color, default_value=Color.BLUE)

entity = MyEntity(color=Color.RED)
entity.color = Color.GREEN
entity.color = "GREEN"
entity.color = "Color.GREEN"
entity.color = "green"
assert entity.color is Color.GREEN
```

Raises:

```
❯ python test_enum.py
Traceback (most recent call last):
File "/home/mnoethe/Projects/test_enum.py", line 16, in
entity.color = "green" # USE: number (as int)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 729, in __set__
self.set(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 703, in set
new_value = self._validate(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 735, in _validate
value = self.validate(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 3582, in validate
self.error(obj, value)
File "/home/mnoethe/.local/conda/envs/cta-dev/lib/python3.9/site-packages/traitlets/traitlets.py", line 841, in error
raise TraitError(e)
traitlets.traitlets.TraitError: The 'color' trait of a MyEntity instance expected any of ['RED', 'BLUE', 'GREEN'], not the str 'green'.
```

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

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

Hướng nghiên cứu

Start by reproducing the reported UseEnum behavior with the Color enum shown in the issue, then inspect UseEnum validation and its existing tests. Add coverage for a str-based enum and verify that assigning the underlying value "green" resolves to Color.GREEN without breaking the existing enum assignments.

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

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

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.