diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 227ad837d..9c1b37185 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -200,7 +200,7 @@ if PYDANTIC_V2: # This expects that GenerateJsonSchema was already used to generate the definitions json_schema = field_mapping[(field, override_mode or field.mode)] if "$ref" not in json_schema: - # TODO remove when deprecating Pydantic v1 + # Set title for field when not using reference # Ref: https://github.com/pydantic/pydantic/blob/d61792cc42c80b13b23e3ffa74bc37ec7c77f7d1/pydantic/schema.py#L207 json_schema["title"] = ( field.field_info.title or field.alias.title().replace("_", " ") diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 808646cc2..86a7bff7a 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -338,16 +338,22 @@ def get_openapi_path( if route.status_code is not None: status_code = str(route.status_code) else: - # It would probably make more sense for all response classes to have an - # explicit default status_code, and to extract it from them, instead of - # doing this inspection tricks, that would probably be in the future - # TODO: probably make status_code a default class attribute for all - # responses in Starlette - response_signature = inspect.signature(current_response_class.__init__) - status_code_param = response_signature.parameters.get("status_code") - if status_code_param is not None: - if isinstance(status_code_param.default, int): - status_code = str(status_code_param.default) + # Use cached attribute if available, otherwise fall back to inspection + # This improves performance by avoiding repeated signature inspection + if hasattr(current_response_class, "default_status_code"): + status_code = str(current_response_class.default_status_code) + else: + response_signature = inspect.signature( + current_response_class.__init__ + ) + status_code_param = response_signature.parameters.get("status_code") + if status_code_param is not None: + if isinstance(status_code_param.default, int): + status_code = str(status_code_param.default) + else: + status_code = "200" + else: + status_code = "200" operation.setdefault("responses", {}).setdefault(status_code, {})[ "description" ] = route.response_description diff --git a/fastapi/routing.py b/fastapi/routing.py index 54c75a027..18d22b3a3 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -365,10 +365,6 @@ def get_websocket_app( ) -> Callable[[WebSocket], Coroutine[Any, Any, Any]]: async def app(websocket: WebSocket) -> None: async with AsyncExitStack() as async_exit_stack: - # TODO: remove this scope later, after a few releases - # This scope fastapi_astack is no longer used by FastAPI, kept for - # compatibility, just in case - websocket.scope["fastapi_astack"] = async_exit_stack solved_result = await solve_dependencies( request=websocket, dependant=dependant, @@ -521,7 +517,6 @@ class APIRoute(routing.Route): # would pass the validation and be returned as is. # By being a new field, no inheritance will be passed as is. A new model # will always be created. - # TODO: remove when deprecating Pydantic v1 self.secure_cloned_response_field: Optional[ModelField] = ( create_cloned_field(self.response_field) )