Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-105164: Detect annotations inside match blocks
  • Loading branch information
JelleZijlstra committed Jun 1, 2023
commit f7ed5ce0244eae4e0b1887e4cbaff4960edb0fca
15 changes: 15 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from test.support import run_code

class TypeAnnotationTests(unittest.TestCase):

Expand Down Expand Up @@ -101,3 +102,17 @@ class D(metaclass=C):
with self.assertRaises(AttributeError):
del D.__annotations__
self.assertEqual(D.__annotations__, {})

def test_module_level(self):
ns = run_code("x: int = 1")
self.assertEqual(ns["__annotations__"], {"x": int})

ns = run_code("if True:\n x: int = 1")
self.assertEqual(ns["__annotations__"], {"x": int})

ns = run_code("""
match 0:
case 0:
x: int = 1
""")
self.assertEqual(ns["__annotations__"], {"x": int})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure annotations are set up correctly if the only annotation in a block is
within a :keyword:`match` block. Patch by Jelle Zijlstra.
10 changes: 10 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,18 @@ find_ann(asdl_stmt_seq *stmts)
find_ann(st->v.TryStar.finalbody) ||
find_ann(st->v.TryStar.orelse);
break;
case Match_kind:
for (j = 0; j < asdl_seq_LEN(st->v.Match.cases); j++) {
match_case_ty match_case = (match_case_ty)asdl_seq_GET(
st->v.Match.cases, j);
if (find_ann(match_case->body)) {
return true;
}
}
break;
default:
res = false;
break;
}
if (res) {
break;
Expand Down