ipython / ipython/traitlets

Should there be a way to disable trait notifications?

Đang mở
#907 1 bình luận 1 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ả

I recently had to completely disable trait notification during instantiation of a `HasTraits` child class. Holding with `hold_trait_notifications` wasn't sufficient; the notifications release when exiting the context manager was modifying attributes I didn't want to change.

I used a snippet found in the source code to implement the following context manager.

```python
import contextlib

from traitlets import HasTraits, Bunch

@contextlib.contextmanager
def disable_trait_notifications(self):
"""Temporatilly disable trait notifications."""
def ignore(change: Bunch) -> None:
pass
original_notify_change = self.notify_change
self.notify_change = ignore
try:
yield
finally:
self.notify_change = original_notify_change

# Monkey patch HasTraits
HasTraits.disable_trait_notifications = disable_trait_notifications
```

Wondering if it would make sense to have this as part of the API. Hope I'm not suggesting an anti pattern :sweat_smile:

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

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

Đánh giá

Issue này chưa được đánh giá.

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.