Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
3 changes: 1 addition & 2 deletions docs_src/query_params_str_validations/tutorial006d.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from fastapi import FastAPI, Query
from pydantic import Required

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default=Required, min_length=3)):
async def read_items(q: str = Query(default=..., min_length=3)):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
Expand Down
3 changes: 1 addition & 2 deletions docs_src/query_params_str_validations/tutorial006d_an.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from fastapi import FastAPI, Query
from pydantic import Required
from typing_extensions import Annotated

app = FastAPI()


@app.get("/items/")
async def read_items(q: Annotated[str, Query(min_length=3)] = Required):
async def read_items(q: Annotated[str, Query(min_length=3)] = ...):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from typing import Annotated

from fastapi import FastAPI, Query
from pydantic import Required

app = FastAPI()


@app.get("/items/")
async def read_items(q: Annotated[str, Query(min_length=3)] = Required):
async def read_items(q: Annotated[str, Query(min_length=3)] = ...):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples seemed old, and I don't see them in the published docs. Guidance wanted on whether these need to be updated, deleted, or left untouched.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been deleted from the (English) docs in PR #10469, but the Russian documentation does in fact still reference it in docs/ru/tutorial/query-params-str-validations.md. Ideally, we probably should remove the reference from the Russian docs as well, and then remove this file altogether, but I would suggest to do that in a separate PR to avoid mixing up different issues - it's always easier to review atomic PRs.

results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
Expand Down
9 changes: 5 additions & 4 deletions fastapi/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
general_plain_validator_function as with_info_plain_validator_function, # noqa: F401
)

Required = PydanticUndefined
RequiredParam = PydanticUndefined
Undefined = PydanticUndefined
UndefinedType = PydanticUndefinedType
evaluate_forwardref = eval_type_lenient
Expand Down Expand Up @@ -313,9 +313,10 @@ def get_model_fields(model: Type[BaseModel]) -> List[ModelField]:
from pydantic.fields import ( # type: ignore[no-redef,attr-defined]
ModelField as ModelField, # noqa: F401
)
from pydantic.fields import ( # type: ignore[no-redef,attr-defined]
Required as Required, # noqa: F401
)

# Keeping old "Required" functionality from Pydantic V1, without
# shadowing typing.Required.
RequiredParam: Any = Ellipsis # type: ignore[no-redef]
from pydantic.fields import ( # type: ignore[no-redef,attr-defined]
Undefined as Undefined,
)
Expand Down
14 changes: 8 additions & 6 deletions fastapi/dependencies/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
PYDANTIC_V2,
ErrorWrapper,
ModelField,
Required,
RequiredParam,
Undefined,
_regenerate_error_with_loc,
copy_field_info,
Expand Down Expand Up @@ -368,15 +368,17 @@ def analyze_param(
field_info = copy_field_info(
field_info=fastapi_annotation, annotation=use_annotation
)
assert field_info.default is Undefined or field_info.default is Required, (
assert (
field_info.default is Undefined or field_info.default is RequiredParam
), (
f"`{field_info.__class__.__name__}` default value cannot be set in"
f" `Annotated` for {param_name!r}. Set the default value with `=` instead."
)
if value is not inspect.Signature.empty:
assert not is_path_param, "Path parameters cannot have default values"
field_info.default = value
else:
field_info.default = Required
field_info.default = RequiredParam
# Get Annotated Depends
elif isinstance(fastapi_annotation, params.Depends):
depends = fastapi_annotation
Expand Down Expand Up @@ -425,9 +427,9 @@ def analyze_param(
), f"Cannot specify FastAPI annotation for type {type_annotation!r}"
# Handle default assignations, neither field_info nor depends was not found in Annotated nor default value
elif field_info is None and depends is None:
default_value = value if value is not inspect.Signature.empty else Required
default_value = value if value is not inspect.Signature.empty else RequiredParam
if is_path_param:
# We might check here that `default_value is Required`, but the fact is that the same
# We might check here that `default_value is RequiredParam`, but the fact is that the same
# parameter might sometimes be a path parameter and sometimes not. See
# `tests/test_infer_param_optionality.py` for an example.
field_info = params.Path(annotation=use_annotation)
Expand Down Expand Up @@ -471,7 +473,7 @@ def analyze_param(
type_=use_annotation_from_field_info,
default=field_info.default,
alias=alias,
required=field_info.default in (Required, Undefined),
required=field_info.default in (RequiredParam, Undefined),
field_info=field_info,
)
if is_path_param:
Expand Down