Open bandophahita opened 5 months ago
I see the problem but my regex-fu is weak. We need a regex that can find int or float.
The pattern is currently:
pattern = (
r"^(?P<lower>[\[\(]?)"
r"(?P<minorant>\d+).*?(?P<majorant>\d+)"
r"(?P<upper>[\]\)]?)$"
)
It should be:
pattern = (
r"^(?P<lower>[\[\(]?)"
r"(?P<minorant>\d+\.?\d*).*?(?P<majorant>\d+\.?\d*)"
r"(?P<upper>[\]\)]?)$"
)
This regex should match a single .
if it's present, which would raise for no match if someone accidentally puts an IP address in there or something. It doesn't match a .
and then expect at least one number afterwards, but it looks like Python doesn't care if they just put a period:
>>> float("4.")
4.0
We should also cover the case where users submit .5
. i.e. `is_in_bounds("[.5, .9]")
I found [.\d]+
works and we can let the cast to float
deal with the possibility of something like IP addresses.
pattern = (
r"^(?P<lower>[\[\(]?)"
r"(?P<minorant>[.\d]+).*?(?P<majorant>[.\d]+)"
r"(?P<upper>[\]\)]?)$"
)
def test_bad_floats(self) -> None:
with pytest.raises(
ValueError, match="could not convert string to float: '1.1.1.1'"
):
is_in_bounds("(1.1.1.1, 4.0]")
Of course .\d
works because .
matches any character. Use the pattern in my previous comment please!
is_in_bounds("[1.0, 4.0]")
should return a majorant of4.0
but instead returns0.0
the number is within the range of 1.0 and 0.0