Force configtype.Dict keys to be strings
This commit is contained in:
parent
41655e7852
commit
1cbb4ece4b
@ -952,6 +952,9 @@ class Dict(BaseType):
|
|||||||
|
|
||||||
def __init__(self, keytype, valtype, *, fixed_keys=None, none_ok=False):
|
def __init__(self, keytype, valtype, *, fixed_keys=None, none_ok=False):
|
||||||
super().__init__(none_ok)
|
super().__init__(none_ok)
|
||||||
|
# If the keytype is not a string, we'll get problems with showing it as
|
||||||
|
# json in to_str() as json converts keys to strings.
|
||||||
|
assert isinstance(keytype, String), keytype
|
||||||
self.keytype = keytype
|
self.keytype = keytype
|
||||||
self.valtype = valtype
|
self.valtype = valtype
|
||||||
self.fixed_keys = fixed_keys
|
self.fixed_keys = fixed_keys
|
||||||
|
@ -1370,9 +1370,9 @@ class TestDict:
|
|||||||
d.from_str(val)
|
d.from_str(val)
|
||||||
|
|
||||||
def test_from_str_int(self):
|
def test_from_str_int(self):
|
||||||
typ = configtypes.Dict(keytype=configtypes.Int(),
|
typ = configtypes.Dict(keytype=configtypes.String(),
|
||||||
valtype=configtypes.Int())
|
valtype=configtypes.Int())
|
||||||
assert typ.from_str('{0: 0}') == {0: 0}
|
assert typ.from_str('{"answer": 42}') == {"answer": 42}
|
||||||
|
|
||||||
@pytest.mark.parametrize('val', [
|
@pytest.mark.parametrize('val', [
|
||||||
{"one": "1"}, # missing key
|
{"one": "1"}, # missing key
|
||||||
@ -1389,20 +1389,24 @@ class TestDict:
|
|||||||
else:
|
else:
|
||||||
d.from_py(val)
|
d.from_py(val)
|
||||||
|
|
||||||
@hypothesis.given(val=strategies.dictionaries(strategies.booleans(),
|
@hypothesis.given(val=strategies.dictionaries(strategies.text(min_size=1),
|
||||||
strategies.booleans()))
|
strategies.booleans()))
|
||||||
def test_hypothesis(self, klass, val):
|
def test_hypothesis(self, klass, val):
|
||||||
d = klass(keytype=configtypes.Bool(),
|
d = klass(keytype=configtypes.String(),
|
||||||
valtype=configtypes.Bool(),
|
valtype=configtypes.Bool(),
|
||||||
none_ok=True)
|
none_ok=True)
|
||||||
converted = d.from_py(val)
|
try:
|
||||||
assert d.from_str(d.to_str(converted)) == converted
|
converted = d.from_py(val)
|
||||||
|
assert d.from_str(d.to_str(converted)) == converted
|
||||||
|
except configexc.ValidationError:
|
||||||
|
# Invalid unicode in the string, etc...
|
||||||
|
hypothesis.assume(False)
|
||||||
|
|
||||||
@hypothesis.given(val=strategies.dictionaries(strategies.booleans(),
|
@hypothesis.given(val=strategies.dictionaries(strategies.text(min_size=1),
|
||||||
strategies.booleans()))
|
strategies.booleans()))
|
||||||
def test_hypothesis_text(self, klass, val):
|
def test_hypothesis_text(self, klass, val):
|
||||||
text = json.dumps(val)
|
text = json.dumps(val)
|
||||||
d = klass(keytype=configtypes.Bool(), valtype=configtypes.Bool())
|
d = klass(keytype=configtypes.String(), valtype=configtypes.Bool())
|
||||||
try:
|
try:
|
||||||
converted = d.from_str(text)
|
converted = d.from_str(text)
|
||||||
except configexc.ValidationError:
|
except configexc.ValidationError:
|
||||||
|
Loading…
Reference in New Issue
Block a user