chore: add ast-grep rule to convert Optional[T] to T | None (#25560)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
-LAN- 2025-09-15 13:06:33 +08:00 • committed by GitHub
commit bab4975809
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
394 changed files with 2555 additions and 2792 deletions

View file

@ -1,11 +1,9 @@
from typing import Optional
from pydantic import BaseModel
class PreviewDetail(BaseModel):
content: str
child_chunks: Optional[list[str]] = None
child_chunks: list[str] | None = None
class QAPreviewDetail(BaseModel):
@ -16,4 +14,4 @@ class QAPreviewDetail(BaseModel):
class IndexingEstimate(BaseModel):
total_segments: int
preview: list[PreviewDetail]
qa_preview: Optional[list[QAPreviewDetail]] = None
qa_preview: list[QAPreviewDetail] | None = None

View file

@ -1,6 +1,5 @@
from collections.abc import Sequence
from enum import StrEnum, auto
from typing import Optional
from pydantic import BaseModel, ConfigDict
@ -29,8 +28,8 @@ class SimpleModelProviderEntity(BaseModel):
provider: str
label: I18nObject
icon_small: Optional[I18nObject] = None
icon_large: Optional[I18nObject] = None
icon_small: I18nObject | None = None
icon_large: I18nObject | None = None
supported_model_types: list[ModelType]
def __init__(self, provider_entity: ProviderEntity):
@ -92,8 +91,8 @@ class DefaultModelProviderEntity(BaseModel):
provider: str
label: I18nObject
icon_small: Optional[I18nObject] = None
icon_large: Optional[I18nObject] = None
icon_small: I18nObject | None = None
icon_large: I18nObject | None = None
supported_model_types: Sequence[ModelType] = []

View file

@ -4,7 +4,6 @@ import re
from collections import defaultdict
from collections.abc import Iterator, Sequence
from json import JSONDecodeError
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
from sqlalchemy import func, select
@ -92,7 +91,7 @@ class ProviderConfiguration(BaseModel):
):
self.provider.configurate_methods.append(ConfigurateMethod.PREDEFINED_MODEL)
def get_current_credentials(self, model_type: ModelType, model: str) -> Optional[dict]:
def get_current_credentials(self, model_type: ModelType, model: str) -> dict | None:
"""
Get current credentials.
@ -165,7 +164,7 @@ class ProviderConfiguration(BaseModel):
return credentials
def get_system_configuration_status(self) -> Optional[SystemConfigurationStatus]:
def get_system_configuration_status(self) -> SystemConfigurationStatus | None:
"""
Get system configuration status.
:return:
@ -793,9 +792,7 @@ class ProviderConfiguration(BaseModel):
stmt = stmt.where(ProviderModelCredential.id != exclude_id)
return session.execute(stmt).scalar_one_or_none() is not None
def get_custom_model_credential(
self, model_type: ModelType, model: str, credential_id: str | None
) -> Optional[dict]:
def get_custom_model_credential(self, model_type: ModelType, model: str, credential_id: str | None) -> dict | None:
"""
Get custom model credentials.
@ -1272,7 +1269,7 @@ class ProviderConfiguration(BaseModel):
return model_setting
def get_provider_model_setting(self, model_type: ModelType, model: str) -> Optional[ProviderModelSetting]:
def get_provider_model_setting(self, model_type: ModelType, model: str) -> ProviderModelSetting | None:
"""
Get provider model setting.
:param model_type: model type
@ -1448,7 +1445,7 @@ class ProviderConfiguration(BaseModel):
def get_provider_model(
self, model_type: ModelType, model: str, only_active: bool = False
) -> Optional[ModelWithProviderEntity]:
) -> ModelWithProviderEntity | None:
"""
Get provider model.
:param model_type: model type
@ -1465,7 +1462,7 @@ class ProviderConfiguration(BaseModel):
return None
def get_provider_models(
self, model_type: Optional[ModelType] = None, only_active: bool = False, model: Optional[str] = None
self, model_type: ModelType | None = None, only_active: bool = False, model: str | None = None
) -> list[ModelWithProviderEntity]:
"""
Get provider models.
@ -1649,7 +1646,7 @@ class ProviderConfiguration(BaseModel):
model_types: Sequence[ModelType],
provider_schema: ProviderEntity,
model_setting_map: dict[ModelType, dict[str, ModelSettings]],
model: Optional[str] = None,
model: str | None = None,
) -> list[ModelWithProviderEntity]:
"""
Get custom provider models.
@ -1783,7 +1780,7 @@ class ProviderConfigurations(BaseModel):
super().__init__(tenant_id=tenant_id)
def get_models(
self, provider: Optional[str] = None, model_type: Optional[ModelType] = None, only_active: bool = False
self, provider: str | None = None, model_type: ModelType | None = None, only_active: bool = False
) -> list[ModelWithProviderEntity]:
"""
Get available models.

View file

@ -1,5 +1,5 @@
from enum import StrEnum, auto
from typing import Optional, Union
from typing import Union
from pydantic import BaseModel, ConfigDict, Field
@ -49,7 +49,7 @@ class SystemConfigurationStatus(StrEnum):
class RestrictModel(BaseModel):
model: str
base_model_name: Optional[str] = None
base_model_name: str | None = None
model_type: ModelType
# pydantic configs
@ -84,9 +84,9 @@ class SystemConfiguration(BaseModel):
"""
enabled: bool
current_quota_type: Optional[ProviderQuotaType] = None
current_quota_type: ProviderQuotaType | None = None
quota_configurations: list[QuotaConfiguration] = []
credentials: Optional[dict] = None
credentials: dict | None = None
class CustomProviderConfiguration(BaseModel):
@ -95,8 +95,8 @@ class CustomProviderConfiguration(BaseModel):
"""
credentials: dict
current_credential_id: Optional[str] = None
current_credential_name: Optional[str] = None
current_credential_id: str | None = None
current_credential_name: str | None = None
available_credentials: list[CredentialConfiguration] = []
@ -108,10 +108,10 @@ class CustomModelConfiguration(BaseModel):
model: str
model_type: ModelType
credentials: dict | None = None
current_credential_id: Optional[str] = None
current_credential_name: Optional[str] = None
current_credential_id: str | None = None
current_credential_name: str | None = None
available_model_credentials: list[CredentialConfiguration] = []
unadded_to_model_list: Optional[bool] = False
unadded_to_model_list: bool | None = False
# pydantic configs
model_config = ConfigDict(protected_namespaces=())
@ -131,7 +131,7 @@ class CustomConfiguration(BaseModel):
Model class for provider custom configuration.
"""
provider: Optional[CustomProviderConfiguration] = None
provider: CustomProviderConfiguration | None = None
models: list[CustomModelConfiguration] = []
can_added_models: list[UnaddedModelConfiguration] = []
@ -205,12 +205,12 @@ class ProviderConfig(BasicProviderConfig):
scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
required: bool = False
default: Optional[Union[int, str, float, bool]] = None
options: Optional[list[Option]] = None
label: Optional[I18nObject] = None
help: Optional[I18nObject] = None
url: Optional[str] = None
placeholder: Optional[I18nObject] = None
default: Union[int, str, float, bool] | None = None
options: list[Option] | None = None
label: I18nObject | None = None
help: I18nObject | None = None
url: str | None = None
placeholder: I18nObject | None = None
def to_basic_provider_config(self) -> BasicProviderConfig:
return BasicProviderConfig(type=self.type, name=self.name)