From 8697707083ee3786ba2cfbea204d56113a92ab0f Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Wed, 29 Nov 2023 15:40:06 -0500 Subject: [PATCH] Fix HashTrieMap __repr__s, which need to repr() the key as well. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 5 ++++- tests/test_hash_trie_map.py | 8 ++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9e52c63..01eb683 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,7 +195,7 @@ dependencies = [ [[package]] name = "rpds-py" -version = "0.13.1" +version = "0.13.2" dependencies = [ "archery", "pyo3", diff --git a/Cargo.toml b/Cargo.toml index 6d98e59..9930d0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rpds-py" -version = "0.13.1" +version = "0.13.2" edition = "2021" [lib] diff --git a/src/lib.rs b/src/lib.rs index 127f7f3..d5b3a91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,7 +133,10 @@ impl HashTrieMapPy { let contents = self.inner.into_iter().map(|(k, v)| { format!( "{}: {}", - k.clone().into_py(py), + k.inner + .call_method0(py, "__repr__") + .and_then(|r| r.extract(py)) + .unwrap_or("".to_owned()), v.call_method0(py, "__repr__") .and_then(|r| r.extract(py)) .unwrap_or("".to_owned()) diff --git a/tests/test_hash_trie_map.py b/tests/test_hash_trie_map.py index 2f3db07..a247b1b 100644 --- a/tests/test_hash_trie_map.py +++ b/tests/test_hash_trie_map.py @@ -287,6 +287,14 @@ def test_iteration_with_many_elements(): assert actual_values == set(values + [12345, 54321]) +def test_repr(): + rep = repr(HashTrieMap({"foo": "12", "": 37})) + assert rep in { + "HashTrieMap({'foo': '12', '': 37})", + "HashTrieMap({'': 37, 'foo': '12'})", + } + + def test_str(): s = str(HashTrieMap({1: 2, 3: 4})) assert s == "HashTrieMap({1: 2, 3: 4})" or s == "HashTrieMap({3: 4, 1: 2})"