Debian Codegraph @

test-data/unit/check-newsyntax.test

raw
1 [case testNewSyntaxSyntaxError_no_native_parse]
2 x: int: int # E: Invalid syntax
3 [out]
4
5 [case testNewSyntaxBasics]
6 x: int
7 x = 5
8 y: int = 5
9
10 a: str
11 a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
12 b: str = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
13
14 zzz: int
15 zzz: str # E: Name "zzz" already defined on line 9
16 [out]
17
18 [case testNewSyntaxWithDict]
19 from typing import Dict, Any
20
21 d: Dict[int, str] = {}
22 d[42] = 'ab'
23 d[42] = 42 # E: Incompatible types in assignment (expression has type "int", target has type "str")
24 d['ab'] = 'ab' # E: Invalid index type "str" for "dict[int, str]"; expected type "int"
25 [builtins fixtures/dict.pyi]
26 [out]
27
28 [case testNewSyntaxWithRevealType]
29 from typing import Dict
30
31 def tst_local(dct: Dict[int, T]) -> Dict[T, int]:
32 ret: Dict[T, int] = {}
33 return ret
34
35 reveal_type(tst_local({1: 'a'})) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
36 [builtins fixtures/dict.pyi]
37 [out]
38
39 [case testNewSyntaxWithInstanceVars]
40 class TstInstance:
41 a: str
42 def __init__(self) -> None:
43 self.x: int
44
45 TstInstance().x = 5
46 TstInstance().x = 'ab' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
47 TstInstance().a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
48 TstInstance().a = 'ab'
49 [out]
50
51 [case testNewSyntaxWithClassVars]
52 class CCC:
53 a: str = None # E: Incompatible types in assignment (expression has type "None", variable has type "str")
54 [out]
55
56 [case testNewSyntaxWithStrictOptional]
57 strict: int
58 strict = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
59 strict2: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
60 [out]
61
62 [case testNewSyntaxWithStrictOptionalFunctions]
63 def f() -> None:
64 x: int
65 if int():
66 x = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
67 [out]
68
69 [case testNewSyntaxWithStrictOptionalClasses]
70 class C:
71 def meth(self) -> None:
72 x: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
73 self.x: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
74 [out]
75
76 [case testNewSyntaxSpecialAssign]
77 class X:
78 x: str
79 x[0]: int
80 x.x: int
81
82 [out]
83 main:3: error: Unexpected type declaration
84 main:3: error: Unsupported target for indexed assignment ("str")
85 main:4: error: Type cannot be declared in assignment to non-self attribute
86 main:4: error: "str" has no attribute "x"
87
88 [case testNewSyntaxFStringBasics]
89 f'foobar'
90 f'{"foobar"}'
91 f'foo{"bar"}'
92 f'.{1}.'
93 f'{type(1)}'
94 a: str
95 a = f'foobar'
96 a = f'{"foobar"}'
97 [builtins fixtures/f_string.pyi]
98
99 [case testNewSyntaxFStringExpressionsOk]
100 f'.{1 + 1}.'
101 f'.{1 + 1}.{"foo" + "bar"}'
102 [builtins fixtures/f_string.pyi]
103
104 [case testNewSyntaxFStringExpressionsErrors]
105 f'{1 + ""}'
106 f'.{1 + ""}'
107 [builtins fixtures/f_string.pyi]
108 [out]
109 main:1: error: Unsupported operand types for + ("int" and "str")
110 main:2: error: Unsupported operand types for + ("int" and "str")
111
112 [case testNewSyntaxFStringParseFormatOptions]
113 value = 10.5142
114 width = 10
115 precision = 4
116 f'result: {value:{width}.{precision}}'
117 [builtins fixtures/f_string.pyi]
118
119 [case testNewSyntaxFStringSingleField]
120 v = 1
121 reveal_type(f'{v}') # N: Revealed type is "builtins.str"
122 reveal_type(f'{1}') # N: Revealed type is "builtins.str"
123 [builtins fixtures/f_string.pyi]
124
125 [case testFeatureVersionSuggestion_no_native_parse]
126 # flags: --python-version 3.99
127 x *** x this is what future python looks like public static void main String[] args await goto exit
128 [out]
129 main:2: error: Invalid syntax; you likely need to run mypy using Python 3.99 or newer