1717
1818from osw .wtsite import WtPage
1919
20+ _EMPTY = (None , [], "" , {})
2021
21- def _is_multi_valued (key : str ) -> bool :
22+
23+ def is_multi_valued (key : str ) -> bool :
2224 """True if the User field is a list or a set, False for any other key."""
2325 model_field = User .__fields__ .get (key )
2426 return model_field is not None and model_field .shape != SHAPE_SINGLETON
2527
2628
29+ def needs_clearing (key : str , value : Any ) -> bool :
30+ """True if a present stored value of ``key`` must be cleared by pruning.
31+
32+ A list or set field needs it only when it is not empty. Any other key needs
33+ it whenever it is present, also with an empty value such as ``[]``, which is
34+ not a valid value for a single-valued field.
35+ """
36+ if is_multi_valued (key ):
37+ return value not in _EMPTY
38+ return True
39+
40+
2741def clear_jsondata_keys (osw : Any , title : str , keys : Iterable [str ]) -> bool :
28- """Clear each present, non-empty key in the page's jsondata slot.
42+ """Clear each key in the page's jsondata slot that needs_clearing selects .
2943
3044 A list or set field becomes ``[]``. Any other key is deleted. The page is
3145 read with a new WtPage, which downloads the current revision and bypasses the
@@ -40,8 +54,8 @@ def clear_jsondata_keys(osw: Any, title: str, keys: Iterable[str]) -> bool:
4054 return False
4155 changed = False
4256 for key in keys :
43- if key in content and content [ key ] not in ( None , [], "" , {} ):
44- if _is_multi_valued (key ):
57+ if key in content and needs_clearing ( key , content [ key ] ):
58+ if is_multi_valued (key ):
4559 content [key ] = []
4660 else :
4761 del content [key ]
0 commit comments