ipython / ipython/traitlets

Should there be a way to disable trait notifications?

未关闭
#907 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
653
派生
217
平均合并
2 天 21 小时
30 天内合并 PR
2

描述

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:

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。