fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
This commit is contained in:
OpenClaw Bot
2026-02-27 18:09:24 +08:00
parent 646b64daf7
commit 17bda3dbce
38 changed files with 1993 additions and 1972 deletions

View File

@@ -5,10 +5,9 @@ Phase 5: Neo4j 图数据库集成
支持数据同步、复杂图查询和图算法分析
"""
import os
import json
import logging
from typing import List, Dict, Optional
import os
from dataclasses import dataclass
logger = logging.getLogger(__name__)
@@ -20,7 +19,7 @@ NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD", "password")
# 延迟导入,避免未安装时出错
try:
from neo4j import GraphDatabase, Driver
from neo4j import Driver, GraphDatabase
NEO4J_AVAILABLE = True
except ImportError:
@@ -37,8 +36,8 @@ class GraphEntity:
name: str
type: str
definition: str = ""
aliases: List[str] = None
properties: Dict = None
aliases: list[str] = None
properties: dict = None
def __post_init__(self):
if self.aliases is None:
@@ -56,7 +55,7 @@ class GraphRelation:
target_id: str
relation_type: str
evidence: str = ""
properties: Dict = None
properties: dict = None
def __post_init__(self):
if self.properties is None:
@@ -67,8 +66,8 @@ class GraphRelation:
class PathResult:
"""路径查询结果"""
nodes: List[Dict]
relationships: List[Dict]
nodes: list[dict]
relationships: list[dict]
length: int
total_weight: float = 0.0
@@ -78,7 +77,7 @@ class CommunityResult:
"""社区发现结果"""
community_id: int
nodes: List[Dict]
nodes: list[dict]
size: int
density: float = 0.0
@@ -100,7 +99,7 @@ class Neo4jManager:
self.uri = uri or NEO4J_URI
self.user = user or NEO4J_USER
self.password = password or NEO4J_PASSWORD
self._driver: Optional["Driver"] = None
self._driver: Driver | None = None
if not NEO4J_AVAILABLE:
logger.error("Neo4j driver not available. Please install: pip install neo4j")
@@ -226,7 +225,7 @@ class Neo4jManager:
properties=json.dumps(entity.properties),
)
def sync_entities_batch(self, entities: List[GraphEntity]):
def sync_entities_batch(self, entities: list[GraphEntity]):
"""批量同步实体到 Neo4j"""
if not self._driver or not entities:
return
@@ -287,7 +286,7 @@ class Neo4jManager:
properties=json.dumps(relation.properties),
)
def sync_relations_batch(self, relations: List[GraphRelation]):
def sync_relations_batch(self, relations: list[GraphRelation]):
"""批量同步关系到 Neo4j"""
if not self._driver or not relations:
return
@@ -350,7 +349,7 @@ class Neo4jManager:
# ==================== 复杂图查询 ====================
def find_shortest_path(self, source_id: str, target_id: str, max_depth: int = 10) -> Optional[PathResult]:
def find_shortest_path(self, source_id: str, target_id: str, max_depth: int = 10) -> PathResult | None:
"""
查找两个实体之间的最短路径
@@ -399,7 +398,7 @@ class Neo4jManager:
return PathResult(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) -> List[PathResult]:
def find_all_paths(self, source_id: str, target_id: str, max_depth: int = 5, limit: int = 10) -> list[PathResult]:
"""
查找两个实体之间的所有路径
@@ -449,7 +448,7 @@ class Neo4jManager:
return paths
def find_neighbors(self, entity_id: str, relation_type: str = None, limit: int = 50) -> List[Dict]:
def find_neighbors(self, entity_id: str, relation_type: str = None, limit: int = 50) -> list[dict]:
"""
查找实体的邻居节点
@@ -502,7 +501,7 @@ class Neo4jManager:
return neighbors
def find_common_neighbors(self, entity_id1: str, entity_id2: str) -> List[Dict]:
def find_common_neighbors(self, entity_id1: str, entity_id2: str) -> list[dict]:
"""
查找两个实体的共同邻居(潜在关联)
@@ -533,7 +532,7 @@ class Neo4jManager:
# ==================== 图算法分析 ====================
def calculate_pagerank(self, project_id: str, top_n: int = 20) -> List[CentralityResult]:
def calculate_pagerank(self, project_id: str, top_n: int = 20) -> list[CentralityResult]:
"""
计算 PageRank 中心性
@@ -619,7 +618,7 @@ class Neo4jManager:
return rankings
def calculate_betweenness(self, project_id: str, top_n: int = 20) -> List[CentralityResult]:
def calculate_betweenness(self, project_id: str, top_n: int = 20) -> list[CentralityResult]:
"""
计算 Betweenness 中心性(桥梁作用)
@@ -663,7 +662,7 @@ class Neo4jManager:
return rankings
def detect_communities(self, project_id: str) -> List[CommunityResult]:
def detect_communities(self, project_id: str) -> list[CommunityResult]:
"""
社区发现(使用 Louvain 算法)
@@ -733,7 +732,7 @@ class Neo4jManager:
results.sort(key=lambda x: x.size, reverse=True)
return results
def find_central_entities(self, project_id: str, metric: str = "degree") -> List[CentralityResult]:
def find_central_entities(self, project_id: str, metric: str = "degree") -> list[CentralityResult]:
"""
查找中心实体
@@ -791,7 +790,7 @@ class Neo4jManager:
# ==================== 图统计 ====================
def get_graph_stats(self, project_id: str) -> Dict:
def get_graph_stats(self, project_id: str) -> dict:
"""
获取项目的图统计信息
@@ -870,7 +869,7 @@ class Neo4jManager:
"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:
def get_subgraph(self, entity_ids: list[str], depth: int = 1) -> dict:
"""
获取指定实体的子图
@@ -959,7 +958,7 @@ def close_neo4j_manager():
# 便捷函数
def sync_project_to_neo4j(project_id: str, project_name: str, entities: List[Dict], relations: List[Dict]):
def sync_project_to_neo4j(project_id: str, project_name: str, entities: list[dict], relations: list[dict]):
"""
同步整个项目到 Neo4j