Browse Source

fix: formats missing `type_` argument in `FastAPIError`

Previously the error messages raised when a complex type
wasn't a pydantic `BaseModel` included "...that {type_} is..."
instead of including the true argument / return type.

This made debugging the use of complex, non-pydantic types
difficult, especially in deeply nested dependencies.
pull/14416/head
Alex Colby 8 months ago
committed by GitHub
parent
commit
2d24b5e11b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      fastapi/utils.py

6
fastapi/utils.py

@ -110,7 +110,7 @@ def create_model_field(
try:
return v1.ModelField(**v1_kwargs) # type: ignore[no-any-return]
except RuntimeError:
raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None
raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_)) from None
elif PYDANTIC_V2:
from ._compat import v2
@ -121,7 +121,7 @@ def create_model_field(
try:
return v2.ModelField(**kwargs) # type: ignore[return-value,arg-type]
except PydanticSchemaGenerationError:
raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None
raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_) from None
# Pydantic v2 is not installed, but it's not a Pydantic v1 ModelField, it could be
# a Pydantic v1 type, like a constrained int
from fastapi._compat import v1
@ -129,7 +129,7 @@ def create_model_field(
try:
return v1.ModelField(**v1_kwargs) # type: ignore[no-any-return]
except RuntimeError:
raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None
raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_) from None
def create_cloned_field(

Loading…
Cancel
Save