fix(data): 校验早于副作用,补空行;经验写入 pitfalls
save_table 的两处收尾(用户授权代改): - 后缀校验原先藏在 if/else 分支的 else 里,排在 path.parent.mkdir(...) 之后, 传 sub/x.txt 会先把 sub\ 建出来再抛 ValueError。改成守卫子句,集中在 mkdir 之前, 校验段结束后才产生副作用;此时 else 分支自然消失,用回 if/else 两路分派 - __all__ 与下一个 def 之间补回第二个空行(PEP 8 / E302) 测试: - 新增 test_save_table_validates_before_touching_disk, 断言参数非法时 tmp_path 下一个条目都不留(ValueError 与 TypeError 两条路径) - 40 passed / 44 skipped,两套环境一致;ruff All checks passed 文档: - docs/dev/personal-pitfalls.md 追加条目 5(参数校验排在副作用之后) 与条目 6(把 lint 的"没报错"当成"没问题"),索引表同步加行 - STATUS:io.py 收尾,L0 第 3 项完成;测试计数 40 - CHANGELOG:Fixed 段补两条
This commit is contained in:
1 parent
19f4a15107
commit
6758e81c9d
5 files changed
+61
-9
No files matched your search
@@ -105,6 +105,18 @@ def test_save_table_rejects_non_dataframe(tmp_path):
|
||||
save_table([1, 2, 3], tmp_path / "bad.csv")
|
||||
|
||||
|
||||
def test_save_table_validates_before_touching_disk(tmp_path):
|
||||
"""校验必须早于副作用:参数不合法时,一个目录、一个文件都不能留下。"""
|
||||
from mcm_toolkit.data import save_table
|
||||
|
||||
with pytest.raises(ValueError, match=r"\.txt"):
|
||||
save_table(pd.DataFrame({"a": [1]}), tmp_path / "d1" / "x.txt")
|
||||
with pytest.raises(TypeError):
|
||||
save_table([1, 2, 3], tmp_path / "d2" / "x.csv")
|
||||
|
||||
assert sorted(p.name for p in tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
# ------------------------------------------------- 以下为未实现算法(L0/L1 待写)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user