Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit69d1245

Browse files
gh-105164: Detect annotations inside match blocks (#105177)
1 parent0689340 commit69d1245

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

‎Lib/test/test_type_annotations.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
importtextwrap
12
importunittest
3+
fromtest.supportimportrun_code
24

35
classTypeAnnotationTests(unittest.TestCase):
46

@@ -101,3 +103,112 @@ class D(metaclass=C):
101103
withself.assertRaises(AttributeError):
102104
delD.__annotations__
103105
self.assertEqual(D.__annotations__, {})
106+
107+
108+
classTestSetupAnnotations(unittest.TestCase):
109+
defcheck(self,code:str):
110+
code=textwrap.dedent(code)
111+
forscopein ("module","class"):
112+
withself.subTest(scope=scope):
113+
ifscope=="class":
114+
code=f"class C:\n{textwrap.indent(code,' ')}"
115+
ns=run_code(code)
116+
ifscope=="class":
117+
annotations=ns["C"].__annotations__
118+
else:
119+
annotations=ns["__annotations__"]
120+
self.assertEqual(annotations, {"x":int})
121+
122+
deftest_top_level(self):
123+
self.check("x: int = 1")
124+
125+
deftest_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+
deftest_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+
deftest_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+
deftest_match(self):
210+
self.check("""
211+
match 0:
212+
case 0:
213+
x: int = 1
214+
""")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
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.

‎Python/compile.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,18 @@ find_ann(asdl_stmt_seq *stmts)
13981398
find_ann(st->v.TryStar.finalbody)||
13991399
find_ann(st->v.TryStar.orelse);
14001400
break;
1401+
caseMatch_kind:
1402+
for (j=0;j<asdl_seq_LEN(st->v.Match.cases);j++) {
1403+
match_case_tymatch_case= (match_case_ty)asdl_seq_GET(
1404+
st->v.Match.cases,j);
1405+
if (find_ann(match_case->body)) {
1406+
return true;
1407+
}
1408+
}
1409+
break;
14011410
default:
14021411
res= false;
1412+
break;
14031413
}
14041414
if (res) {
14051415
break;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp