File tree Expand file tree Collapse file tree 3 files changed +123
-0
lines changed
Misc/NEWS.d/next/Core and Builtins Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ import textwrap
12import unittest
3+ from test .support import run_code
24
35class TypeAnnotationTests (unittest .TestCase ):
46
@@ -101,3 +103,112 @@ class D(metaclass=C):
101103 with self .assertRaises (AttributeError ):
102104 del D .__annotations__
103105 self .assertEqual (D .__annotations__ , {})
106+
107+
108+ class TestSetupAnnotations (unittest .TestCase ):
109+ def check (self , code : str ):
110+ code = textwrap .dedent (code )
111+ for scope in ("module" , "class" ):
112+ with self .subTest (scope = scope ):
113+ if scope == "class" :
114+ code = f"class C:\n { textwrap .indent (code , ' ' )} "
115+ ns = run_code (code )
116+ if scope == "class" :
117+ annotations = ns ["C" ].__annotations__
118+ else :
119+ annotations = ns ["__annotations__" ]
120+ self .assertEqual (annotations , {"x" : int })
121+
122+ def test_top_level (self ):
123+ self .check ("x: int = 1" )
124+
125+ def test_blocks (self ):
126+ self .check ("if True:\n x: int = 1" )
127+ self .check ("""
128+ while True:
129+ x: int = 1
130+ break
131+ """ )
132+ self .check ("""
133+ while False:
134+ pass
135+ else:
136+ x: int = 1
137+ """ )
138+ self .check ("""
139+ for i in range(1):
140+ x: int = 1
141+ """ )
142+ self .check ("""
143+ for i in range(1):
144+ pass
145+ else:
146+ x: int = 1
147+ """ )
148+
149+ def test_try (self ):
150+ self .check ("""
151+ try:
152+ x: int = 1
153+ except:
154+ pass
155+ """ )
156+ self .check ("""
157+ try:
158+ pass
159+ except:
160+ pass
161+ else:
162+ x: int = 1
163+ """ )
164+ self .check ("""
165+ try:
166+ pass
167+ except:
168+ pass
169+ finally:
170+ x: int = 1
171+ """ )
172+ self .check ("""
173+ try:
174+ 1/0
175+ except:
176+ x: int = 1
177+ """ )
178+
179+ def test_try_star (self ):
180+ self .check ("""
181+ try:
182+ x: int = 1
183+ except* Exception:
184+ pass
185+ """ )
186+ self .check ("""
187+ try:
188+ pass
189+ except* Exception:
190+ pass
191+ else:
192+ x: int = 1
193+ """ )
194+ self .check ("""
195+ try:
196+ pass
197+ except* Exception:
198+ pass
199+ finally:
200+ x: int = 1
201+ """ )
202+ self .check ("""
203+ try:
204+ 1/0
205+ except* Exception:
206+ x: int = 1
207+ """ )
208+
209+ def test_match (self ):
210+ self .check ("""
211+ match 0:
212+ case 0:
213+ x: int = 1
214+ """ )
Original file line number Diff line number Diff line change 1+ Ensure annotations are set up correctly if the only annotation in a block is
2+ within a :keyword: `match ` block. Patch by Jelle Zijlstra.
Original file line number Diff line number Diff line change @@ -1422,8 +1422,18 @@ find_ann(asdl_stmt_seq *stmts)
14221422 find_ann (st -> v .TryStar .finalbody ) ||
14231423 find_ann (st -> v .TryStar .orelse );
14241424 break ;
1425+ case Match_kind :
1426+ for (j = 0 ; j < asdl_seq_LEN (st -> v .Match .cases ); j ++ ) {
1427+ match_case_ty match_case = (match_case_ty )asdl_seq_GET (
1428+ st -> v .Match .cases , j );
1429+ if (find_ann (match_case -> body )) {
1430+ return true;
1431+ }
1432+ }
1433+ break ;
14251434 default :
14261435 res = false;
1436+ break ;
14271437 }
14281438 if (res ) {
14291439 break ;
You can’t perform that action at this time.
0 commit comments