Skip to content

Commit

Permalink
Restrict underscore formats which are converted to float
Browse files Browse the repository at this point in the history
Disallow numbers with trailing underscore from being converted to float.

The specific change here is to treat 0_e0 as a string.

Fixes issue #838
  • Loading branch information
pixelb committed Aug 10, 2022
1 parent 09b1f46 commit a4b95d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
8 changes: 4 additions & 4 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,21 @@
("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),
("float_exp_4", "1.07e+2", 107.0),
("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),
Expand Down

0 comments on commit a4b95d1

Please sign in to comment.