fix: from_json() preserves falsy data instead of collapsing to None - #246
fix: from_json() preserves falsy data instead of collapsing to None#246patchwright wants to merge 2 commits into
Conversation
Distilled from caesar0301/treelib#246 (from_json collapsed falsy data like 0/""/[] to None via node_info.get("data") or None). dict.get() already returns None for a missing key, so a trailing or None is redundant and collapses legitimate falsy values. DEFAULT tier; the legitimate d.get(k) or fallback idiom is intentionally not flagged. Precision: 3 findings across the 6-package corpus (all django), comparable to WL005 -- not a FP explosion; DEFAULT tier validated. Dogfood: catches both or-None sites in treelib/tree.py @ master HEAD; 0 findings on wildlint's own source. Tracked in the corpus gate (RULES + baseline). Bumps 0.8.2 -> 0.8.3.
|
Hey patchwright. One of the implicit decisions I made when working on The only thing I'm wondering is if you can add a test for the other falsy values, as it only tests 0. I think it'd be prudent to consider all of these edge cases explicitly. |
Thanks for finding and fixing this @patchwright! I agree with @cmoliverio here. Can you add unit test for some other falsy values and I'll get this merged |
|
I'm going to go ahead and cherry pick this commit and add the other tests. I want to confirm that the other falsy cases round-trip correctly myself. |
|
Thanks both. I pushed the extra cases.
One detail worth flagging: the falsy test asserts on On the round-trip question, the serialisation side was already fine. Full suite is 212 passed, and |
|
Can this or #248 get merged? They are good tests. |
Problem
Tree.from_json()collapses every falsy-but-valid nodedatavalue (0,False,"",[],{}) toNone, so a round-trip throughto_json/from_jsonsilently drops it.Confirmed on
masterHEADfce5fa3.Root cause
from_jsonreads each node's data asnode_info.get("data") or None(treelib/tree.py:251).dict.get()already returnsNonefor a missing key, so theor Noneonly converts legitimate falsy values toNone. The method was added in #244 (merged 2026-07-23).Fix
Drop the redundant
or None:One line, no API change. Missing key still maps to
None; a present value is returned as-is.How to test
Added
test_from_json_preserves_falsy_data, next to theto_jsontests. It fails onmaster(AssertionError: None != 0) and passes with this change. Full suite: 211 passed.Notes
The identical
or Noneon theidline just above is latent:to_json()does not currently emit an"id"field, soget("id")is alreadyNone. Left untouched to keep this change to the one observed bug.Backward compatibility
No breaking changes. Callers that stored falsy data and round-tripped it now get the correct value back instead of
None.Assisted-by: Claude (code generation, reviewed and tested locally)