11"""
22Reusable value and type casting transformations
33
4+ This module is stable on a "best effort"-basis, and small backwards incompatibilities
5+ can be introduced in "minor"/"patch" version bumps (it will not be considered a
6+ regression automatically).
7+ While it can be used by plugin writers, it is not intended for general public use.
8+
49.. testsetup:: *
510
611 # workaround for missing import in sphinx-doctest
5358 For example: transforming ``"2"`` (string) into ``2`` (integer).
5459- The second one tries to preserve metadata (such as comments) from the original CFG/INI
5560 file. This kind of transformation processes a string value into an intermediary
56- representation (e.g. :obj:`Commented`, :obj:`CommentedList`, obj:`CommentedKV`)
61+ representation (e.g. :obj:`Commented`, :obj:`CommentedList`, : obj:`CommentedKV`)
5762 that needs to be properly handled before adding to the TOML document.
5863
5964In a higher level we can also consider an ensemble of transformations that transform an
6873
6974
7075def noop (x : T ) -> T :
76+ """Return the value unchanged"""
7177 return x
7278
7379
7480def is_true (value : str ) -> bool :
81+ """``value in ("true", "1", "yes", "on")``"""
7582 value = value .lower ()
7683 return value in ("true" , "1" , "yes" , "on" )
7784
7885
7986def is_false (value : str ) -> bool :
87+ """``value in ("false", "0", "no", "off", "none", "null", "nil")``"""
8088 value = value .lower ()
8189 return value in ("false" , "0" , "no" , "off" , "none" , "null" , "nil" )
8290
@@ -87,6 +95,7 @@ def is_float(value: str) -> bool:
8795
8896
8997def coerce_bool (value : str ) -> bool :
98+ """Convert the value based on :func:`~.is_true` and :func:`~.is_false`."""
9099 if is_true (value ):
91100 return True
92101 if is_false (value ):
@@ -158,6 +167,7 @@ def split_comment(
158167
159168
160169def split_comment (value , coerce_fn = noop , comment_prefixes = CP ):
170+ """Split a "comment suffix" from the value."""
161171 if not isinstance (value , str ):
162172 return value
163173 value = value .strip ()
@@ -176,6 +186,7 @@ def split_comment(value, coerce_fn=noop, comment_prefixes=CP):
176186
177187
178188def split_scalar (value : str , * , comment_prefixes = CP ) -> Commented [Scalar ]:
189+ """Combination of :func:`~.split_comment` and :func:`~.coerce_scalar`."""
179190 return split_comment (value , coerce_scalar , comment_prefixes )
180191
181192
0 commit comments