alecthomas / alecthomas/voluptuous

Hashable markers

未關閉
#87 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
1.9k
分支
237
PR 合併指標
30 天內沒有已合併 PR

描述

Currently, markers are not correctly hashable and comparable, so after a schema is defined, it's impossible to make changes to it:

``` python
from voluptuous import Optional, Any
d['a'] = Any(None, d['a'])
```

This raises an exception, since `Optional('a') != 'a'`, and their hashes differ. [Explanation](http://stackoverflow.com/questions/4901815/object-as-a-dictionary-key)

```
Traceback (most recent call last):
File "test.py", line 17, in
d['a'] = Any(None, d['a'])
KeyError: 'a'
```

I suggest to add the following to markers:

``` python
class Marker(object):
# ...
def __hash__(self):
return hash(self.schema)

def __eq__(self, other):
return self.schema == (other.schema if isinstance(other, Marker) else other)
```

Now they are correctly hashable and compare equal with strings.

There is a dangerous issue that it `schema` argument is not hashable -- it will fail.. but those markers are only used with dictionary keys anyway, which must be hashable.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。