From 9e8f426d52856080ead043151763e641d01a9a05 Mon Sep 17 00:00:00 2001 From: I531058 Date: Wed, 1 Oct 2025 12:03:41 +0200 Subject: [PATCH] fix: Add type guards for BaseModel annotation in _apply_property_names_to_definitions --- fastapi/_compat.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 254bde4ab..48de1d961 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -705,11 +705,13 @@ def _apply_property_names_to_definitions( for field in fields: # Get the actual annotation (which might be the BaseModel class) annotation = field.field_info.annotation - if lenient_issubclass(annotation, BaseModel): - if annotation not in model_fields_map: - model_fields_map[annotation] = [] + if annotation is not None and lenient_issubclass(annotation, BaseModel): + # Type guard ensures annotation is a valid BaseModel type + model_annotation = cast(Type[BaseModel], annotation) + if model_annotation not in model_fields_map: + model_fields_map[model_annotation] = [] # Get fields from the model itself, not the field that references it - model_fields_map[annotation].extend(get_model_fields(annotation)) + model_fields_map[model_annotation].extend(get_model_fields(model_annotation)) # Apply PropertyNames constraints to definitions for model_class, model_field_list in model_fields_map.items():