Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object immutability iso639.Language #3

Closed
KonstZiv opened this issue Jun 13, 2023 · 4 comments
Closed

object immutability iso639.Language #3

KonstZiv opened this issue Jun 13, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@KonstZiv
Copy link

Objects of class iso639.Language are marked as immutable:

@dataclass(frozen=True)
class Language:
    """Represents a language in the ISO 639-3 charts."""

    __slots__ = (
        # From the "codes" table
        "part3",
...

But when you try to use them in this way (for example, as elements of a set), you can get an error if the element other_names exists. Example:

>>> import iso639
>>> cat_lng = iso639.Language.match('cat')
>>> cat_lng
Language(part3='cat', part2b='cat', part2t='cat', part1='ca', scope='I', type='L', status='A', name='Catalan', comment=None, other_names=[Name(print='Valencian', inverted='Valencian')], macrolanguage=None, retire_reason=None, retire_change_to=None, retire_remedy=None, retire_date=None)
>>> lng_set = set()
>>> lng_set.add(cat_lng)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3, in __hash__
TypeError: unhashable type: 'list'
>>> 

is it possible to replace the use of a list with a tuple to describe this attribute in order to get a fully immutable object?

@jacksonllee
Copy link
Owner

Hello, thank you for catching this! I've just created a branch for a potential fix (by explicitly defining how to hash a Language class instance). It is available for testing if you run pip install git+https://github.com/jacksonllee/iso639@make-language-instances-truly-hashable. Would you be able to see if this would resolve the issue on your end?

@jacksonllee jacksonllee added the bug Something isn't working label Jun 14, 2023
@KonstZiv
Copy link
Author

KonstZiv commented Jun 14, 2023 via email

@KonstZiv
Copy link
Author

KonstZiv commented Jun 15, 2023 via email

@jacksonllee
Copy link
Owner

Thank you for confirming that the fix would work for you! I've just made a new release to PyPI, version 2023.6.15, so running pip install --upgrade python-iso639 should get you this latest version.

Closing this issue now -- thank you again for catching and reporting this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants