File: check-newsyntax.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (129 lines) | stat: -rw-r--r-- 3,829 bytes parent folder | download | duplicates (3)
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
[case testNewSyntaxSyntaxError]
x: int: int  # E: Invalid syntax
[out]

[case testNewSyntaxBasics]
x: int
x = 5
y: int = 5

a: str
a = 5  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
b: str = 5  # E: Incompatible types in assignment (expression has type "int", variable has type "str")

zzz: int
zzz: str  # E: Name "zzz" already defined on line 9
[out]

[case testNewSyntaxWithDict]
from typing import Dict, Any

d: Dict[int, str] = {}
d[42] = 'ab'
d[42] = 42  # E: Incompatible types in assignment (expression has type "int", target has type "str")
d['ab'] = 'ab'  # E: Invalid index type "str" for "dict[int, str]"; expected type "int"
[builtins fixtures/dict.pyi]
[out]

[case testNewSyntaxWithRevealType]
from typing import Dict

def tst_local(dct: Dict[int, T]) -> Dict[T, int]:
    ret: Dict[T, int] = {}
    return ret

reveal_type(tst_local({1: 'a'}))  # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
[builtins fixtures/dict.pyi]
[out]

[case testNewSyntaxWithInstanceVars]
class TstInstance:
    a: str
    def __init__(self) -> None:
        self.x: int

TstInstance().x = 5
TstInstance().x = 'ab'  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
TstInstance().a = 5  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
TstInstance().a = 'ab'
[out]

[case testNewSyntaxWithClassVars]
class CCC:
    a: str = None  # E: Incompatible types in assignment (expression has type "None", variable has type "str")
[out]

[case testNewSyntaxWithStrictOptional]
strict: int
strict = None  # E: Incompatible types in assignment (expression has type "None", variable has type "int")
strict2: int = None  # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]

[case testNewSyntaxWithStrictOptionalFunctions]
def f() -> None:
    x: int
    if int():
        x = None  # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]

[case testNewSyntaxWithStrictOptionalClasses]
class C:
    def meth(self) -> None:
        x: int = None  # E: Incompatible types in assignment (expression has type "None", variable has type "int")
        self.x: int = None  # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[out]

[case testNewSyntaxSpecialAssign]
class X:
    x: str
    x[0]: int
    x.x: int

[out]
main:3: error: Unexpected type declaration
main:3: error: Unsupported target for indexed assignment ("str")
main:4: error: Type cannot be declared in assignment to non-self attribute
main:4: error: "str" has no attribute "x"

[case testNewSyntaxFStringBasics]
f'foobar'
f'{"foobar"}'
f'foo{"bar"}'
f'.{1}.'
f'{type(1)}'
a: str
a = f'foobar'
a = f'{"foobar"}'
[builtins fixtures/f_string.pyi]

[case testNewSyntaxFStringExpressionsOk]
f'.{1 + 1}.'
f'.{1 + 1}.{"foo" + "bar"}'
[builtins fixtures/f_string.pyi]

[case testNewSyntaxFStringExpressionsErrors]
f'{1 + ""}'
f'.{1 + ""}'
[builtins fixtures/f_string.pyi]
[out]
main:1: error: Unsupported operand types for + ("int" and "str")
main:2: error: Unsupported operand types for + ("int" and "str")

[case testNewSyntaxFStringParseFormatOptions]
value = 10.5142
width = 10
precision = 4
f'result: {value:{width}.{precision}}'
[builtins fixtures/f_string.pyi]

[case testNewSyntaxFStringSingleField]
v = 1
reveal_type(f'{v}') # N: Revealed type is "builtins.str"
reveal_type(f'{1}') # N: Revealed type is "builtins.str"
[builtins fixtures/f_string.pyi]

[case testFeatureVersionSuggestion]
# flags: --python-version 3.99
x *** x this is what future python looks like public static void main String[] args await goto exit
[out]
main:2: error: Invalid syntax; you likely need to run mypy using Python 3.99 or newer