summaryrefslogtreecommitdiff
path: root/tests/test_fake_fastnumbers.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_fake_fastnumbers.py')
-rw-r--r--tests/test_fake_fastnumbers.py49
1 files changed, 25 insertions, 24 deletions
diff --git a/tests/test_fake_fastnumbers.py b/tests/test_fake_fastnumbers.py
index c75bb11..574f7cf 100644
--- a/tests/test_fake_fastnumbers.py
+++ b/tests/test_fake_fastnumbers.py
@@ -5,13 +5,14 @@ Test the fake fastnumbers module.
import unicodedata
from math import isnan
+from typing import Union, cast
from hypothesis import given
from hypothesis.strategies import floats, integers, text
from natsort.compat.fake_fastnumbers import fast_float, fast_int
-def is_float(x):
+def is_float(x: str) -> bool:
try:
float(x)
except ValueError:
@@ -25,19 +26,19 @@ def is_float(x):
return True
-def not_a_float(x):
+def not_a_float(x: str) -> bool:
return not is_float(x)
-def is_int(x):
+def is_int(x: Union[str, float]) -> bool:
try:
- return x.is_integer()
+ return cast(float, x).is_integer()
except AttributeError:
try:
int(x)
except ValueError:
try:
- unicodedata.digit(x)
+ unicodedata.digit(cast(str, x))
except (ValueError, TypeError):
return False
else:
@@ -46,7 +47,7 @@ def is_int(x):
return True
-def not_an_int(x):
+def not_an_int(x: Union[str, float]) -> bool:
return not is_int(x)
@@ -54,56 +55,56 @@ def not_an_int(x):
# and a test that uses the hypothesis module.
-def test_fast_float_returns_nan_alternate_if_nan_option_is_given():
+def test_fast_float_returns_nan_alternate_if_nan_option_is_given() -> None:
assert fast_float("nan", nan=7) == 7
-def test_fast_float_converts_float_string_to_float_example():
+def test_fast_float_converts_float_string_to_float_example() -> None:
assert fast_float("45.8") == 45.8
assert fast_float("-45") == -45.0
assert fast_float("45.8e-2", key=len) == 45.8e-2
- assert isnan(fast_float("nan"))
- assert isnan(fast_float("+nan"))
- assert isnan(fast_float("-NaN"))
+ assert isnan(cast(float, fast_float("nan")))
+ assert isnan(cast(float, fast_float("+nan")))
+ assert isnan(cast(float, fast_float("-NaN")))
assert fast_float("۱۲.۱۲") == 12.12
assert fast_float("-۱۲.۱۲") == -12.12
@given(floats(allow_nan=False))
-def test_fast_float_converts_float_string_to_float(x):
+def test_fast_float_converts_float_string_to_float(x: float) -> None:
assert fast_float(repr(x)) == x
-def test_fast_float_leaves_string_as_is_example():
+def test_fast_float_leaves_string_as_is_example() -> None:
assert fast_float("invalid") == "invalid"
@given(text().filter(not_a_float).filter(bool))
-def test_fast_float_leaves_string_as_is(x):
+def test_fast_float_leaves_string_as_is(x: str) -> None:
assert fast_float(x) == x
-def test_fast_float_with_key_applies_to_string_example():
+def test_fast_float_with_key_applies_to_string_example() -> None:
assert fast_float("invalid", key=len) == len("invalid")
@given(text().filter(not_a_float).filter(bool))
-def test_fast_float_with_key_applies_to_string(x):
+def test_fast_float_with_key_applies_to_string(x: str) -> None:
assert fast_float(x, key=len) == len(x)
-def test_fast_int_leaves_float_string_as_is_example():
+def test_fast_int_leaves_float_string_as_is_example() -> None:
assert fast_int("45.8") == "45.8"
assert fast_int("nan") == "nan"
assert fast_int("inf") == "inf"
@given(floats().filter(not_an_int))
-def test_fast_int_leaves_float_string_as_is(x):
+def test_fast_int_leaves_float_string_as_is(x: float) -> None:
assert fast_int(repr(x)) == repr(x)
-def test_fast_int_converts_int_string_to_int_example():
+def test_fast_int_converts_int_string_to_int_example() -> None:
assert fast_int("-45") == -45
assert fast_int("+45") == 45
assert fast_int("۱۲") == 12
@@ -111,23 +112,23 @@ def test_fast_int_converts_int_string_to_int_example():
@given(integers())
-def test_fast_int_converts_int_string_to_int(x):
+def test_fast_int_converts_int_string_to_int(x: int) -> None:
assert fast_int(repr(x)) == x
-def test_fast_int_leaves_string_as_is_example():
+def test_fast_int_leaves_string_as_is_example() -> None:
assert fast_int("invalid") == "invalid"
@given(text().filter(not_an_int).filter(bool))
-def test_fast_int_leaves_string_as_is(x):
+def test_fast_int_leaves_string_as_is(x: str) -> None:
assert fast_int(x) == x
-def test_fast_int_with_key_applies_to_string_example():
+def test_fast_int_with_key_applies_to_string_example() -> None:
assert fast_int("invalid", key=len) == len("invalid")
@given(text().filter(not_an_int).filter(bool))
-def test_fast_int_with_key_applies_to_string(x):
+def test_fast_int_with_key_applies_to_string(x: str) -> None:
assert fast_int(x, key=len) == len(x)