From 86a14756f8ea66afb5fcffeab1eebdfa3698cbfc Mon Sep 17 00:00:00 2001 From: moi15moi Date: Sat, 7 Sep 2024 17:37:26 -0400 Subject: [PATCH 1/2] Rework Language.__hash__ Close #19 --- langcodes/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langcodes/__init__.py b/langcodes/__init__.py index 4d874630..7d78d3e6 100644 --- a/langcodes/__init__.py +++ b/langcodes/__init__.py @@ -1501,7 +1501,7 @@ def __eq__(self, other): return self._str_tag == other._str_tag def __hash__(self) -> int: - return hash(id(self)) + return hash(self._str_tag) def __getitem__(self, key: str) -> Optional[Union[str, List[str]]]: if key in self.ATTRIBUTES: From b21c2442ca3c00d015903c91bb7262f892619749 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Mon, 9 Sep 2024 19:47:15 -0400 Subject: [PATCH 2/2] [test_language] Add test__hash__ --- langcodes/tests/test_language.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 langcodes/tests/test_language.py diff --git a/langcodes/tests/test_language.py b/langcodes/tests/test_language.py new file mode 100644 index 00000000..e96af933 --- /dev/null +++ b/langcodes/tests/test_language.py @@ -0,0 +1,16 @@ +from langcodes import Language + + +def test__hash__(): + en1 = Language.get("en") + # Disable caching + Language._INSTANCES = {} + Language._PARSE_CACHE = {} + en2 = Language.get("en") + assert hash(en1) == hash(en2) + + # Again, disable caching + Language._INSTANCES = {} + Language._PARSE_CACHE = {} + en_us = Language.get("en-US") + assert hash(en1) != hash(en_us)