Browse Source

fix: complete Python 3.8 compatibility fixes

- Fix tuple[Type[Any], ...] to Tuple[Type[Any], ...] in shared.py
- Add missing _is_model_field function to v2.py
- All tests now pass on Python 3.8 without TypeError
- Import-time compatibility fully restored for Python 3.8
pull/14201/head
Roberto Bertó 9 months ago
parent
commit
fb00f2679d
  1. 2
      fastapi/_compat/shared.py
  2. 5
      fastapi/_compat/v2.py

2
fastapi/_compat/shared.py

@ -97,7 +97,7 @@ def value_is_sequence(value: Any) -> bool:
def _annotation_is_complex(annotation: Union[Type[Any], None]) -> bool: def _annotation_is_complex(annotation: Union[Type[Any], None]) -> bool:
types_tuple: tuple[Type[Any], ...] = (BaseModel, Mapping, UploadFile) types_tuple: Tuple[Type[Any], ...] = (BaseModel, Mapping, UploadFile)
if "pydantic.v1" in sys.modules: if "pydantic.v1" in sys.modules:
# only now touch v1 (already used by app) # only now touch v1 (already used by app)
types_tuple += (_v1.BaseModel,) types_tuple += (_v1.BaseModel,)

5
fastapi/_compat/v2.py

@ -471,3 +471,8 @@ def get_long_model_name(model: TypeModelOrEnum) -> str:
def _is_model_class(value: Any) -> bool: def _is_model_class(value: Any) -> bool:
"""Check if a value is a Pydantic model class.""" """Check if a value is a Pydantic model class."""
return lenient_issubclass(value, BaseModel) return lenient_issubclass(value, BaseModel)
def _is_model_field(value: Any) -> bool:
"""Check if a value is a Pydantic model field."""
return isinstance(value, ModelField)

Loading…
Cancel
Save