修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -15,7 +15,8 @@ from __future__ import annotations
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import List, Optional
|
||||
from typing import List
|
||||
from lib.core.VLogger import get_logger as _vlog
|
||||
|
||||
|
||||
class DiagnosticLevel(Enum):
|
||||
@@ -30,29 +31,29 @@ class Diagnostic:
|
||||
lineno: int
|
||||
message: str
|
||||
|
||||
def __str__(self):
|
||||
short_file = os.path.basename(self.file) if self.file else "<unknown>"
|
||||
def __str__(self) -> str:
|
||||
short_file: str = os.path.basename(self.file) if self.file else "<unknown>"
|
||||
return f"[{self.level.value}] {short_file}:{self.lineno} — {self.message}"
|
||||
|
||||
|
||||
class DiagnosticCollector:
|
||||
"""诊断信息收集器"""
|
||||
|
||||
def __init__(self):
|
||||
self._diagnostics: List[Diagnostic] = []
|
||||
def __init__(self) -> None:
|
||||
self._diagnostics: list[Diagnostic] = []
|
||||
|
||||
def warn(self, file: str, lineno: int, message: str):
|
||||
def warn(self, file: str, lineno: int, message: str) -> None:
|
||||
self._diagnostics.append(Diagnostic(DiagnosticLevel.WARNING, file, lineno, message))
|
||||
|
||||
def error(self, file: str, lineno: int, message: str):
|
||||
def error(self, file: str, lineno: int, message: str) -> None:
|
||||
self._diagnostics.append(Diagnostic(DiagnosticLevel.ERROR, file, lineno, message))
|
||||
|
||||
@property
|
||||
def warnings(self) -> List[Diagnostic]:
|
||||
def warnings(self) -> list[Diagnostic]:
|
||||
return [d for d in self._diagnostics if d.level == DiagnosticLevel.WARNING]
|
||||
|
||||
@property
|
||||
def errors(self) -> List[Diagnostic]:
|
||||
def errors(self) -> list[Diagnostic]:
|
||||
return [d for d in self._diagnostics if d.level == DiagnosticLevel.ERROR]
|
||||
|
||||
@property
|
||||
@@ -63,17 +64,17 @@ class DiagnosticCollector:
|
||||
def has_warnings(self) -> bool:
|
||||
return any(d.level == DiagnosticLevel.WARNING for d in self._diagnostics)
|
||||
|
||||
def report(self, min_level: DiagnosticLevel = DiagnosticLevel.WARNING):
|
||||
def report(self, min_level: DiagnosticLevel = DiagnosticLevel.WARNING) -> None:
|
||||
"""打印所有诊断信息"""
|
||||
for d in self._diagnostics:
|
||||
if d.level.value >= min_level.value:
|
||||
print(d)
|
||||
_vlog().debug(str(d))
|
||||
|
||||
def clear(self):
|
||||
def clear(self) -> None:
|
||||
self._diagnostics.clear()
|
||||
|
||||
def __len__(self):
|
||||
def __len__(self) -> int:
|
||||
return len(self._diagnostics)
|
||||
|
||||
def __bool__(self):
|
||||
def __bool__(self) -> bool:
|
||||
return len(self._diagnostics) > 0
|
||||
|
||||
Reference in New Issue
Block a user