diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index dc2dc304c..7a8e1a284 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -155,10 +155,10 @@ def construct_mapping(self, node: yaml.Node, deep: bool = False) -> Any: "tag:yaml.org,2002:float", re.compile( """^(?: - [-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)? - |[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+) - |\\.[0-9_]+(?:[eE][-+][0-9]+)? - |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]* + [-+]?[0-9]+(?:_[0-9]+)*\\.[0-9_]*(?:[eE][-+]?[0-9]+)? + |[-+]?[0-9]+(?:_[0-9]+)*(?:[eE][-+]?[0-9]+) + |\\.[0-9]+(?:_[0-9]+)*(?:[eE][-+][0-9]+)? + |[-+]?[0-9]+(?:_[0-9]+)*(?::[0-5]?[0-9])+\\.[0-9_]* |[-+]?\\.(?:inf|Inf|INF) |\\.(?:nan|NaN|NAN))$""", re.X, diff --git a/tests/test_create.py b/tests/test_create.py index ada960e8a..66b073cd8 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -337,6 +337,42 @@ def test_create_unmodified_loader() -> None: assert yaml_cfg["gitrev"] == "100e100" +def test_create_float_yaml() -> None: + # Note there are some discrepencies with the antrl parser. + # The following follow the yaml more closely, + # but arguably the antlr interpretation is better (which also + # more closely matches python. Specifically: + # c_s not parsed as float. antlr does parse as float + # e_s and f_s not parsed. antlr does parse as float + # h_f and i_f parsed as float. antlr does not parse as float + cfg = OmegaConf.create( + dedent( + """\ + a_s: 0_e0 + b_i: 0_0 + c_s: 1_0e1_0 + d_f: .5 + e_s: +.9 + f_s: -.9 + g_f: 1_1_2.1 + h_f: 1__2.1 + i_f: 1.2_ + """ + ) + ) + assert cfg == { + "a_s": "0_e0", + "b_i": 0, + "c_s": "1_0e1_0", + "d_f": 0.5, + "e_s": "+.9", + "f_s": "-.9", + "g_f": 112.1, + "h_f": 12.1, + "i_f": 1.2, + } + + def test_create_untyped_list() -> None: from omegaconf._utils import get_type_hint diff --git a/tests/test_grammar.py b/tests/test_grammar.py index 4ef739201..5dcedfd0d 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -89,10 +89,13 @@ ("float_no_int", ".1", 0.1), ("float_no_decimal", "1.", 1.0), ("float_minus", "-.2", -0.2), - ("float_underscore", "1.1_1", 1.11), + ("float_plus", "+.2", 0.2), + ("float_underscore_1", "1.1_1", 1.11), + ("float_underscore_2", "1_1_2.1", 112.1), ("float_bad_1", "1.+2", "1.+2"), ("float_bad_2", r"1\.2", r"1\.2"), ("float_bad_3", "1.2_", "1.2_"), + ("float_bad_4", "1__1.2", "1__1.2"), ("float_exp_1", "-1e2", -100.0), ("float_exp_2", "+1E-2", 0.01), ("float_exp_3", "1_0e1_0", 10e10), @@ -100,6 +103,7 @@ ("float_exp_5", "1e+03", 1000.0), ("float_exp_bad_1", "e-2", "e-2"), ("float_exp_bad_2", "01e2", "01e2"), + ("float_exp_bad_3", "0_e0", "0_e0"), ("float_inf", "inf", math.inf), ("float_plus_inf", "+inf", math.inf), ("float_minus_inf", "-inf", -math.inf),