Skip to content
This repository was archived by the owner on Nov 26, 2026. It is now read-only.

fix(utils): return None from flatten_errors when there is no error - #26

Open
PttCodingMan wants to merge 1 commit into
mainfrom
fix/flatten-errors
Open

PttCodingMan wants to merge 1 commit into
mainfrom
fix/flatten-errors

Conversation

@PttCodingMan

Copy link
Copy Markdown
Member

這個 PR 的目的 / Purpose of this PR

utils.util.flatten_errors() 用 filter(...) 過濾出有錯誤的項目後,直接用 if not the_errors 判斷是否沒有錯誤。在 Python 3 中 filter 回傳的是 iterator,永遠為 truthy,所以 return None, the_list 這個分支永遠不會執行;沒有錯誤時實際回傳的是空字串 '' 而不是 None。目前呼叫端剛好用 if err: 判斷所以沒出事,但函式的回傳值與預期(None 表示沒有錯誤)不符。本 PR 將 filter 結果轉成 list。

flatten_errors() filters the failed items with filter(...) and then checks if not the_errors. In Python 3 filter returns an iterator, which is always truthy, so the return None, the_list branch was unreachable and the function returned '' instead of None when there were no errors. Current callers use if err: so it went unnoticed, but the return value did not match the intended contract. This PR wraps the result in list(...).

驗證 / Verification

環境:Python 3.10 venv,依 setup.py 安裝依賴(另外 pin mongoengine==0.20.0、pymongo<4),測試使用 mongomock。nose 1.3.7 在 Python 3.10 無法執行,所以改用 pytest<8 並以暫時的 conftest.py(不 commit)呼叫 tests.setup()。

Environment: Python 3.10 venv with the setup.py dependencies (plus mongoengine==0.20.0, pymongo<4); tests use mongomock. nose 1.3.7 does not run on Python 3.10, so I used pytest<8 with a temporary, uncommitted conftest.py that calls tests.setup().

  • 新增 test_flatten_errors_no_error / test_flatten_errors_with_error;修正前前者失敗:AssertionError: '' is not None

  • 修正後 python -m pytest -q tests:51 passed

  • Added test_flatten_errors_no_error / test_flatten_errors_with_error; before the fix the former fails with AssertionError: '' is not None.

  • After the fix, python -m pytest -q tests: 51 passed.

相關的 issue / Related Issues

無 / None

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant