ruff
This commit is contained in:
@@ -6,13 +6,14 @@ from typing import Dict, Optional
|
|||||||
|
|
||||||
感谢D指导
|
感谢D指导
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TimerTypeError(TypeError):
|
class TimerTypeError(TypeError):
|
||||||
"""自定义类型错误异常"""
|
"""自定义类型错误异常"""
|
||||||
|
|
||||||
def __init__(self, param_name, expected_type, actual_type):
|
def __init__(self, param_name, expected_type, actual_type):
|
||||||
super().__init__(
|
super().__init__(f"Invalid type for '{param_name}'. Expected {expected_type}, got {actual_type.__name__}")
|
||||||
f"Invalid type for '{param_name}'. "
|
|
||||||
f"Expected {expected_type}, got {actual_type.__name__}"
|
|
||||||
)
|
|
||||||
|
|
||||||
class Timer:
|
class Timer:
|
||||||
def __init__(self, name: Optional[str] = None, storage: Optional[Dict[str, float]] = None):
|
def __init__(self, name: Optional[str] = None, storage: Optional[Dict[str, float]] = None):
|
||||||
@@ -24,19 +25,14 @@ class Timer:
|
|||||||
"""类型验证核心方法"""
|
"""类型验证核心方法"""
|
||||||
# 验证 name 类型
|
# 验证 name 类型
|
||||||
if name is not None and not isinstance(name, str):
|
if name is not None and not isinstance(name, str):
|
||||||
raise TimerTypeError(
|
raise TimerTypeError(param_name="name", expected_type="Optional[str]", actual_type=type(name))
|
||||||
param_name="name",
|
|
||||||
expected_type="Optional[str]",
|
|
||||||
actual_type=type(name)
|
|
||||||
)
|
|
||||||
|
|
||||||
# 验证 storage 类型
|
# 验证 storage 类型
|
||||||
if storage is not None and not isinstance(storage, dict):
|
if storage is not None and not isinstance(storage, dict):
|
||||||
raise TimerTypeError(
|
raise TimerTypeError(
|
||||||
param_name="storage",
|
param_name="storage", expected_type="Optional[Dict[str, float]]", actual_type=type(storage)
|
||||||
expected_type="Optional[Dict[str, float]]",
|
|
||||||
actual_type=type(storage)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.start = perf_counter()
|
self.start = perf_counter()
|
||||||
return self
|
return self
|
||||||
|
|||||||
Reference in New Issue
Block a user