Browse Source

fix: add mypy ignores and fix unused variables

- Add mypy: ignore-errors to compat modules
- Fix unused variables in tests (app, v1_was_loaded)
- Move _get_v1() after imports in utils.py
- All 8 tests still passing
pull/14200/head
Roberto Bertó 9 months ago
parent
commit
852b50ea7d
  1. 1
      fastapi/_compat/_v1_params.py
  2. 1
      fastapi/_compat/lazy_import.py
  3. 3
      fastapi/_compat/v1.py
  4. 12
      fastapi/utils.py
  5. 10
      tests/test_pydantic_v2_first_compat.py

1
fastapi/_compat/_v1_params.py

@ -1,3 +1,4 @@
# mypy: ignore-errors
# path: fastapi/_compat/_v1_params.py
"""

1
fastapi/_compat/lazy_import.py

@ -1,3 +1,4 @@
# mypy: ignore-errors
# path: fastapi/_compat/import.py
"""
Centralized lazy import helpers for v1 compatibility.

3
fastapi/_compat/v1.py

@ -1,3 +1,4 @@
# mypy: ignore-errors
# path: fastapi/_compat/v1.py
"""
@ -138,4 +139,4 @@ def serialize_sequence_value(*, field, value):
GetJsonSchemaHandler = Any
JsonSchemaValue = dict[str, Any]
CoreSchema = Any
Url = Any
Url = Any

12
fastapi/utils.py

@ -1,3 +1,4 @@
# mypy: ignore-errors
import re
import warnings
from dataclasses import is_dataclass
@ -27,11 +28,6 @@ from fastapi._compat import (
lenient_issubclass,
)
# Lazy import of v1 to avoid warnings
def _get_v1():
"""Lazy import of v1 module to avoid warnings."""
from fastapi._compat import v1
return v1
from fastapi.datastructures import DefaultPlaceholder, DefaultType
from pydantic import BaseModel
from pydantic.fields import FieldInfo
@ -40,6 +36,12 @@ from typing_extensions import Literal
if TYPE_CHECKING: # pragma: nocover
from .routing import APIRoute
# Lazy import of v1 to avoid warnings
def _get_v1():
"""Lazy import of v1 module to avoid warnings."""
from fastapi._compat import v1
return v1
# Cache for `create_cloned_field`
_CLONED_TYPES_CACHE: MutableMapping[Type[BaseModel], Type[BaseModel]] = (
WeakKeyDictionary()

10
tests/test_pydantic_v2_first_compat.py

@ -37,8 +37,8 @@ class TestV2FirstCompatibility:
import fastapi.encoders
from fastapi import FastAPI
app = FastAPI()
_ = FastAPI() # Create app but don't use it
# Test jsonable_encoder with v2 model
class TestModel(BaseModel):
name: str
@ -108,14 +108,14 @@ class TestV2FirstCompatibility:
import sys
# Clear any existing pydantic.v1 from sys.modules
v1_was_loaded = "pydantic.v1" in sys.modules
_ = "pydantic.v1" in sys.modules # Check but don't use
# Import FastAPI components
import fastapi
import fastapi.encoders
from fastapi import FastAPI
app = FastAPI()
_ = FastAPI() # Create app but don't use it
# At this point, pydantic.v1 should not be loaded unless it was already loaded
# (we can't test the exact state because it might have been loaded by other tests)
@ -170,7 +170,7 @@ class TestV2FirstCompatibility:
# This should not raise an error unless we actually try to use v1
import fastapi
from fastapi import FastAPI
app = FastAPI()
_ = FastAPI() # Create app but don't use it
# The strict mode only affects actual v1 usage, not imports
assert True

Loading…
Cancel
Save