Skip to content

Commit 843280f

Browse files
enhancement: introduce Ruff for Python linter for reordering and removing unused imports with automated pre-commit and sytle check (langgenius#2366)
1 parent 4234479 commit 843280f

File tree

333 files changed

+2097
-1038
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

333 files changed

+2097
-1038
lines changed

.github/workflows/style.yml

+24
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,33 @@ concurrency:
1010
cancel-in-progress: true
1111

1212
jobs:
13+
python-style:
14+
name: Python Style
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: '3.10'
25+
26+
- name: Python dependencies
27+
run: pip install ruff
28+
29+
- name: Ruff check
30+
run: ruff check ./api
31+
32+
- name: Lint hints
33+
if: failure()
34+
run: echo "Please run 'dev/reformat' to fix the fixable linting errors."
35+
1336
test:
1437
name: ESLint and SuperLinter
1538
runs-on: ubuntu-latest
39+
needs: python-style
1640

1741
steps:
1842
- name: Checkout code

api/app.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@
1919
import time
2020
import warnings
2121

22+
from flask import Flask, Response, request
23+
from flask_cors import CORS
24+
2225
from commands import register_commands
2326
from config import CloudEditionConfig, Config
24-
from events import event_handlers
25-
from extensions import (ext_celery, ext_code_based_extension, ext_database, ext_hosting_provider, ext_login, ext_mail,
26-
ext_migrate, ext_redis, ext_sentry, ext_storage)
27+
from extensions import (
28+
ext_celery,
29+
ext_code_based_extension,
30+
ext_database,
31+
ext_hosting_provider,
32+
ext_login,
33+
ext_mail,
34+
ext_migrate,
35+
ext_redis,
36+
ext_sentry,
37+
ext_storage,
38+
)
2739
from extensions.ext_database import db
2840
from extensions.ext_login import login_manager
29-
from flask import Flask, Response, request
30-
from flask_cors import CORS
3141
from libs.passport import PassportService
42+
3243
# DO NOT REMOVE BELOW
33-
from models import account, dataset, model, source, task, tool, tools, web
3444
from services.account_service import AccountService
3545

3646
# DO NOT REMOVE ABOVE

api/commands.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
import secrets
44

55
import click
6+
from flask import current_app
7+
from werkzeug.exceptions import NotFound
8+
69
from core.embedding.cached_embedding import CacheEmbedding
710
from core.model_manager import ModelManager
811
from core.model_runtime.entities.model_entities import ModelType
912
from extensions.ext_database import db
10-
from flask import current_app
1113
from libs.helper import email as email_validate
1214
from libs.password import hash_password, password_pattern, valid_password
1315
from libs.rsa import generate_key_pair
1416
from models.account import Tenant
1517
from models.dataset import Dataset
1618
from models.model import Account
1719
from models.provider import Provider, ProviderModel
18-
from werkzeug.exceptions import NotFound
1920

2021

2122
@click.command('reset-password', help='Reset the account password.')

api/constants/model_template.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import json
22

3-
from models.model import App, AppModelConfig
4-
53
model_templates = {
64
# completion default mode
75
'completion_default': {

api/controllers/console/admin.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import os
22
from functools import wraps
33

4+
from flask import request
5+
from flask_restful import Resource, reqparse
6+
from werkzeug.exceptions import NotFound, Unauthorized
7+
48
from constants.languages import supported_language
59
from controllers.console import api
610
from controllers.console.wraps import only_edition_cloud
711
from extensions.ext_database import db
8-
from flask import request
9-
from flask_restful import Resource, reqparse
1012
from models.model import App, InstalledApp, RecommendedApp
11-
from werkzeug.exceptions import NotFound, Unauthorized
1213

1314

1415
def admin_required(view):

api/controllers/console/apikey.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import flask_restful
2-
from extensions.ext_database import db
32
from flask_login import current_user
43
from flask_restful import Resource, fields, marshal_with
4+
from werkzeug.exceptions import Forbidden
5+
6+
from extensions.ext_database import db
57
from libs.helper import TimestampField
68
from libs.login import login_required
79
from models.dataset import Dataset
810
from models.model import ApiToken, App
9-
from werkzeug.exceptions import Forbidden
1011

1112
from . import api
1213
from .setup import setup_required

api/controllers/console/app/advanced_prompt_template.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from flask_restful import Resource, reqparse
2+
13
from controllers.console import api
24
from controllers.console.setup import setup_required
35
from controllers.console.wraps import account_initialization_required
4-
from flask_restful import Resource, reqparse
56
from libs.login import login_required
67
from services.advanced_prompt_template_service import AdvancedPromptTemplateService
78

api/controllers/console/app/annotation.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
from flask import request
2+
from flask_login import current_user
3+
from flask_restful import Resource, marshal, marshal_with, reqparse
4+
from werkzeug.exceptions import Forbidden
5+
16
from controllers.console import api
27
from controllers.console.app.error import NoFileUploadedError
38
from controllers.console.datasets.error import TooManyFilesError
49
from controllers.console.setup import setup_required
510
from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
611
from extensions.ext_redis import redis_client
7-
from fields.annotation_fields import (annotation_fields, annotation_hit_history_fields,
8-
annotation_hit_history_list_fields, annotation_list_fields)
9-
from flask import request
10-
from flask_login import current_user
11-
from flask_restful import Resource, marshal, marshal_with, reqparse
12+
from fields.annotation_fields import (
13+
annotation_fields,
14+
annotation_hit_history_fields,
15+
)
1216
from libs.login import login_required
1317
from services.annotation_service import AppAnnotationService
14-
from werkzeug.exceptions import Forbidden
1518

1619

1720
class AnnotationReplyActionApi(Resource):

api/controllers/console/app/app.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import logging
44
from datetime import datetime
55

6+
from flask_login import current_user
7+
from flask_restful import Resource, abort, inputs, marshal_with, reqparse
8+
from werkzeug.exceptions import Forbidden
9+
610
from constants.languages import demo_model_templates, languages
711
from constants.model_template import model_templates
812
from controllers.console import api
@@ -15,16 +19,15 @@
1519
from core.provider_manager import ProviderManager
1620
from events.app_event import app_was_created, app_was_deleted
1721
from extensions.ext_database import db
18-
from fields.app_fields import (app_detail_fields, app_detail_fields_with_site, app_pagination_fields,
19-
template_list_fields)
20-
from flask import current_app
21-
from flask_login import current_user
22-
from flask_restful import Resource, abort, inputs, marshal_with, reqparse
22+
from fields.app_fields import (
23+
app_detail_fields,
24+
app_detail_fields_with_site,
25+
app_pagination_fields,
26+
template_list_fields,
27+
)
2328
from libs.login import login_required
2429
from models.model import App, AppModelConfig, Site
25-
from models.tools import ApiToolProvider
2630
from services.app_model_config_service import AppModelConfigService
27-
from werkzeug.exceptions import Forbidden
2831

2932

3033
def _get_app(app_id, tenant_id):

api/controllers/console/app/audio.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
# -*- coding:utf-8 -*-
22
import logging
33

4+
from flask import request
5+
from flask_restful import Resource
6+
from werkzeug.exceptions import InternalServerError
7+
48
import services
59
from controllers.console import api
610
from controllers.console.app import _get_app
7-
from controllers.console.app.error import (AppUnavailableError, AudioTooLargeError, CompletionRequestError,
8-
NoAudioUploadedError, ProviderModelCurrentlyNotSupportError,
9-
ProviderNotInitializeError, ProviderNotSupportSpeechToTextError,
10-
ProviderQuotaExceededError, UnsupportedAudioTypeError)
11+
from controllers.console.app.error import (
12+
AppUnavailableError,
13+
AudioTooLargeError,
14+
CompletionRequestError,
15+
NoAudioUploadedError,
16+
ProviderModelCurrentlyNotSupportError,
17+
ProviderNotInitializeError,
18+
ProviderNotSupportSpeechToTextError,
19+
ProviderQuotaExceededError,
20+
UnsupportedAudioTypeError,
21+
)
1122
from controllers.console.setup import setup_required
1223
from controllers.console.wraps import account_initialization_required
1324
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
1425
from core.model_runtime.errors.invoke import InvokeError
15-
from flask import request
16-
from flask_restful import Resource
1726
from libs.login import login_required
1827
from services.audio_service import AudioService
19-
from services.errors.audio import (AudioTooLargeServiceError, NoAudioUploadedServiceError,
20-
ProviderNotSupportSpeechToTextServiceError, UnsupportedAudioTypeServiceError)
21-
from werkzeug.exceptions import InternalServerError
28+
from services.errors.audio import (
29+
AudioTooLargeServiceError,
30+
NoAudioUploadedServiceError,
31+
ProviderNotSupportSpeechToTextServiceError,
32+
UnsupportedAudioTypeServiceError,
33+
)
2234

2335

2436
class ChatMessageAudioApi(Resource):

api/controllers/console/app/completion.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@
44
from typing import Generator, Union
55

66
import flask_login
7+
from flask import Response, stream_with_context
8+
from flask_restful import Resource, reqparse
9+
from werkzeug.exceptions import InternalServerError, NotFound
10+
711
import services
812
from controllers.console import api
913
from controllers.console.app import _get_app
10-
from controllers.console.app.error import (AppUnavailableError, CompletionRequestError, ConversationCompletedError,
11-
ProviderModelCurrentlyNotSupportError, ProviderNotInitializeError,
12-
ProviderQuotaExceededError)
14+
from controllers.console.app.error import (
15+
AppUnavailableError,
16+
CompletionRequestError,
17+
ConversationCompletedError,
18+
ProviderModelCurrentlyNotSupportError,
19+
ProviderNotInitializeError,
20+
ProviderQuotaExceededError,
21+
)
1322
from controllers.console.setup import setup_required
1423
from controllers.console.wraps import account_initialization_required
1524
from core.application_queue_manager import ApplicationQueueManager
1625
from core.entities.application_entities import InvokeFrom
1726
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
1827
from core.model_runtime.errors.invoke import InvokeError
19-
from flask import Response, stream_with_context
20-
from flask_restful import Resource, reqparse
2128
from libs.helper import uuid_value
2229
from libs.login import login_required
2330
from services.completion_service import CompletionService
24-
from werkzeug.exceptions import InternalServerError, NotFound
2531

2632

2733
# define completion message api for user

api/controllers/console/app/conversation.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
from datetime import datetime
22

33
import pytz
4+
from flask_login import current_user
5+
from flask_restful import Resource, marshal_with, reqparse
6+
from flask_restful.inputs import int_range
7+
from sqlalchemy import func, or_
8+
from sqlalchemy.orm import joinedload
9+
from werkzeug.exceptions import NotFound
10+
411
from controllers.console import api
512
from controllers.console.app import _get_app
613
from controllers.console.setup import setup_required
714
from controllers.console.wraps import account_initialization_required
815
from extensions.ext_database import db
9-
from fields.conversation_fields import (conversation_detail_fields, conversation_message_detail_fields,
10-
conversation_pagination_fields, conversation_with_summary_pagination_fields)
11-
from flask_login import current_user
12-
from flask_restful import Resource, marshal_with, reqparse
13-
from flask_restful.inputs import int_range
16+
from fields.conversation_fields import (
17+
conversation_detail_fields,
18+
conversation_message_detail_fields,
19+
conversation_pagination_fields,
20+
conversation_with_summary_pagination_fields,
21+
)
1422
from libs.helper import datetime_string
1523
from libs.login import login_required
1624
from models.model import Conversation, Message, MessageAnnotation
17-
from sqlalchemy import func, or_
18-
from sqlalchemy.orm import joinedload
19-
from werkzeug.exceptions import NotFound
2025

2126

2227
class CompletionConversationApi(Resource):

api/controllers/console/app/generator.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
from flask_login import current_user
2+
from flask_restful import Resource, reqparse
3+
14
from controllers.console import api
2-
from controllers.console.app.error import (CompletionRequestError, ProviderModelCurrentlyNotSupportError,
3-
ProviderNotInitializeError, ProviderQuotaExceededError)
5+
from controllers.console.app.error import (
6+
CompletionRequestError,
7+
ProviderModelCurrentlyNotSupportError,
8+
ProviderNotInitializeError,
9+
ProviderQuotaExceededError,
10+
)
411
from controllers.console.setup import setup_required
512
from controllers.console.wraps import account_initialization_required
613
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
714
from core.generator.llm_generator import LLMGenerator
815
from core.model_runtime.errors.invoke import InvokeError
9-
from flask_login import current_user
10-
from flask_restful import Resource, reqparse
1116
from libs.login import login_required
1217

1318

api/controllers/console/app/message.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22
import logging
33
from typing import Generator, Union
44

5+
from flask import Response, stream_with_context
6+
from flask_login import current_user
7+
from flask_restful import Resource, fields, marshal_with, reqparse
8+
from flask_restful.inputs import int_range
9+
from werkzeug.exceptions import Forbidden, InternalServerError, NotFound
10+
511
from controllers.console import api
612
from controllers.console.app import _get_app
7-
from controllers.console.app.error import (AppMoreLikeThisDisabledError, CompletionRequestError,
8-
ProviderModelCurrentlyNotSupportError, ProviderNotInitializeError,
9-
ProviderQuotaExceededError)
13+
from controllers.console.app.error import (
14+
AppMoreLikeThisDisabledError,
15+
CompletionRequestError,
16+
ProviderModelCurrentlyNotSupportError,
17+
ProviderNotInitializeError,
18+
ProviderQuotaExceededError,
19+
)
1020
from controllers.console.setup import setup_required
1121
from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
1222
from core.entities.application_entities import InvokeFrom
1323
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
1424
from core.model_runtime.errors.invoke import InvokeError
1525
from extensions.ext_database import db
1626
from fields.conversation_fields import annotation_fields, message_detail_fields
17-
from flask import Response, stream_with_context
18-
from flask_login import current_user
19-
from flask_restful import Resource, fields, marshal_with, reqparse
20-
from flask_restful.inputs import int_range
2127
from libs.helper import uuid_value
2228
from libs.infinite_scroll_pagination import InfiniteScrollPagination
2329
from libs.login import login_required
@@ -28,7 +34,6 @@
2834
from services.errors.conversation import ConversationNotExistsError
2935
from services.errors.message import MessageNotExistsError
3036
from services.message_service import MessageService
31-
from werkzeug.exceptions import Forbidden, InternalServerError, NotFound
3237

3338

3439
class ChatMessageListApi(Resource):

0 commit comments

Comments
 (0)