swaroopch / edn_format

EDN reader and writer implementation in Python, using PLY (lex, yacc)
https://www.swaroopch.com/Wrote-an-EDN-format-reader-and-writer-in-Python-11e0924249b181e3af8bdb9af1456373
Other
134 stars 31 forks source link

vectors are parsed as mutable lists #32

Closed LeXofLeviafan closed 6 years ago

LeXofLeviafan commented 8 years ago

Now, emitting mutable values may or may not be an issue depending on the point of view, but since you went to the trouble of implementing immutable dicts, you probably want them to be completely immutable.

data = edn_format.loads('{5 [1 2 3]}')
print( type(data) ) # <class 'edn_format.immutable_dict.ImmutableDict'>
data[5].append(8)
print(data) # {5: [1, 2, 3, 8]}

...Also, Python lists are unhashable, so both "{[1 2 3] 5}" and "#{[1 2 3] 5}" generate TypeError.

Proposed solution is to add a corresponding class (named, for example, ImmutableVector), implemented in similar fashion to ImmutableDict; however, whether it extends list or not (it would make sense to implement it using tuples), it would break code that mutates parsed vectors.

swaroopch commented 8 years ago

@LeXofLeviafan I'm not actively using EDN any more, so I don't know if I'll get around to this. In the meanwhile, pull requests are welcome.

bfontaine commented 6 years ago

Fixes by #50.

adam0z commented 4 years ago

@swaroopch Why have you decided to implement and use ImmutableDict and ImmutableList instead of plain dict and list?

swaroopch commented 4 years ago

@adam0z

Why have you decided to implement and use ImmutableDict and ImmutableList instead of plain dict and list?

The change was made in https://github.com/swaroopch/edn_format/commit/84c308300355b68af144a083fb32078d02d9472c and I don't think the reason was documented.

IIRC, it was related to https://github.com/edn-format/edn#rationale :

edn will yield distinct object identities when read, unless a reader implementation goes out of its way to make such a promise. Thus the resulting values should be considered immutable, and a reader implementation should yield values that ensure this, to the extent possible.