Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyright call issue errors for Field and Query #1335

Open
krims0n32 opened this issue Nov 11, 2024 · 2 comments
Open

Pyright call issue errors for Field and Query #1335

krims0n32 opened this issue Nov 11, 2024 · 2 comments

Comments

@krims0n32
Copy link

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
Copy link
Contributor

sunfkny commented Nov 13, 2024

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],  # <--- !!!
): ...

@krims0n32
Copy link
Author

Thanks, that fixes the Pyright complaints. I take it the NinjaAPI docs on this topic need a little update then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants