Browse Source

refactor: Move type-only imports into TYPE_CHECKING blocks

Move imports that are only used for type annotations into TYPE_CHECKING
blocks to prevent unnecessary runtime imports and potential circular
import issues.

Changes:
- fastapi/openapi/utils.py: Response
- tests/test_dependency_contextmanager.py: StreamingResponse
- tests/test_route_scope.py: APIRoute

Detected via: ruff check . --select TC002
pull/15212/head
Hiten Shah 4 months ago
parent
commit
78ea8a2107
  1. 6
      fastapi/openapi/utils.py
  2. 5
      tests/test_dependency_contextmanager.py
  3. 5
      tests/test_route_scope.py

6
fastapi/openapi/utils.py

@ -3,7 +3,7 @@ import http.client
import inspect import inspect
import warnings import warnings
from collections.abc import Sequence from collections.abc import Sequence
from typing import Any, Literal, cast from typing import Any, Literal, cast, TYPE_CHECKING
from fastapi import routing from fastapi import routing
from fastapi._compat import ( from fastapi._compat import (
@ -27,7 +27,6 @@ from fastapi.exceptions import FastAPIDeprecationWarning
from fastapi.openapi.constants import METHODS_WITH_BODY, REF_PREFIX from fastapi.openapi.constants import METHODS_WITH_BODY, REF_PREFIX
from fastapi.openapi.models import OpenAPI from fastapi.openapi.models import OpenAPI
from fastapi.params import Body, ParamTypes from fastapi.params import Body, ParamTypes
from fastapi.responses import Response
from fastapi.sse import _SSE_EVENT_SCHEMA from fastapi.sse import _SSE_EVENT_SCHEMA
from fastapi.types import ModelNameMap from fastapi.types import ModelNameMap
from fastapi.utils import ( from fastapi.utils import (
@ -39,6 +38,9 @@ from pydantic import BaseModel
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
from starlette.routing import BaseRoute from starlette.routing import BaseRoute
if TYPE_CHECKING:
from fastapi.responses import Response
validation_error_definition = { validation_error_definition = {
"title": "ValidationError", "title": "ValidationError",
"type": "object", "type": "object",

5
tests/test_dependency_contextmanager.py

@ -2,8 +2,11 @@ import json
import pytest import pytest
from fastapi import BackgroundTasks, Depends, FastAPI from fastapi import BackgroundTasks, Depends, FastAPI
from fastapi.responses import StreamingResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from fastapi.responses import StreamingResponse
app = FastAPI() app = FastAPI()
state = { state = {

5
tests/test_route_scope.py

@ -1,7 +1,10 @@
import pytest import pytest
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.routing import APIRoute, APIWebSocketRoute
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from fastapi.routing import APIRoute, APIWebSocketRoute
app = FastAPI() app = FastAPI()

Loading…
Cancel
Save