Closed andre-merzky closed 1 year ago
have a general wondering - should we consider something as the following case (speaking about reassignment and changing the type of a value if needed)
c = ru.zmq.RegistryClient(url=r.addr) c.put('foo.bar.buz', 11) c.put('foo.bar.buz.biz', 42)
which will raise an exception
..... this[leaf] = val TypeError: 'int' object does not support item assignment
but the following will pass as expected
c.put('foo.bar.buz', 11) c.put('foo.bar.buz', {'biz': 42})
You are right, but I think the behavior is consistent: if a leaf is treated as a path element, an exception is appropriate. I don't think there is a good way to guess the user intention.
In the second case, the path is explicitly pruned and replaced by a dict, so that's all right in my book. The basically same behavior is what you would get on a standard dictionary:
#!/usr/bin/env python3
c = {'foo': {'bar': {'buz': None}}}
# this works
c['foo']['bar']['buz'] = 11
c['foo']['bar']['buz'] = {'biz': 42}
# this raises
c['foo']['bar']['buz'] = 11
c['foo']['bar']['buz']['biz'] = 42
$ python3 t.py
Traceback (most recent call last):
File "t.py", line 12, in <module>
c['foo']['bar']['buz']['biz'] = 42
TypeError: 'int' object does not support item assignment
The important change is on the
put()
method where the linethis = this[elem]
needed to be de-indented. The original code failed when adding keys to an existing entry - the keys were placed in root instead.