Skip to content

Commit fcbb6fc

Browse files
committed
style: 确保flake8与Black的配置保持一致,优化代码质量和格式,符合pre-commit配置要求
1 parent 25cfbab commit fcbb6fc

34 files changed

Lines changed: 121 additions & 268 deletions

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
extend-ignore = E203

examples/advanced_usage.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
此文件包含pytest-dynamic-params插件的高级使用示例
55
"""
66

7-
import pytest
87
import time
98

9+
import pytest
10+
1011
from dynamic_params import param_generator, with_dynamic_params
1112

1213

@@ -21,15 +22,9 @@ def get_raw_data(data_source, size):
2122
def process_data(raw_data, algorithm):
2223
"""处理数据生成器,依赖于raw_data"""
2324
if algorithm == "algo1":
24-
return [
25-
{"id": item["id"], "processed": item["id"] * 2}
26-
for item in raw_data
27-
]
25+
return [{"id": item["id"], "processed": item["id"] * 2} for item in raw_data]
2826
else:
29-
return [
30-
{"id": item["id"], "processed": item["id"] * 3}
31-
for item in raw_data
32-
]
27+
return [{"id": item["id"], "processed": item["id"] * 3} for item in raw_data]
3328

3429

3530
@param_generator
@@ -40,9 +35,7 @@ def validate_results(processed_data, threshold):
4035

4136

4237
@with_dynamic_params(
43-
raw_data=get_raw_data,
44-
processed_data=process_data,
45-
is_valid=validate_results
38+
raw_data=get_raw_data, processed_data=process_data, is_valid=validate_results
4639
)
4740
@pytest.mark.parametrize("data_source", ["api", "database"])
4841
@pytest.mark.parametrize("size", [5, 10])
@@ -59,8 +52,7 @@ def test_dynamic_params_nesting(
5952
# 验证处理后的数据
6053
assert len(processed_data) == size
6154
assert all(
62-
item["id"] == raw_item["id"]
63-
for item, raw_item in zip(processed_data, raw_data)
55+
item["id"] == raw_item["id"] for item, raw_item in zip(processed_data, raw_data)
6456
)
6557

6658
# 验证结果

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ target-version = ['py38']
5252

5353
[tool.isort]
5454
profile = "black"
55+
56+
[tool.flake8]
57+
max-line-length = 88
58+
extend-ignore = ["E203"]

src/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
"""
77

88
from .dynamic_params import (
9-
ParamGenerator,
10-
LazyResult,
11-
GeneratorRegistry,
9+
DynamicParamConfig,
1210
DynamicParamError,
13-
MissingParameterError,
11+
GeneratorRegistry,
1412
InvalidGeneratorError,
13+
LazyResult,
14+
MissingParameterError,
15+
ParamGenerator,
1516
param_generator,
16-
with_dynamic_params,
1717
pytest_configure,
1818
pytest_generate_tests,
19-
DynamicParamConfig,
19+
with_dynamic_params,
2020
)
2121

2222
__version__ = "0.1.0"

src/dynamic_params/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
from .core.generator import ParamGenerator
33
from .core.registry import GeneratorRegistry
44
from .decorators import param_generator, with_dynamic_params
5-
from .errors import (
6-
DynamicParamError,
7-
InvalidGeneratorError,
8-
MissingParameterError
9-
)
5+
from .errors import DynamicParamError, InvalidGeneratorError, MissingParameterError
106
from .lazy import LazyResult
117
from .plugin import pytest_configure, pytest_generate_tests
128

src/dynamic_params/config.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import os
21
import configparser
3-
from typing import Dict, Any, Optional
2+
import os
3+
from typing import Any, Dict, Optional
44

55
from .errors import ConfigurationError
66

@@ -19,10 +19,7 @@ class DynamicParamConfig:
1919
"dir": ".pytest_cache/dynamic_params",
2020
},
2121
"validation": {"level": "strict", "log_level": "INFO"},
22-
"performance": {
23-
"lazy_loading": "true",
24-
"incremental_generation": "true"
25-
},
22+
"performance": {"lazy_loading": "true", "incremental_generation": "true"},
2623
"debug": {"enabled": "false", "profile": "false"},
2724
}
2825

@@ -73,8 +70,7 @@ def _update_from_env(self, config: configparser.ConfigParser):
7370
"PYTEST_DYNAMIC_PARAM_VALIDATION": "validation.level",
7471
"PYTEST_DYNAMIC_PARAM_LOG_LEVEL": "validation.log_level",
7572
"PYTEST_DYNAMIC_PARAM_LAZY_LOADING": "performance.lazy_loading",
76-
"PYTEST_DYNAMIC_PARAM_INCREMENTAL":
77-
"performance.incremental_generation",
73+
"PYTEST_DYNAMIC_PARAM_INCREMENTAL": "performance.incremental_generation",
7874
"PYTEST_DYNAMIC_PARAM_DEBUG": "debug.enabled",
7975
"PYTEST_DYNAMIC_PARAM_PROFILE": "debug.profile",
8076
"PYTEST_DYNAMIC_PARAM_CACHE_DIR": "cache.dir",
@@ -88,13 +84,9 @@ def _update_from_env(self, config: configparser.ConfigParser):
8884
config[section] = {}
8985
config[section][option] = os.environ[env_var]
9086
except Exception as e:
91-
print(
92-
f"Warning: Failed to update config from {env_var}: {e}"
93-
)
87+
print(f"Warning: Failed to update config from {env_var}: {e}")
9488

95-
def _normalize_config(
96-
self, config: configparser.ConfigParser
97-
) -> Dict[str, Any]:
89+
def _normalize_config(self, config: configparser.ConfigParser) -> Dict[str, Any]:
9890
"""标准化配置值"""
9991
normalized = {}
10092

@@ -104,8 +96,7 @@ def _normalize_config(
10496
try:
10597
# 转换布尔值
10698
if value.lower() in ("true", "false"):
107-
normalized[section][key] = \
108-
config[section].getboolean(key)
99+
normalized[section][key] = config[section].getboolean(key)
109100
# 转换整数
110101
elif value.isdigit():
111102
normalized[section][key] = config[section].getint(key)
@@ -120,9 +111,7 @@ def _normalize_config(
120111

121112
return normalized
122113

123-
def get(
124-
self, section: str, option: str, default: Optional[Any] = ...
125-
) -> Any:
114+
def get(self, section: str, option: str, default: Optional[Any] = ...) -> Any:
126115
"""获取配置值"""
127116
try:
128117
if section not in self._config:
@@ -163,9 +152,7 @@ def validate(self) -> bool:
163152
valid_log_levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
164153
if self.get("validation", "log_level") not in valid_log_levels:
165154
raise ConfigurationError(
166-
"validation.log_level",
167-
self.get("validation", "log_level"),
168-
str
155+
"validation.log_level", self.get("validation", "log_level"), str
169156
)
170157

171158
return True

src/dynamic_params/core/registry.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""参数生成器注册表模块"""
22

3-
from typing import List, Optional, Callable
3+
from typing import Callable, List, Optional
44

5-
from .generator import ParamGenerator
65
from ..errors import InvalidGeneratorError
6+
from .generator import ParamGenerator
77

88

99
class GeneratorRegistry:
@@ -58,9 +58,7 @@ def is_generator_registered(self, generator_func: Callable) -> bool:
5858
return True
5959
return False
6060

61-
def register(
62-
self, generator_func: Callable, param_name: str
63-
) -> ParamGenerator:
61+
def register(self, generator_func: Callable, param_name: str) -> ParamGenerator:
6462
"""注册生成器函数
6563
6664
参数:

src/dynamic_params/decorators.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class _ParamGeneratorDecorator:
99
参数生成器装饰器类
1010
"""
1111

12-
def __init__(
13-
self, scope: str = "function", cache: bool = True, lazy: bool = True
14-
):
12+
def __init__(self, scope: str = "function", cache: bool = True, lazy: bool = True):
1513
self.scope = scope
1614
self.cache_enabled = cache
1715
self.lazy_support = lazy
@@ -39,10 +37,7 @@ def wrapper(*args, **kwargs):
3937

4038

4139
def param_generator(
42-
func_or_scope=None,
43-
scope: str = "function",
44-
cache: bool = True,
45-
lazy: bool = True
40+
func_or_scope=None, scope: str = "function", cache: bool = True, lazy: bool = True
4641
):
4742
"""
4843
参数生成器装饰器,支持两种用法:
@@ -52,17 +47,13 @@ def param_generator(
5247
# 如果第一个参数是可调用的,说明是 @param_generator 用法
5348
if callable(func_or_scope):
5449
# 直接装饰函数
55-
decorator = _ParamGeneratorDecorator(
56-
scope=scope, cache=cache, lazy=lazy
57-
)
50+
decorator = _ParamGeneratorDecorator(scope=scope, cache=cache, lazy=lazy)
5851
return decorator(func_or_scope)
5952
else:
6053
# 是 @param_generator() 或 @param_generator(scope="...") 用法
6154
# 返回配置好的装饰器实例
6255
actual_scope = func_or_scope if func_or_scope is not None else scope
63-
return _ParamGeneratorDecorator(
64-
scope=actual_scope, cache=cache, lazy=lazy
65-
)
56+
return _ParamGeneratorDecorator(scope=actual_scope, cache=cache, lazy=lazy)
6657

6758

6859
def with_dynamic_params(**param_mapping: Callable):
@@ -76,12 +67,9 @@ def decorator(test_func: Callable) -> Callable:
7667

7768
# Verify generator is properly decorated
7869
if not hasattr(generator_func, "_is_param_generator"):
79-
func_name = getattr(
80-
generator_func, "__name__", str(generator_func)
81-
)
70+
func_name = getattr(generator_func, "__name__", str(generator_func))
8271
raise ValueError(
83-
f"Function {func_name} must be decorated "
84-
f"with @param_generator"
72+
f"Function {func_name} must be decorated " f"with @param_generator"
8573
)
8674

8775
# Initialize dynamic param attributes on test function
@@ -90,9 +78,7 @@ def decorator(test_func: Callable) -> Callable:
9078

9179
# Mark function as dynamic param test
9280
test_func.pytestmark = getattr(test_func, "pytestmark", [])
93-
test_func.pytestmark.append(
94-
pytest.mark.dynamic_param
95-
)
81+
test_func.pytestmark.append(pytest.mark.dynamic_param)
9682

9783
@functools.wraps(test_func)
9884
def wrapper(*args, **kwargs):

src/dynamic_params/dependency.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
from .errors import CircularDependencyError
77

88

9-
def resolve_dependency_order(
10-
generators: List[ParamGenerator]
11-
) -> List[ParamGenerator]:
9+
def resolve_dependency_order(generators: List[ParamGenerator]) -> List[ParamGenerator]:
1210
"""解析生成器依赖顺序,使用拓扑排序并检测循环依赖"""
1311
# 构建依赖图
1412
graph: Dict[str, Set[str]] = {}

src/dynamic_params/errors.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""错误处理模块"""
22

3-
from typing import List, Dict, Any
3+
from typing import Any, Dict, List
44

55

66
class DynamicParamError(Exception):
@@ -55,10 +55,7 @@ class ExecutionError(DynamicParamError):
5555
"""生成器执行异常"""
5656

5757
def __init__(
58-
self,
59-
generator_name: str,
60-
exception: Exception,
61-
context: Dict[str, Any]
58+
self, generator_name: str, exception: Exception, context: Dict[str, Any]
6259
):
6360
self.generator_name = generator_name
6461
self.exception = exception
@@ -75,9 +72,7 @@ def __init__(
7572
class ConfigurationError(DynamicParamError):
7673
"""配置错误异常"""
7774

78-
def __init__(
79-
self, config_key: str, config_value: Any, expected_type: type
80-
):
75+
def __init__(self, config_key: str, config_value: Any, expected_type: type):
8176
self.config_key = config_key
8277
self.config_value = config_value
8378
self.expected_type = expected_type

0 commit comments

Comments
 (0)