Skip to content

Commit

Permalink
dev(hansbug): add test for typing
Browse files Browse the repository at this point in the history
  • Loading branch information
HansBug committed Aug 29, 2022
1 parent ef3d241 commit 5d70def
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugins/dict
87 changes: 87 additions & 0 deletions test/rules/test_installed.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import NoReturn, List, Dict, Set, Tuple, Callable, Any, TypeVar

import pytest

import test
Expand All @@ -12,6 +14,13 @@ def potc_dict_installed():
yield


@pytest.fixture()
def potc_typing_installed():
with with_pythonpath('plugins/typing'):
with mock_potc_plugins('potc_typing.plugin'):
yield


@pytest.fixture()
def potc_invalid_plugin_1_installed():
with mock_potc_plugins(test): # module without __rules__
Expand Down Expand Up @@ -49,3 +58,81 @@ def test_potc_invalid_plugin_2(self, potc_invalid_plugin_2_installed):
with pytest.raises(TypeError):
with transobj_assert({'a': 1, 'b': 2}) as (obj, name):
pytest.fail('Should not reach here.')

def test_typing_installed(self, potc_typing_installed):
with transobj_assert(NoReturn) as (obj, name):
assert obj is NoReturn

with transobj_assert(List) as (obj, name):
assert obj == List
with transobj_assert(Dict) as (obj, name):
assert obj == Dict
with transobj_assert(Set) as (obj, name):
assert obj == Set
with transobj_assert(Tuple) as (obj, name):
assert obj == Tuple
with transobj_assert(Callable) as (obj, name):
assert obj == Callable

with transobj_assert(List[int]) as (obj, name):
assert obj == List[int]
with transobj_assert(Dict[int, List[str]]) as (obj, name):
assert obj == Dict[int, List[str]]
with transobj_assert(Set[str]) as (obj, name):
assert obj == Set[str]
with transobj_assert(Tuple[int, str, List[str]]) as (obj, name):
assert obj == Tuple[int, str, List[str]]
with transobj_assert(Callable[..., int]) as (obj, name):
assert obj == Callable[..., int]
with transobj_assert(Callable[[], int]) as (obj, name):
assert obj == Callable[[], int]
with transobj_assert(Callable[[int, Any], str]) as (obj, name):
assert obj == Callable[[int, Any], str]
with transobj_assert(Callable[[int], Any]) as (obj, name):
assert obj == Callable[[int], Any]

try:
_ = list[int]
except TypeError:
pass
else:
with transobj_assert(list[int]) as (obj, name):
assert obj == list[int]
with transobj_assert(dict[int, list[int]]) as (obj, name):
assert obj == dict[int, list[int]]

K = TypeVar('K', bound=int, contravariant=True)
V = TypeVar('V', int, str, covariant=True)

with transobj_assert(K) as (obj, name):
assert isinstance(obj, TypeVar)
assert obj.__name__ == 'K'
assert obj.__constraints__ == ()
assert obj.__bound__ == int
assert not obj.__covariant__
assert obj.__contravariant__

with transobj_assert(V) as (obj, name):
assert isinstance(obj, TypeVar)
assert obj.__name__ == 'V'
assert obj.__constraints__ == (int, str)
assert obj.__bound__ is None
assert obj.__covariant__
assert not obj.__contravariant__

with transobj_assert(Dict[K, V]) as (obj, name):
assert obj.__origin__ in {Dict, dict}
_k, _v = obj.__args__
assert isinstance(_k, TypeVar)
assert _k.__name__ == 'K'
assert _k.__constraints__ == ()
assert _k.__bound__ == int
assert not _k.__covariant__
assert _k.__contravariant__

assert isinstance(_v, TypeVar)
assert _v.__name__ == 'V'
assert _v.__constraints__ == (int, str)
assert _v.__bound__ is None
assert _v.__covariant__
assert not _v.__contravariant__

0 comments on commit 5d70def

Please sign in to comment.