Pyright flaging mixed keys dictionary as an error #9187
-
The following is a copy paste from FastAPI - Query Parameters and String Validations from typing import Annotated
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: Annotated[str | None, Query(max_length=50)] = None):
results = {"items": [{"items_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results
It seems How can I configure |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is a legitimate type violation, so pyright is correct to generate an error here. You can suppress type errors with a If your intent is for results: dict[str, object] = ... For more information about type declarations and type inference, refer to the pyright documentation. |
Beta Was this translation helpful? Give feedback.
This is a legitimate type violation, so pyright is correct to generate an error here. You can suppress type errors with a
# type: ignore
or# pyright: ignore
comments, but it's better to fix your code to eliminate the type violation. Otherwise you defeat the purpose of a type checker.If your intent is for
results
to be adict
that accepts values of any type, you can declare its type asdict[str, object]
by providing a type annotation.For more information about type declarations and type inference, refer to the pyright documentation.