isort是否有一种方法可以自动检测具有多个独立软件包的代码库中的第一方和第三方模块?

2024-05-18 15:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

project代码库中,名为plugins的文件夹中有多个独立的包,每个包都位于自己的文件夹中,该文件夹中有一个setup.py文件,项目本身是一个具有自己setup.py文件的python包

我有两个文件夹project/projectplugins/myplugin_one/project_plugins/myplugin_one,需要在适当的时候考虑first_partythird_party。例如,在plugins/myplugin_one/project_plugins/myplugin_one内,有一个文件config.py包含以下代码:

from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional

# First Party
from project.core.config_store import ConfigStore

导入from project.core.config_store import ConfigStore被视为first_party导入,但应视为third_party导入,因为文件位于plugins/myplugin_one/project_plugins/myplugin_one中,myplugin_one是一个独立的包(first_party),而project在此上下文中是third_party

类似地,对于驻留在project/project内的文件中的任何导入,应将project/project视为first_party,从plugins/myplugin_one/project_plugins/myplugin_one导入应视为third_party

项目的sections顺序应为:

sections=
    FUTURE
    STDLIB
    THIRDPARTY
    FIRSTPARTY
    LOCALFOLDER

这是从isort 4到isort 5.4.2的升级,因此默认部分不再是first_party,而是third_party,默认情况下不会跳过__init__.py

这是我的isort.cfg文件:

[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
float_to_top = true
line_length=88
ensure_newline_before_comments=True
sections=
    FUTURE
    STDLIB
    THIRDPARTY
    FIRSTPARTY
    LOCALFOLDER
import_heading_stdlib=Standard Library
import_heading_firstparty=First Party
import_heading_thirdparty=Third Party
import_heading_localfolder=Local Folder
known_first_party=project,project_plugins
known_local_folder=build_helpers,tests
src_paths=
skip=
    __init__.py

Tags: 文件frompyimportproject文件夹configparty

热门问题