Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
221 additions
and
147 deletions.
There are no files selected for viewing
File renamed without changes.
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,49 @@ | ||
import pathlib as p | ||
from typing import NamedTuple, Union, Dict | ||
|
||
from typeit import type_constructor | ||
|
||
|
||
def test_path(): | ||
class X(NamedTuple): | ||
pure: p.PurePath | ||
pure_posix: p.PurePosixPath | ||
pure_win: p.PureWindowsPath | ||
path: p.Path | ||
# WinPath is not possible to instantiate on Linux, | ||
# we are omitting a test here | ||
posix_path: p.PosixPath | ||
|
||
mk_x, dict_x = type_constructor(X) | ||
|
||
data = { | ||
'pure': '/', | ||
'pure_posix': '/a/b/c', | ||
'pure_win': '\\\\a\\b\\c', | ||
'path': '\\a\\b\\c', | ||
'posix_path': '.', | ||
} | ||
x = mk_x(data) | ||
assert dict_x(x) == data | ||
|
||
|
||
def test_path_union(): | ||
class X(NamedTuple): | ||
x: Union[p.Path, Dict] | ||
|
||
mk_x, dict_x = type_constructor(X) | ||
data = { | ||
'x': '/' | ||
} | ||
x = mk_x(data) | ||
assert isinstance(x.x, p.Path) | ||
assert dict_x(x) == data | ||
|
||
data = { | ||
'x': { | ||
'x': '/' | ||
} | ||
} | ||
x = mk_x(data) | ||
assert isinstance(x.x, dict) | ||
assert dict_x(x) == data |
File renamed without changes.
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 was deleted.
Oops, something went wrong.
Empty file.
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,100 @@ | ||
from typing import NamedTuple, Union, Mapping, Any, Dict | ||
|
||
import colander | ||
import pytest | ||
|
||
from typeit import type_constructor, parser as p, flags | ||
from typeit.compat import PY36 | ||
|
||
|
||
def test_type_with_unions(): | ||
class VariantA(NamedTuple): | ||
variant_a: int | ||
|
||
class VariantB(NamedTuple): | ||
variant_b: int | ||
variant_b_attr: int | ||
|
||
class X(NamedTuple): | ||
x: Union[None, VariantA, VariantB] | ||
y: Union[str, VariantA] | ||
|
||
mk_x, dict_x = p.type_constructor(X) | ||
|
||
x = mk_x({'x': {'variant_a': 1}, 'y': 'y'}) | ||
assert isinstance(x.x, VariantA) | ||
|
||
data = {'x': {'variant_b': 1, 'variant_b_attr': 1}, 'y': 'y'} | ||
x = mk_x(data) | ||
assert isinstance(x.x, VariantB) | ||
|
||
assert data == dict_x(x) | ||
|
||
assert mk_x({'x': None, 'y': 'y'}) == mk_x({'y': 'y'}) | ||
with pytest.raises(colander.Invalid): | ||
# this is not the same as mk_x({}), | ||
# the empty structure is passed as attribute x, | ||
# which should match with only an empty named tuple definition, | ||
# which is not the same as None. | ||
mk_x({'x': {}}) | ||
|
||
|
||
def test_type_with_primitive_union(): | ||
class X(NamedTuple): | ||
x: Union[None, str] | ||
|
||
mk_x, dict_x = type_constructor(X) | ||
|
||
x = mk_x({'x': None}) | ||
assert x.x is None | ||
|
||
data = {'x': 'test'} | ||
x = mk_x(data) | ||
assert x.x == 'test' | ||
|
||
assert data == dict_x(x) | ||
|
||
|
||
def test_union_primitive_match(): | ||
class X(NamedTuple): | ||
# here, str() accepts everything that could be passed to int(), | ||
# and int() accepts everything that could be passed to float(), | ||
# and we still want to get int values instead of string values, | ||
# and float values instead of rounded int values. | ||
x: Union[str, int, float, bool] | ||
|
||
mk_x, serializer = type_constructor(X) | ||
|
||
x = mk_x({'x': 1}) | ||
assert isinstance(x.x, int) | ||
|
||
x = mk_x({'x': 1.0}) | ||
assert isinstance(x.x, float) | ||
|
||
if not PY36: | ||
# Python 3.6 has a bug that drops bool types from | ||
# unions that include int already, so | ||
# x: Union[int, bool] -- will be reduced to just `x: int` | ||
# x: Union[str, bool] -- will be left as is | ||
x = mk_x({'x': True}) | ||
assert isinstance(x.x, bool) | ||
|
||
data = {'x': '1'} | ||
x = mk_x(data) | ||
assert isinstance(x.x, str) | ||
assert serializer(x) == data | ||
|
||
|
||
def test_test_union_primitive_and_compound_types(): | ||
class X(NamedTuple): | ||
x: Union[str, Dict[str, Any]] | ||
|
||
mk_x, dict_x = type_constructor(X) | ||
mk_x_nonstrict, dict_x_nonstrict = type_constructor(X, overrides={flags.NON_STRICT_PRIMITIVES: 1}) | ||
|
||
data = {'x': {'key': 'value'}} | ||
x = mk_x(data) | ||
assert dict_x(x) == data | ||
|
||
x = mk_x_nonstrict(data) | ||
assert dict_x_nonstrict(x) == data |
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,5 @@ | ||
import sys | ||
|
||
PY_VERSION = sys.version_info[:2] | ||
PY37 = PY_VERSION == (3, 7) | ||
PY36 = PY_VERSION == (3, 6) |
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
Oops, something went wrong.