Browse Source

fix: use Python 3.8 compatible type annotations in stubs

- Replace modern type syntax (dict[str, Any]) with legacy syntax (Dict[str, Any])
- Add UP006 exceptions for .pyi files to allow legacy syntax for Python 3.8 compatibility
- Fixes TypeError: 'type' object is not subscriptable in Python 3.8 tests
- Maintains compatibility with Python 3.8+ while keeping modern syntax in main code
pull/14201/head
Roberto Bertó 9 months ago
parent
commit
24835120ba
  1. 30
      fastapi/_compat/v1.pyi
  2. 26
      fastapi/_compat/v2.pyi
  3. 2
      pyproject.toml

30
fastapi/_compat/v1.pyi

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Dict, List, Tuple
from typing_extensions import Literal
@ -21,20 +21,10 @@ def _model_dump(
) -> Any: ...
def _get_model_config(model: Any) -> Any: ...
def _normalize_errors(errors: Any) -> Any: ...
def _regenerate_error_with_loc(
*, errors: Any, loc_prefix: tuple[Any, ...]
) -> list[dict[str, Any]]: ...
def get_schema_from_model_field(
*,
field: Any,
model_name_map: Any,
field_mapping: Any,
separate_input_output_schemas: bool = True,
) -> dict[str, Any]: ...
def get_definitions(
*, fields: Any, model_name_map: Any, separate_input_output_schemas: bool = True
) -> tuple[Any, dict[str, dict[str, Any]]]: ...
def get_model_fields(model: Any) -> list[Any]: ...
def _regenerate_error_with_loc(*, errors: Any, loc_prefix: Tuple[Any, ...]) -> List[Dict[str, Any]]: ...
def get_schema_from_model_field(*, field: Any, model_name_map: Any, field_mapping: Any, separate_input_output_schemas: bool=True) -> Dict[str, Any]: ...
def get_definitions(*, fields: Any, model_name_map: Any, separate_input_output_schemas: bool=True) -> Tuple[Any, Dict[str, Dict[str, Any]]]: ...
def get_model_fields(model: Any) -> List[Any]: ...
def is_bytes_field(field: Any) -> bool: ...
def is_bytes_sequence_field(field: Any) -> bool: ...
def is_scalar_field(field: Any) -> bool: ...
@ -42,13 +32,9 @@ def is_scalar_sequence_field(field: Any) -> bool: ...
def is_sequence_field(field: Any) -> bool: ...
def copy_field_info(field_info: Any, annotation: Any, **kwargs: Any) -> Any: ...
def create_body_model(fields: Any, model_name: str) -> Any: ...
def evaluate_forwardref(
type_: Any, globalns: dict[str, Any], localns: dict[str, Any]
) -> Any: ...
def get_annotation_from_field_info(
annotation: Any, field_info: Any, field_name: str
) -> Any: ...
def get_missing_field_error(loc: tuple[str, ...], field: Any) -> dict[str, Any]: ...
def evaluate_forwardref(type_: Any, globalns: Dict[str, Any], localns: Dict[str, Any]) -> Any: ...
def get_annotation_from_field_info(annotation: Any, field_info: Any, field_name: str) -> Any: ...
def get_missing_field_error(loc: Tuple[str, ...], field: Any) -> Dict[str, Any]: ...
def serialize_sequence_value(*, field: Any, value: Any) -> Any: ...
def with_info_plain_validator_function(func: Any) -> Any: ...
def get_flat_models_from_fields(fields: Any, known_models: Any) -> Any: ...

26
fastapi/_compat/v2.pyi

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Dict, List, Tuple
class BaseConfig: ...
class FieldInfo: ...
@ -12,27 +12,15 @@ class Validator: ...
def _is_model_class(value: Any) -> bool: ...
def _model_rebuild(model: Any) -> None: ...
def evaluate_forwardref(
type_: Any, globalns: dict[str, Any], localns: dict[str, Any]
) -> Any: ...
def evaluate_forwardref(type_: Any, globalns: Dict[str, Any], localns: Dict[str, Any]) -> Any: ...
def _get_model_config(model: Any) -> Any: ...
def _model_dump(model: Any, **kwargs: Any) -> Any: ...
def copy_field_info(field_info: Any, annotation: Any, **kwargs: Any) -> Any: ...
def create_body_model(fields: Any, model_name: str) -> Any: ...
def get_annotation_from_field_info(
annotation: Any, field_info: Any, field_name: str
) -> Any: ...
def get_definitions(
*, fields: Any, model_name_map: Any, separate_input_output_schemas: bool = True
) -> tuple[Any, dict[str, dict[str, Any]]]: ...
def get_missing_field_error(loc: tuple[str, ...], field: Any) -> dict[str, Any]: ...
def get_schema_from_model_field(
*,
field: Any,
model_name_map: Any,
field_mapping: Any,
separate_input_output_schemas: bool = True,
) -> dict[str, Any]: ...
def get_annotation_from_field_info(annotation: Any, field_info: Any, field_name: str) -> Any: ...
def get_definitions(*, fields: Any, model_name_map: Any, separate_input_output_schemas: bool=True) -> Tuple[Any, Dict[str, Dict[str, Any]]]: ...
def get_missing_field_error(loc: Tuple[str, ...], field: Any) -> Dict[str, Any]: ...
def get_schema_from_model_field(*, field: Any, model_name_map: Any, field_mapping: Any, separate_input_output_schemas: bool=True) -> Dict[str, Any]: ...
def is_bytes_field(field: Any) -> bool: ...
def is_bytes_sequence_field(field: Any) -> bool: ...
def is_scalar_field(field: Any) -> bool: ...
@ -44,4 +32,4 @@ def get_flat_models_from_fields(fields: Any, known_models: Any) -> Any: ...
def get_model_name_map(models: Any) -> Any: ...
def _is_error_wrapper(exc: Any) -> bool: ...
def _is_model_field(value: Any) -> bool: ...
def get_model_fields(model: Any) -> list[Any]: ...
def get_model_fields(model: Any) -> List[Any]: ...

2
pyproject.toml

@ -238,6 +238,8 @@ ignore = [
"docs_src/custom_response/tutorial007.py" = ["B007"]
"docs_src/dataclasses/tutorial003.py" = ["I001"]
"fastapi/_compat/__init__.py" = ["I001"]
"fastapi/_compat/v1.pyi" = ["UP006"]
"fastapi/_compat/v2.pyi" = ["UP006"]
"docs_src/path_operation_advanced_configuration/tutorial007.py" = ["B904"]
"docs_src/path_operation_advanced_configuration/tutorial007_pv1.py" = ["B904"]
"docs_src/custom_request_and_route/tutorial002.py" = ["B904"]

Loading…
Cancel
Save