mirror of
https://github.com/fastapi/fastapi.git
synced 2026-09-14 13:36:21 +08:00
👷 Run mypy by pre-commit (#14806)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -30,6 +30,13 @@ repos:
|
|||||||
language: unsupported
|
language: unsupported
|
||||||
types: [python]
|
types: [python]
|
||||||
|
|
||||||
|
- id: local-mypy
|
||||||
|
name: mypy check
|
||||||
|
entry: uv run mypy fastapi
|
||||||
|
require_serial: true
|
||||||
|
language: unsupported
|
||||||
|
pass_filenames: false
|
||||||
|
|
||||||
- id: add-permalinks-pages
|
- id: add-permalinks-pages
|
||||||
language: unsupported
|
language: unsupported
|
||||||
name: add-permalinks-pages
|
name: add-permalinks-pages
|
||||||
|
|||||||
@@ -477,7 +477,7 @@ def get_model_fields(model: type[BaseModel]) -> list[ModelField]:
|
|||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def get_cached_model_fields(model: type[BaseModel]) -> list[ModelField]:
|
def get_cached_model_fields(model: type[BaseModel]) -> list[ModelField]:
|
||||||
return get_model_fields(model) # type: ignore[return-value]
|
return get_model_fields(model)
|
||||||
|
|
||||||
|
|
||||||
# Duplicate of several schema functions from Pydantic v1 to make them compatible with
|
# Duplicate of several schema functions from Pydantic v1 to make them compatible with
|
||||||
@@ -500,13 +500,13 @@ def get_model_name_map(unique_models: TypeModelSet) -> dict[TypeModelOrEnum, str
|
|||||||
|
|
||||||
|
|
||||||
def get_compat_model_name_map(fields: list[ModelField]) -> ModelNameMap:
|
def get_compat_model_name_map(fields: list[ModelField]) -> ModelNameMap:
|
||||||
all_flat_models = set()
|
all_flat_models: TypeModelSet = set()
|
||||||
|
|
||||||
v2_model_fields = [field for field in fields if isinstance(field, ModelField)]
|
v2_model_fields = [field for field in fields if isinstance(field, ModelField)]
|
||||||
v2_flat_models = get_flat_models_from_fields(v2_model_fields, known_models=set())
|
v2_flat_models = get_flat_models_from_fields(v2_model_fields, known_models=set())
|
||||||
all_flat_models = all_flat_models.union(v2_flat_models) # type: ignore[arg-type]
|
all_flat_models = all_flat_models.union(v2_flat_models)
|
||||||
|
|
||||||
model_name_map = get_model_name_map(all_flat_models) # type: ignore[arg-type]
|
model_name_map = get_model_name_map(all_flat_models)
|
||||||
return model_name_map
|
return model_name_map
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -399,7 +399,7 @@ def analyze_param(
|
|||||||
if isinstance(fastapi_annotation, FieldInfo):
|
if isinstance(fastapi_annotation, FieldInfo):
|
||||||
# Copy `field_info` because we mutate `field_info.default` below.
|
# Copy `field_info` because we mutate `field_info.default` below.
|
||||||
field_info = copy_field_info(
|
field_info = copy_field_info(
|
||||||
field_info=fastapi_annotation, # type: ignore[arg-type]
|
field_info=fastapi_annotation,
|
||||||
annotation=use_annotation,
|
annotation=use_annotation,
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
@@ -433,7 +433,7 @@ def analyze_param(
|
|||||||
"Cannot specify FastAPI annotations in `Annotated` and default value"
|
"Cannot specify FastAPI annotations in `Annotated` and default value"
|
||||||
f" together for {param_name!r}"
|
f" together for {param_name!r}"
|
||||||
)
|
)
|
||||||
field_info = value # type: ignore[assignment]
|
field_info = value
|
||||||
if isinstance(field_info, FieldInfo):
|
if isinstance(field_info, FieldInfo):
|
||||||
field_info.annotation = type_annotation
|
field_info.annotation = type_annotation
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -90,7 +90,7 @@ def create_model_field(
|
|||||||
field_info = field_info or FieldInfo(annotation=type_, default=default, alias=alias)
|
field_info = field_info or FieldInfo(annotation=type_, default=default, alias=alias)
|
||||||
kwargs = {"mode": mode, "name": name, "field_info": field_info}
|
kwargs = {"mode": mode, "name": name, "field_info": field_info}
|
||||||
try:
|
try:
|
||||||
return v2.ModelField(**kwargs) # type: ignore[return-value,arg-type]
|
return v2.ModelField(**kwargs) # type: ignore[arg-type]
|
||||||
except PydanticSchemaGenerationError:
|
except PydanticSchemaGenerationError:
|
||||||
raise fastapi.exceptions.FastAPIError(
|
raise fastapi.exceptions.FastAPIError(
|
||||||
_invalid_args_message.format(type_=type_)
|
_invalid_args_message.format(type_=type_)
|
||||||
|
|||||||
Reference in New Issue
Block a user