Commit d24d086
Fix sequence pattern matching type narrowing (#2049)
Summary:
Adds proper type narrowing for sequence patterns in match/case statements. Previously, sequence patterns like [*values] did not narrow union types correctly, and captured variables were not properly typed.
This PR:
- Adds Sequence type to the stdlib for type checking
- Introduces IsSequence/IsNotSequence narrowing operations
- Emits IsSequence narrowing for sequence patterns to properly filter union types
- Correctly types captured variables (e.g., [*values] gives values: list[T] when matching Sequence[T])
- Excludes str, bytes, and bytearray from sequence pattern matching per PEP 634
Fixes #1806.
Pull Request resolved: #2049
Test Plan:
Added 9 new test cases covering:
- Basic star capture patterns ([*values])
- Union type narrowing (int | Sequence[int])
- Fixed-length patterns ([a, b])
- Mixed patterns ([first, *middle, last])
- String exclusion per PEP 634
- List and tuple patterns
Reviewed By: yangdanny97
Differential Revision: D90423128
Pulled By: stroxler
fbshipit-source-id: 359a6441285870de71157476e6562eb50dcd39dc1 parent e34b42a commit d24d086
File tree
6 files changed
+260
-31
lines changed- crates/pyrefly_types/src
- pyrefly/lib
- alt
- binding
- test
6 files changed
+260
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
202 | 203 | | |
203 | 204 | | |
204 | 205 | | |
| 206 | + | |
205 | 207 | | |
206 | 208 | | |
207 | 209 | | |
| |||
460 | 462 | | |
461 | 463 | | |
462 | 464 | | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
463 | 469 | | |
464 | 470 | | |
465 | 471 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
624 | 624 | | |
625 | 625 | | |
626 | 626 | | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
627 | 651 | | |
628 | 652 | | |
629 | 653 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
164 | 199 | | |
165 | 200 | | |
166 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
89 | 93 | | |
90 | 94 | | |
91 | 95 | | |
| |||
164 | 168 | | |
165 | 169 | | |
166 | 170 | | |
| 171 | + | |
| 172 | + | |
167 | 173 | | |
168 | 174 | | |
169 | 175 | | |
| |||
233 | 239 | | |
234 | 240 | | |
235 | 241 | | |
| 242 | + | |
| 243 | + | |
236 | 244 | | |
237 | 245 | | |
238 | 246 | | |
| |||
419 | 427 | | |
420 | 428 | | |
421 | 429 | | |
422 | | - | |
| 430 | + | |
| 431 | + | |
423 | 432 | | |
424 | 433 | | |
425 | 434 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
123 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
127 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
128 | 137 | | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
132 | | - | |
| 141 | + | |
133 | 142 | | |
134 | 143 | | |
135 | 144 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
141 | 154 | | |
142 | 155 | | |
143 | 156 | | |
| |||
240 | 253 | | |
241 | 254 | | |
242 | 255 | | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
243 | 274 | | |
244 | 275 | | |
245 | 276 | | |
| |||
249 | 280 | | |
250 | 281 | | |
251 | 282 | | |
252 | | - | |
253 | | - | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
254 | 287 | | |
255 | 288 | | |
256 | 289 | | |
| |||
263 | 296 | | |
264 | 297 | | |
265 | 298 | | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | 299 | | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
| 300 | + | |
282 | 301 | | |
283 | 302 | | |
284 | 303 | | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
290 | 312 | | |
291 | 313 | | |
292 | 314 | | |
| |||
0 commit comments