-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_tc006.py
162 lines (142 loc) · 3.31 KB
/
test_tc006.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
This file tests the TC006 error:
>> Annotation in typing.cast() should be a string literal
Types passed to typing.cast() are only ever used by type checkers, never at
runtime. Despite this, constructing complex types at runtime can have a very
significant overhead in hot paths. This can be avoided by quoting the type so
that it isn't resolved at runtime.
"""
import textwrap
import pytest
from flake8_type_checking.constants import TC006
from tests.conftest import _get_error
examples = [
# No error
('', set()),
# Simple type unquoted
(
textwrap.dedent(
"""
from typing import cast
cast(int, 3.0)
"""
),
{'4:5 ' + TC006.format(annotation='int')},
),
# Complex type unquoted
(
textwrap.dedent(
"""
from typing import cast
cast(list[tuple[bool | float | int | str]], 3.0)
"""
),
{'4:5 ' + TC006.format(annotation='list[tuple[bool | float | int | str]]')},
),
# Complex type unquoted using Union
(
textwrap.dedent(
"""
from typing import Union, cast
cast(list[tuple[Union[bool, float, int, str]]], 3.0)
"""
),
{'4:5 ' + TC006.format(annotation='list[tuple[Union[bool, float, int, str]]]')},
),
# Simple type quoted
(
textwrap.dedent(
"""
from typing import cast
cast("int", 3.0)
"""
),
set(),
),
# Complex type quoted
(
textwrap.dedent(
"""
from typing import cast
cast("list[tuple[bool | float | int | str]]", 3.0)
"""
),
set(),
),
# Complex type quoted using Union
(
textwrap.dedent(
"""
from typing import Union, cast
cast("list[tuple[Union[bool, float, int, str]]]", 3.0)
"""
),
set(),
),
# Call aliased function
(
textwrap.dedent(
"""
from typing import cast as typecast
typecast(int, 3.0)
"""
),
{'4:9 ' + TC006.format(annotation='int')},
),
# Call function from module
(
textwrap.dedent(
"""
import typing
typing.cast(int, 3.0)
"""
),
{'4:12 ' + TC006.format(annotation='int')},
),
# Call function from aliased module
(
textwrap.dedent(
"""
import typing as t
t.cast(int, 3.0)
"""
),
{'4:7 ' + TC006.format(annotation='int')},
),
# re-export of cast using a registered compat module
(
textwrap.dedent(
"""
from mylib import compat
compat.cast(int, 3.0)
"""
),
{'4:12 ' + TC006.format(annotation='int')},
),
(
textwrap.dedent(
"""
from .compat import cast
cast(int, 3.0)
"""
),
{'4:5 ' + TC006.format(annotation='int')},
),
(
textwrap.dedent(
"""
from ..compat import cast
cast(int, 3.0)
"""
),
{'4:5 ' + TC006.format(annotation='int')},
),
]
@pytest.mark.parametrize(('example', 'expected'), examples)
def test_TC006_errors(example, expected):
assert (
_get_error(
example, error_code_filter='TC006', type_checking_typing_modules=['mylib.compat', '.compat', '..compat']
)
== expected
)