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 # path: fastapi/_compat/_v1_params.py
""" """

1
fastapi/_compat/lazy_import.py

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

3
fastapi/_compat/v1.py

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

12
fastapi/utils.py

@ -1,3 +1,4 @@
# mypy: ignore-errors
import re import re
import warnings import warnings
from dataclasses import is_dataclass from dataclasses import is_dataclass
@ -27,11 +28,6 @@ from fastapi._compat import (
lenient_issubclass, 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 fastapi.datastructures import DefaultPlaceholder, DefaultType
from pydantic import BaseModel from pydantic import BaseModel
from pydantic.fields import FieldInfo from pydantic.fields import FieldInfo
@ -40,6 +36,12 @@ from typing_extensions import Literal
if TYPE_CHECKING: # pragma: nocover if TYPE_CHECKING: # pragma: nocover
from .routing import APIRoute 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` # Cache for `create_cloned_field`
_CLONED_TYPES_CACHE: MutableMapping[Type[BaseModel], Type[BaseModel]] = ( _CLONED_TYPES_CACHE: MutableMapping[Type[BaseModel], Type[BaseModel]] = (
WeakKeyDictionary() WeakKeyDictionary()

10
tests/test_pydantic_v2_first_compat.py

@ -37,8 +37,8 @@ class TestV2FirstCompatibility:
import fastapi.encoders import fastapi.encoders
from fastapi import FastAPI from fastapi import FastAPI
app = FastAPI() _ = FastAPI() # Create app but don't use it
# Test jsonable_encoder with v2 model # Test jsonable_encoder with v2 model
class TestModel(BaseModel): class TestModel(BaseModel):
name: str name: str
@ -108,14 +108,14 @@ class TestV2FirstCompatibility:
import sys import sys
# Clear any existing pydantic.v1 from sys.modules # 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 components
import fastapi import fastapi
import fastapi.encoders import fastapi.encoders
from fastapi import FastAPI 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 # 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) # (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 # This should not raise an error unless we actually try to use v1
import fastapi import fastapi
from fastapi 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 # The strict mode only affects actual v1 usage, not imports
assert True assert True

Loading…
Cancel
Save