Commit cb0728c
authored
Add basic end-to-end support for Literal types (#5947)
This pull request primarily adds support for string, int, bool, and
None Literal types in the parsing and semantic analysis layer. It also
adds a small smidge of logic to the subtyping and overlapping types
parts of the typechecking layer -- just enough so we can start using
Literal types end-to-end and write some tests. It also adds a
"skip path normalization" option to our test cases.
Basically, my previous diff made a bunch of gross changes in a
horizontal way to lay out a foundation; this diff makes a bunch of
gross changes in a vertical way to create a proof-of-concept of the
proof-of-concept.
I'll probably need to submit a few follow-up PRs cleaning up some
stuff in the semantic analysis layer, but my hope is to switch focus
on fleshing out the type checking layer shortly after this PR lands.
Specific changes made:
1. I added a new 'RawLiteralType' synthetic type meant to represent any
literal expression that appears in the earliest stages of semantic
analysis. If these 'RawLiteralTypes' appear in a "Literal[...]" context,
they transformed into regular 'LiteralTypes' during phase 2 of semantic
analysis (turning UnboundTypes into actual types). If they appear
outside of the correct context, we report an error instead.
I also added a string field to UnboundType to keep track of similar
information. Basically, if we have `Foo["bar"]`, we don't actually
know immediately whether or not that "bar" is meant to be string
literal vs a forward reference. (Foo could be a regular class or
an alias for 'Literal').
Note: I wanted to avoid having to introduce yet another type, so
looked into perhaps attaching even more metadata to UnboundType
or perhaps to LiteralType. Both of those approaches ended up looking
pretty messy though, so I went with this.
2. As a consequence, some of the syntax checking logic had to move
from the parsing layer to the semantic analysis layer, and some of
the existing error messages changed slightly.
3. The PEP, at some point, provisionally rejected having Literals
contain other Literal types. For example, something like this:
RegularIds = Literal[1, 2, 3, 4]
AdminIds = Literal[100, 101, 102]
AllIds = Literal[RegularIds, AdminIds, 30, 32]
This was mainly because I thought this would be challenging to
implement, but it turned out to be easier then expected -- it
happened almost by accident while I was implementing support
for 'Literal[1, 2, 3]'.
I can excise this part out if we think supporting this kind
of thing is a fundamentally bad idea.
4. I also penciled in some minimal code to the subtyping and
overlapping types logic.
This diff also tweaks some of our test-case logic: we can now specify
we want to skip path normalization on certain test cases.
Specifically, the problem was that mypy attempts to normalize
paths by replacing all instances of '\' with '/' so that the
output when running the tests on Windows matches the specified
errors. This is what we want to most of the time: except when we want
to print out Literal types containing strings with slashes.
I thought about maybe changing the output of mypy in general so
it always uses '/' for paths in error outputs, even on Windows:
this would mean we would no longer have to do path normalization.
However, I wasn't convinced this was the right thing to do: using
'\' on Windows technically *is* the right thing to do, and I didn't
want to complicate the codebase by forcing us to keep track of when
to use os.sep vs '/'.
I don't think I'll add too many of these test cases, so I decided
to just go with a localized solution instead of changing mypy's
error output as a whole.1 parent 4f2a88d commit cb0728c
29 files changed
Lines changed: 1100 additions & 81 deletions
File tree
- mypy
- server
- test
- test-data/unit
- lib-stub
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
41 | 47 | | |
42 | 48 | | |
43 | 49 | | |
| |||
108 | 114 | | |
109 | 115 | | |
110 | 116 | | |
111 | | - | |
112 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
113 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
114 | 130 | | |
115 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
116 | 137 | | |
117 | 138 | | |
118 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
56 | 59 | | |
57 | 60 | | |
58 | 61 | | |
| |||
1138 | 1141 | | |
1139 | 1142 | | |
1140 | 1143 | | |
1141 | | - | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
1142 | 1174 | | |
1143 | 1175 | | |
1144 | 1176 | | |
1145 | | - | |
1146 | | - | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
1147 | 1184 | | |
1148 | 1185 | | |
1149 | 1186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
238 | 245 | | |
239 | 246 | | |
240 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
| 300 | + | |
| 301 | + | |
300 | 302 | | |
301 | 303 | | |
302 | 304 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
113 | | - | |
| 114 | + | |
114 | 115 | | |
| 116 | + | |
115 | 117 | | |
116 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
117 | 125 | | |
118 | 126 | | |
119 | 127 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
704 | 704 | | |
705 | 705 | | |
706 | 706 | | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
707 | 714 | | |
708 | 715 | | |
709 | 716 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
391 | 392 | | |
392 | 393 | | |
393 | 394 | | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
394 | 398 | | |
395 | 399 | | |
396 | 400 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | | - | |
331 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
332 | 335 | | |
333 | 336 | | |
334 | 337 | | |
| |||
1172 | 1175 | | |
1173 | 1176 | | |
1174 | 1177 | | |
1175 | | - | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
1176 | 1182 | | |
1177 | 1183 | | |
1178 | 1184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
101 | 105 | | |
102 | | - | |
| 106 | + | |
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
| |||
147 | 151 | | |
148 | 152 | | |
149 | 153 | | |
| 154 | + | |
150 | 155 | | |
151 | 156 | | |
152 | 157 | | |
| |||
168 | 173 | | |
169 | 174 | | |
170 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
171 | 180 | | |
172 | 181 | | |
173 | 182 | | |
| |||
0 commit comments