Skip to content

Commit

Permalink
pythongh-105164: Detect annotations inside match blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Jun 1, 2023
1 parent df396b5 commit f7ed5ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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

0 comments on commit f7ed5ce

Please sign in to comment.