|
1
|
|
|
from typing import Callable, List, Set |
|
2
|
|
|
from unittest import TestCase |
|
3
|
|
|
|
|
4
|
|
|
from test_resources import some_module |
|
5
|
|
|
from typish import Something, TypingType |
|
6
|
|
|
|
|
7
|
|
|
Inyerface = Something[{ |
|
8
|
|
|
'a': int, |
|
9
|
|
|
'b': Callable[[int, int], str], |
|
10
|
|
|
}] |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class C1: |
|
14
|
|
|
a = 42 |
|
15
|
|
|
|
|
16
|
|
|
def b(self, a: int, b: int) -> str: |
|
17
|
|
|
return str(a + b) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class C2: |
|
21
|
|
|
def __init__(self): |
|
22
|
|
|
self.a = 42 |
|
23
|
|
|
|
|
24
|
|
|
def b(self, a: int, b: int) -> str: |
|
25
|
|
|
return str(a + b) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class C3: |
|
29
|
|
|
def __init__(self): |
|
30
|
|
|
self.a = 42 |
|
31
|
|
|
|
|
32
|
|
|
def b(self, a, b): |
|
33
|
|
|
return str(a + b) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class C4: |
|
37
|
|
|
def __init__(self): |
|
38
|
|
|
self.a = 42 |
|
39
|
|
|
|
|
40
|
|
|
def b(self, a, b: str): |
|
41
|
|
|
return str(a + b) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
class C5: |
|
45
|
|
|
a = 42 |
|
46
|
|
|
|
|
47
|
|
|
@staticmethod |
|
48
|
|
|
def b(a: int, b: int) -> str: |
|
49
|
|
|
return str(a + b) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
class C6: |
|
53
|
|
|
a = 42 |
|
54
|
|
|
|
|
55
|
|
|
@classmethod |
|
56
|
|
|
def b(cls, a: int, b: int) -> str: |
|
57
|
|
|
return str(a + b) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
class TestSomething(TestCase): |
|
61
|
|
|
def test_something_with_slices(self): |
|
62
|
|
|
self.assertEqual(Inyerface, Something['a': int, 'b': Callable[[int, int], str]]) |
|
63
|
|
|
|
|
64
|
|
|
def test_something_instance_check(self): |
|
65
|
|
|
self.assertTrue(isinstance(C1(), Inyerface)) |
|
66
|
|
|
self.assertTrue(isinstance(C2(), Inyerface)) |
|
67
|
|
|
self.assertTrue(not isinstance(C3(), Inyerface)) |
|
68
|
|
|
self.assertTrue(not isinstance(C4(), Inyerface)) |
|
69
|
|
|
self.assertTrue(isinstance(C5(), Inyerface)) |
|
70
|
|
|
self.assertTrue(isinstance(C6(), Inyerface)) |
|
71
|
|
|
|
|
72
|
|
|
def test_something_subclass_check(self): |
|
73
|
|
|
self.assertTrue(issubclass(C1, Inyerface)) |
|
74
|
|
|
self.assertTrue(issubclass(C2, Inyerface)) |
|
75
|
|
|
self.assertTrue(not issubclass(C3, Inyerface)) |
|
76
|
|
|
self.assertTrue(not issubclass(C4, Inyerface)) |
|
77
|
|
|
self.assertTrue(issubclass(C5, Inyerface)) |
|
78
|
|
|
self.assertTrue(issubclass(C6, Inyerface)) |
|
79
|
|
|
|
|
80
|
|
|
def test_module_something_instance_check(self): |
|
81
|
|
|
self.assertTrue(isinstance(some_module, Inyerface)) |
|
82
|
|
|
|
|
83
|
|
|
def test_something_repr(self): |
|
84
|
|
|
self.assertEqual("typish.Something['x': int]", str(Something['x': int])) |
|
85
|
|
|
self.assertEqual("typish.Something['a': int, 'b': typing.Callable[[int, int], str]]", |
|
86
|
|
|
repr(Inyerface)) |
|
87
|
|
|
|
|
88
|
|
|
def test_isinstance_generic_collection(self): |
|
89
|
|
|
isinstance(List[int], TypingType) |
|
90
|
|
|
isinstance(Set[str], TypingType) |
|
91
|
|
|
|
|
92
|
|
|
def test_hash_something(self): |
|
93
|
|
|
# If the lines below raise no errors, the test succeeds. |
|
94
|
|
|
hash(Something['abc': int]) |
|
95
|
|
|
L = List[Something['abc': int]] |
|
96
|
|
|
|