feat: mypy for all type check (#10921)

This commit is contained in:
yihong
2024-12-24 18:38:51 +08:00
committed by GitHub
parent c91e8b1737
commit 56e15d09a9
584 changed files with 3975 additions and 2826 deletions

View File

View File

@@ -64,7 +64,7 @@ def build_from_mapping(
if not build_func:
raise ValueError(f"Invalid file transfer method: {transfer_method}")
file = build_func(
file: File = build_func(
mapping=mapping,
tenant_id=tenant_id,
transfer_method=transfer_method,
@@ -72,7 +72,7 @@ def build_from_mapping(
if config and not _is_file_valid_with_config(
input_file_type=mapping.get("type", FileType.CUSTOM),
file_extension=file.extension,
file_extension=file.extension or "",
file_transfer_method=file.transfer_method,
config=config,
):
@@ -281,6 +281,7 @@ def _get_file_type_by_extension(extension: str) -> FileType | None:
return FileType.AUDIO
elif extension in DOCUMENT_EXTENSIONS:
return FileType.DOCUMENT
return None
def _get_file_type_by_mimetype(mime_type: str) -> FileType | None:

View File

@@ -1,5 +1,5 @@
from collections.abc import Mapping, Sequence
from typing import Any
from typing import Any, cast
from uuid import uuid4
from configs import dify_config
@@ -84,6 +84,8 @@ def _build_variable_from_mapping(*, mapping: Mapping[str, Any], selector: Sequen
raise VariableError("missing value type")
if (value := mapping.get("value")) is None:
raise VariableError("missing value")
# FIXME: using Any here, fix it later
result: Any
match value_type:
case SegmentType.STRING:
result = StringVariable.model_validate(mapping)
@@ -109,7 +111,7 @@ def _build_variable_from_mapping(*, mapping: Mapping[str, Any], selector: Sequen
raise VariableError(f"variable size {result.size} exceeds limit {dify_config.MAX_VARIABLE_SIZE}")
if not result.selector:
result = result.model_copy(update={"selector": selector})
return result
return cast(Variable, result)
def build_segment(value: Any, /) -> Segment:
@@ -164,10 +166,13 @@ def segment_to_variable(
raise UnsupportedSegmentTypeError(f"not supported segment type {segment_type}")
variable_class = SEGMENT_TO_VARIABLE_MAP[segment_type]
return variable_class(
id=id,
name=name,
description=description,
value=segment.value,
selector=selector,
return cast(
Variable,
variable_class(
id=id,
name=name,
description=description,
value=segment.value,
selector=selector,
),
)