babelfish icon indicating copy to clipboard operation
babelfish copied to clipboard

Immutability issues

Open Diaoul opened this issue 2 years ago • 0 comments

Currently babelfish implements the hash function to override default behavior and make it possible for objects to be keys in dictionaries and other useful features. While this is a good thing, the way it is implemented makes it prone to some weird errors as explained in this lyft blog post:

>>> import babelfish
>>> fr = babelfish.Language("fra")
>>> fr_fr = babelfish.Language("fra", "FR")
>>> s = set([fr])
>>> fr in s
True
>>> fr_fr in s
False

All that is great, but if we modify the objects, things get weird because python expect the result of hash not to change:

>>> fr.country = babelfish.Country("FR")
>>> fr
<Language [fr-FR]>
>>> fr in s
False
>>> list(s)[0]
<Language [fr-FR]>
>>> fr_fr in s
False

I want to have true immutability of babelfish objects by making use of tuples (and derivatives) or at least faking it maybe with dataclasses frozen options. This will surely be a breaking change.

Diaoul avatar Jul 14 '21 07:07 Diaoul