M
M
marselabdullin2020-06-23 17:16:40
Python
marselabdullin, 2020-06-23 17:16:40

Why does FastAPI complain about a get request?

I decided to try a new framework for an asynchronous backend in python and ran into an error when getting a request:
TypeError: 'dict_keys' object is not subscriptable
Swears at the get_random function:

class Database:
    def __init__(self):
        self._items: typing.Dict[int, PhraseOutput] = {}

    def get_random(self) -> int:
        return random.choice(self._items.keys())

    def get(self, id: int) -> typing.Optional[PhraseOutput]:
        return self._items.get(id)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-23
@marselabdullin

random.choice(list(self._items.keys()))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question