Passed
Pull Request — master (#13)
by Ramon
01:38
created

test_resources.hintable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A TestHintable.test_override_with_custom_hint() 0 8 1
A TestHintable.test_hintable() 0 10 1
A SomeClass.some_method() 0 3 1
A TestHintable.test_hintable_method() 0 11 1
A TestHintable.test_hintable_with_custom_param_name() 0 7 1
A TestHintable.test_hintable_with_comment_hint() 0 11 1
A TestHintable.test_hintable_with_custom_type() 0 8 1
A TestHintable.test_hintable_with_textual_hint() 0 8 1
A SomeClass.some_static_method() 0 4 1
A SomeClass.some_class_method() 0 4 1
A C.__init__() 0 2 1
A TestHintable.test_hintable_with_parentheses() 0 10 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A some_func() 0 4 1
A some_func_with_custom_param_name() 0 3 1
A cast() 0 3 1
1
from typing import Type
2
from unittest import TestCase
3
4
from typish import T, hintable
5
6
7
class C:
8
    def __init__(self, subject):
9
        self.subject = subject
10
11
12
@hintable
13
def cast(subject, hint: Type[T]) -> T:
14
    return hint(subject)
15
16
17
@hintable
18
def some_func(hint: Type[T]) -> Type[T]:
19
    """Some docstring"""
20
    return hint
21
22
23
@hintable(param='cls')
24
def some_func_with_custom_param_name(cls):
25
    return cls
26
27
28
class SomeClass:
29
    @hintable
30
    def some_method(self, hint):
31
        return hint
32
33
    @staticmethod
34
    @hintable
35
    def some_static_method(hint):
36
        return hint
37
38
    @classmethod
39
    @hintable
40
    def some_class_method(cls, hint):
41
        return hint
42
43
44
class TestHintable(TestCase):
45
    def test_hintable(self):
46
        # Test that a function can be decorated and receives a hint.
47
48
        x: int = cast('42')
49
        y: str = cast(42)
50
        z: '  str  ' = cast(42)  # Even a sloppy hint should work.
51
52
        self.assertEqual(42, x)
53
        self.assertEqual('42', y)
54
        self.assertEqual('42', z)
55
56
    def test_hintable_with_parentheses(self):
57
        # Test that hintable can be used with parentheses as well.
58
59
        @hintable()  # Note the parentheses.
60
        def some_function(hint):
61
            return hint
62
63
        x: int = some_function()
64
65
        self.assertEqual(int, x)
66
67
    def test_hintable_with_custom_param_name(self):
68
        # Test that functions can customize the parameter name that receives
69
        # the type hint.
70
71
        x: int = some_func_with_custom_param_name()
72
73
        self.assertEqual(int, x)
74
75
    def test_hintable_method(self):
76
        # Test that methods can be hintable as well.
77
78
        sc = SomeClass()
79
        x: int = sc.some_method()
80
        y: float = SomeClass.some_static_method()
81
        z: str = SomeClass.some_class_method()
82
83
        self.assertEqual(int, x)
84
        self.assertEqual(float, y)
85
        self.assertEqual(str, z)
86
87
    def test_hintable_with_custom_type(self):
88
        # Test that a custom type can be used as hint without a problem.
89
90
        x: C = cast(42)
91
        y: 'C' = cast(42)
92
93
        self.assertTrue(isinstance(x, C))
94
        self.assertTrue(isinstance(y, C))
95
96
    def test_hintable_with_textual_hint(self):
97
        # Test that textual hints are received as strings.
98
99
        x: 'some rubbish' = some_func()
100
        y: "'some even greater rubbish'" = some_func()
101
102
        self.assertEqual('some rubbish', x)
103
        self.assertEqual('\'some even greater rubbish\'', y)
104
105
    def test_hintable_with_comment_hint(self):
106
        # Test that hints in MyPy style work as well.
107
108
        x = some_func()  # type: int
109
        y = some_func()  # type: rubbish_again
110
        # The type hint should take precedence of MyPy-styled-hints:
111
        z: int = some_func()  # type: str
0 ignored issues
show
introduced by
misplaced type annotation (<unknown>, line 111)
Loading history...
112
113
        self.assertEqual(int, x)
114
        self.assertEqual('rubbish_again', y)
115
        self.assertEqual(int, z)
116
117
    def test_override_with_custom_hint(self):
118
        # Test that you can still override the hint.
119
120
        x = some_func(hint=int)
121
        y: int = some_func(hint=str)  # It's allowed, but is it a good idea?
122
123
        self.assertEqual(int, x)
124
        self.assertEqual(str, y)
125