fix: auto-fix code issues (cron)

- 修复隐式 Optional 类型注解 (RUF013)
- 修复不必要的赋值后返回 (RET504)
- 优化列表推导式 (PERF401)
- 修复未使用的参数 (ARG002)
- 清理重复导入
- 优化异常处理
This commit is contained in:
AutoFix Bot
2026-03-03 21:11:47 +08:00
parent d17a58ceae
commit 259f2c90d0
36 changed files with 1651 additions and 863 deletions

View File

@@ -37,7 +37,7 @@ class GraphEntity:
type: str
definition: str = ""
aliases: list[str] = None
properties: dict = None
properties: dict | None = None
def __post_init__(self) -> None:
if self.aliases is None:
@@ -55,7 +55,7 @@ class GraphRelation:
target_id: str
relation_type: str
evidence: str = ""
properties: dict = None
properties: dict | None = None
def __post_init__(self) -> None:
if self.properties is None:
@@ -95,7 +95,7 @@ class CentralityResult:
class Neo4jManager:
"""Neo4j 图数据库管理器"""
def __init__(self, uri: str = None, user: str = None, password: str = None) -> None:
def __init__(self, uri: str | None = None, user: str = None, password: str = None) -> None:
self.uri = uri or NEO4J_URI
self.user = user or NEO4J_USER
self.password = password or NEO4J_PASSWORD
@@ -179,7 +179,10 @@ class Neo4jManager:
# ==================== 数据同步 ====================
def sync_project(
self, project_id: str, project_name: str, project_description: str = "",
self,
project_id: str,
project_name: str,
project_description: str = "",
) -> None:
"""同步项目节点到 Neo4j"""
if not self._driver:
@@ -352,7 +355,10 @@ class Neo4jManager:
# ==================== 复杂图查询 ====================
def find_shortest_path(
self, source_id: str, target_id: str, max_depth: int = 10,
self,
source_id: str,
target_id: str,
max_depth: int = 10,
) -> PathResult | None:
"""
查找两个实体之间的最短路径
@@ -404,11 +410,17 @@ class Neo4jManager:
]
return PathResult(
nodes=nodes, relationships=relationships, length=len(path.relationships),
nodes=nodes,
relationships=relationships,
length=len(path.relationships),
)
def find_all_paths(
self, source_id: str, target_id: str, max_depth: int = 5, limit: int = 10,
self,
source_id: str,
target_id: str,
max_depth: int = 5,
limit: int = 10,
) -> list[PathResult]:
"""
查找两个实体之间的所有路径
@@ -460,14 +472,19 @@ class Neo4jManager:
paths.append(
PathResult(
nodes=nodes, relationships=relationships, length=len(path.relationships),
nodes=nodes,
relationships=relationships,
length=len(path.relationships),
),
)
return paths
def find_neighbors(
self, entity_id: str, relation_type: str = None, limit: int = 50,
self,
entity_id: str,
relation_type: str | None = None,
limit: int = 50,
) -> list[dict]:
"""
查找实体的邻居节点
@@ -752,7 +769,10 @@ class Neo4jManager:
results.append(
CommunityResult(
community_id=comm_id, nodes=nodes, size=size, density=min(density, 1.0),
community_id=comm_id,
nodes=nodes,
size=size,
density=min(density, 1.0),
),
)
@@ -761,7 +781,9 @@ class Neo4jManager:
return results
def find_central_entities(
self, project_id: str, metric: str = "degree",
self,
project_id: str,
metric: str = "degree",
) -> list[CentralityResult]:
"""
查找中心实体
@@ -896,9 +918,11 @@ class Neo4jManager:
"type_distribution": types,
"average_degree": round(avg_degree, 2) if avg_degree else 0,
"relation_type_distribution": relation_types,
"density": round(relation_count / (entity_count * (entity_count - 1)), 4)
if entity_count > 1
else 0,
"density": (
round(relation_count / (entity_count * (entity_count - 1)), 4)
if entity_count > 1
else 0
),
}
def get_subgraph(self, entity_ids: list[str], depth: int = 1) -> dict:
@@ -993,7 +1017,10 @@ def close_neo4j_manager() -> None:
def sync_project_to_neo4j(
project_id: str, project_name: str, entities: list[dict], relations: list[dict],
project_id: str,
project_name: str,
entities: list[dict],
relations: list[dict],
) -> None:
"""
同步整个项目到 Neo4j