| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3e7ddc2 commit 2aaae40
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,6 @@ | |||
| 1 | + import textwrap | ||
| 1 | 2 | import unittest | |
| 3 | + from test.support import run_code | ||
| 2 | 4 | ||
| 3 | 5 | class TypeAnnotationTests(unittest.TestCase): | |
| 4 | 6 | ||
@@ -101,3 +103,112 @@ class D(metaclass=C): | |||
| 101 | 103 | with self.assertRaises(AttributeError): | |
| 102 | 104 | del D.__annotations__ | |
| 103 | 105 | 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 | |
|---|---|---|---|
@@ -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. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1422,8 +1422,18 @@ find_ann(asdl_stmt_seq *stmts) | |||
| 1422 | 1422 | find_ann(st->v.TryStar.finalbody) || | |
| 1423 | 1423 | find_ann(st->v.TryStar.orelse); | |
| 1424 | 1424 | 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; | ||
| 1425 | 1434 | default: | |
| 1426 | 1435 | res = false; | |
| 1436 | + break; | ||
| 1427 | 1437 | } | |
| 1428 | 1438 | if (res) { | |
| 1429 | 1439 | break; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments