-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from explosion/feature/common-converter-utils
Add commonly used types/converters to core library
- Loading branch information
Showing
6 changed files
with
137 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Union, Generic, List, TypeVar | ||
from pathlib import Path | ||
from uuid import UUID | ||
import pytest | ||
import shutil | ||
from radicli.util import stringify_type, get_list_converter | ||
|
||
_KindT = TypeVar("_KindT", bound=Union[str, int, float, Path]) | ||
|
||
|
||
class CustomGeneric(Generic[_KindT]): | ||
... | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"arg_type,expected", | ||
[ | ||
(str, "str"), | ||
(bool, "bool"), | ||
(Path, "Path"), | ||
(List[int], "List[int]"), | ||
(CustomGeneric, "CustomGeneric"), | ||
(CustomGeneric[str], "CustomGeneric[str]"), | ||
(UUID, "UUID"), | ||
(shutil.rmtree, "rmtree"), | ||
("foo.bar", "foo.bar"), | ||
(None, None), | ||
], | ||
) | ||
def test_stringify_type(arg_type, expected): | ||
assert stringify_type(arg_type) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"item_type,value,expected", | ||
[ | ||
# Separated string | ||
(str, "hello, world,test", ["hello", "world", "test"]), | ||
(int, " 1,2,3 ", [1, 2, 3]), | ||
(float, "0.123,5, 1.234", [0.123, 5.0, 1.234]), | ||
# Quoted list | ||
(str, "[hello, world]", ["hello", "world"]), | ||
(str, '["hello", "world"]', ["hello", "world"]), | ||
(str, "['hello', 'world']", ["hello", "world"]), | ||
(int, "[1,2,3]", [1, 2, 3]), | ||
(int, '["1","2","3"]', [1, 2, 3]), | ||
(int, "['1','2','3']", [1, 2, 3]), | ||
(float, "[0.23,2,3.45]", [0.23, 2.0, 3.45]), | ||
(float, '["0.23","2","3.45"]', [0.23, 2.0, 3.45]), | ||
(float, "['0.23','2','3.45']", [0.23, 2.0, 3.45]), | ||
], | ||
) | ||
def test_get_list_converter(item_type, value, expected): | ||
converter = get_list_converter(item_type) | ||
assert converter(value) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters