Reputation: 2094
Why does mypy
say that the value returned by the fetch_one
method of the databases
library is not indexable? What should I do in this case to resolve this error?
async def get_reaction_by_name(name: str, user_id: str) -> Optional[Mapping[Any, Any]]:
query = reactions.select().where(
(reactions.c.name == name) & (reactions.c.user_id == user_id)
)
return await database.fetch_one(query)
...
reaction = await get_reaction_by_name(name=name, user_id=user_id)
reaction["name"]
# mypy: Value of type "Optional[Mapping[Any, Any]]" is not indexable
Upvotes: 0
Views: 2013
Reputation: 10989
What mypy says us is that since function returns Optional[...]
before treating its result as a ...
(Mapping[Any, Any]
in given snippet) we should check whether it's not a None
like
reaction = await get_reaction_by_name(name=name, user_id=user_id)
if reaction is not None:
reaction["name"]
Upvotes: 2