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
Using Python reserved keyword for parameter alias causes error on start up #1103
Comments
@cwtfp Looks like it's fastapi-pagination bug, I will take a look |
Hi @cwtfp, Looks like it's not fastapi-pagination issue, also I have found workaround. You just need to replace class FromSizeParams(BaseModel, AbstractParams):
size: int = Query(50, ge=1, le=100)
from_: int = Query(0, ge=0, validation_alias="from")
def to_raw_params(self) -> RawParams:
return RawParams(limit=self.size, offset=self.from_) |
Apologies for the delay in response. I did try that as a work around before posting the issue. However, it didn't pick up the # picks up size but not from
curl localhost:8000/api/v1/things?size=1&from=1
# does still work
curl localhost:8000/api/v1/things?size=1&from_=1 I did, however, find a work around that worked. Only drawback is that class FromSizeParams(BaseModel, AbstractParams):
size: int = Query(50, ge=1, le=100)
# labeled as depcreated and unsupported to discourage use
from_: int | None = Query(
None, ge=0, deprecated=True, description="Unsupported. Use `from` instead"
)
def to_raw_params(self) -> RawParams:
return RawParams(limit=self.size, offset=self.from_)
async def set_pagination_params(
from_workaround: Annotated[int, Query(ge=0, alias="from")] = 0,
params: FromSizeParams = Depends(),
) -> FromSizeParams:
params.from_ = from_workaround
return params
class FromSizePage(AbstractPage[T], Generic[T]):
# page stuff
__params_type__ = FromSizeParams
@router.get("/things", response_model=FromSizePage[Thing])
def get_queries(
params=Depends(set_pagination_params),
):
return paginate(..., params) Also needed some extra custom code in |
Hi @cwtfp New version |
I have requirements to implement pagination with parameters
size
andfrom
. I am forming my custom parameter class like:Pydantic does allow using a reserved keywords as an alias. However,
add_pagination(app)
fails on start up.It appears the signature of
FromSizeParams
still hasfrom_
instead of the alias, causing the key error.(*, size: typing.Annotated[int, Ge(ge=1), Le(le=100)] = 50, from_: typing.Annotated[int, Ge(ge=0)] = 0) -> None
Aliases of words that are not reserved work fine.
Keywords other than
from
like:import
anddef
also fail.Is there a good fix or work around to use a reserved python keyword as an alias?
The text was updated successfully, but these errors were encountered: