alecthomas / alecthomas/voluptuous

Hashable markers

Aperta
#87 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
1.9k
Fork
237
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.