Skip to content

Commit

Permalink
Make _parse_size robust to already parsed values (#142)
Browse files Browse the repository at this point in the history
* Make _parse_size robust to already parsed values
(needed for unpickling a folium Map)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add safety & tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
BastienGauthier and pre-commit-ci[bot] authored Oct 16, 2023
1 parent de98b15 commit ce17e54
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions branca/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ def _parse_size(value):
raise ValueError(
f"Cannot parse {value!r}, it should be a number followed by a unit.",
)
elif (
isinstance(value, tuple)
and isinstance(value[0], (int, float))
and isinstance(value[1], str)
):
# value had been already parsed
return (float(value[0]), value[1])
else:
raise TypeError(
f"Cannot parse {value!r}, it should be a number or a string containing a number and a unit.",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def test_color_avoid_unexpected_error():
("100% ", (100.0, "%")),
("3 vw", (3.0, "vw")),
("3.14 rem", (3.14, "rem")),
((1, "px"), (1.0, "px")),
((80.0, "%"), (80.0, "%")),
],
)
def test_parse_size(value, result):
Expand All @@ -128,6 +130,7 @@ def test_parse_size(value, result):
"what?",
"1.21 jigawatts",
ut._parse_size,
(1.21, 4.9),
],
)
def test_parse_size_exceptions(value):
Expand Down

0 comments on commit ce17e54

Please sign in to comment.