Skip to content

Commit 778b78f

Browse files
committed
Better hashing
1 parent be9b9dd commit 778b78f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

adafruit_connection_manager.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ def create_fake_ssl_context(
111111

112112

113113
def _get_radio_hash_key(radio):
114-
class_name = radio.__class__.__name__
115-
# trying to use wifi.radio as a key results in:
116-
# TypeError: unsupported type for __hash__: 'Radio'
117-
# So just use the class name in this case
118-
return class_name if class_name == "Radio" else radio
114+
try:
115+
return hash(radio)
116+
except TypeError:
117+
return radio.__class__.__name__
119118

120119

121120
def get_radio_socketpool(radio):

tests/get_radio_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
import adafruit_connection_manager
1414

1515

16+
def test__get_radio_hash_key():
17+
radio = mocket.MockRadio.Radio()
18+
assert adafruit_connection_manager._get_radio_hash_key(radio) == hash(radio)
19+
20+
21+
def test__get_radio_hash_key_not_hashable():
22+
radio = mocket.MockRadio.Radio()
23+
24+
with mock.patch("builtins.hash", side_effect=TypeError()):
25+
assert adafruit_connection_manager._get_radio_hash_key(radio) == "Radio"
26+
27+
1628
def test_get_radio_socketpool_wifi( # pylint: disable=unused-argument
1729
circuitpython_socketpool_module,
1830
):

0 commit comments

Comments
 (0)