vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.27k stars 432 forks source link

Pyright call issue errors for Field and Query #1335

Open krims0n32 opened 4 days ago

krims0n32 commented 4 days ago
from ninja import Query, Field

class InstrumentFilter(FilterSchema):
    symbol: Optional[str] = Field(None, q='symbol__icontains')

Here I get "No parameter named 'q'" on Field

@router.get('/instruments', response=List[InstrumentSchema])
@paginate
def instruments(request, filters: InstrumentFilter = Query(...)):
    ...

And here I get "Object of type Annotated is not callable" on Query.

I followed the docs on filtering. Am I doing something wrong here, missing a package?

Thanks!

sunfkny commented 2 days ago

No parameter named 'q'

ninja.Field is actually pydantic.Field, pydantic V2 Field extra kwargs is deprecated.

https://docs.pydantic.dev/2.9/migration/#changes-to-pydanticfield https://github.com/pydantic/pydantic/blob/8bad227e7557519307a9ea9b0ca129aca1997667/pydantic/fields.py#L1025-L1033 https://github.com/pydantic/pydantic/blob/8bad227e7557519307a9ea9b0ca129aca1997667/pydantic/fields.py#L685-L686

So this will fix this typing error

class InstrumentFilter(FilterSchema):
    symbol: str | None = Field(None, json_schema_extra={"q": "symbol__icontains"})

Object of type Annotated is not callable

https://github.com/vitalik/django-ninja/blob/master/docs/docs/whatsnew_v1.md#shorter--cleaner-parameters-syntax

You can use shorter parameters syntax to fix this typing error

@router.get("/instruments", response=List[InstrumentSchema])
@paginate
def instruments(
    request,
    filters: Query[InstrumentFilter],  # <--- !!!
): ...