FAILED test_api.py::TestApi::test_get_token - TypeError: unhashable type: 'dict'
时间: 2024-02-05 07:03:06 浏览: 23
This error occurs because you are attempting to use a dictionary as a key in a hash table, which is not possible because dictionaries are mutable and their values can change. Hash tables require keys to be immutable so that they can be hashed and looked up quickly.
To fix this error, you should convert the dictionary to an immutable data type such as a tuple, frozenset, or named tuple before using it as a key in a hash table. Alternatively, you could use a different data structure that does not require hashable keys, such as a list or a queue.
Here is an example of how you could convert a dictionary to a tuple:
```
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_tuple = tuple(sorted(my_dict.items()))
```
This converts the dictionary to a tuple of its (key, value) pairs, sorted by key, which can be used as a hashable key in a hash table.
Alternatively, you could use a frozenset:
```
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_frozenset = frozenset(my_dict.items())
```
This creates a frozenset of the (key, value) pairs, which is also immutable and can be used as a key in a hash table.