Commit fcee66d
authored
Add support for byte and unicode Literal strings (#6087)
This pull request adds support for byte and unicode Literal strings.
I left in some comments explaining some nuances of the implementation;
here are a few additional meta-notes:
1. I reworded several of the comments suggesting that the way we
represent bytes as a string is a "hack" or that we should eventually
switch to representing bytes as literally bytes.
I started with that approach but ultimately rejected it: I ended
up having to constantly serialize/deserialize between bytes and
strings, which I felt complicated the code.
As a result, I decided that the solution we had previously is in
fact, from a high-level perspective, the best possible approach.
(The actual code for translating the output of `typed_ast` into a
human-readable string *is* admittedly a bit hacky though.)
In any case, the phrase "how mypy currently parses the contents of bytes
literals" is severely out-of-date anyways. That comment was added
about 3 years ago, when we were adding the fast parser for the first
time and running it concurrently with the actual parser.
2. I removed the `is_stub` field from `fastparse2.ASTConverter`: it turned
out we were just never using that field.
3. One complication I ran into was figuring out how to handle forward
references to literal strings. For example, suppose we have the type
`List["Literal['foo']"]`. Do we treat this as being equivalent to
`List[Literal[u'foo']]` or `List[Literal[b'foo']]`?
If this is a Python 3 file or a Python 2 file with
`unicode_literals`, we'd want to pick the former. If this is a
standard Python 2 file, we'd want to pick the latter.
In order to make this happen, I decided to use a heuristic where the
type of the "outer" string decides the type of the "inner" string.
For example:
- In Python 3, `"Literal['foo']"` is a unicode string. So,
the inner `Literal['foo']` will be treated as the same as
`Literal[u'foo']`.
- The same thing happens when using Python 2 with
`unicode_literals`.
- In Python 3, it is illegal to use a byte string as a forward
reference. So, types like `List[b"Literal['foo']"]` are already
illegal.
- In standard Python 2, `"Literal['foo']"` is a byte string. So the
inner `Literal['foo']` will be treated as the same as
`Literal[u'foo']`.
4. I will add tests validating that all of this stuff works as expected
with incremental and fine-grained mode in a separate diff --
probably after fixing and landing #6075,
which I intend to use as a baseline foundation.1 parent 9a3fa64 commit fcee66d
File tree
10 files changed
+668
-68
lines changed- mypy
- test-data/unit
10 files changed
+668
-68
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1784 | 1784 | | |
1785 | 1785 | | |
1786 | 1786 | | |
1787 | | - | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
1788 | 1791 | | |
1789 | 1792 | | |
1790 | 1793 | | |
1791 | | - | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
1792 | 1798 | | |
1793 | 1799 | | |
1794 | 1800 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
116 | 123 | | |
117 | 124 | | |
118 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
143 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
144 | 149 | | |
145 | 150 | | |
146 | 151 | | |
| |||
151 | 156 | | |
152 | 157 | | |
153 | 158 | | |
154 | | - | |
| 159 | + | |
| 160 | + | |
155 | 161 | | |
156 | 162 | | |
157 | | - | |
158 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
159 | 167 | | |
160 | 168 | | |
161 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
162 | 178 | | |
163 | 179 | | |
164 | | - | |
| 180 | + | |
| 181 | + | |
165 | 182 | | |
166 | 183 | | |
| 184 | + | |
167 | 185 | | |
168 | 186 | | |
169 | | - | |
170 | | - | |
171 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
172 | 192 | | |
173 | 193 | | |
174 | 194 | | |
| |||
966 | 986 | | |
967 | 987 | | |
968 | 988 | | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
| 989 | + | |
973 | 990 | | |
974 | 991 | | |
975 | 992 | | |
| |||
1042 | 1059 | | |
1043 | 1060 | | |
1044 | 1061 | | |
1045 | | - | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
1046 | 1067 | | |
1047 | 1068 | | |
1048 | 1069 | | |
| 1070 | + | |
1049 | 1071 | | |
1050 | 1072 | | |
1051 | 1073 | | |
| |||
1090 | 1112 | | |
1091 | 1113 | | |
1092 | 1114 | | |
1093 | | - | |
1094 | | - | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
1095 | 1120 | | |
1096 | 1121 | | |
1097 | 1122 | | |
| |||
1190 | 1215 | | |
1191 | 1216 | | |
1192 | 1217 | | |
1193 | | - | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
1194 | 1234 | | |
1195 | 1235 | | |
1196 | 1236 | | |
| |||
1246 | 1286 | | |
1247 | 1287 | | |
1248 | 1288 | | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
117 | 116 | | |
118 | 117 | | |
119 | 118 | | |
| |||
141 | 140 | | |
142 | 141 | | |
143 | 142 | | |
144 | | - | |
145 | 143 | | |
146 | 144 | | |
147 | 145 | | |
148 | 146 | | |
149 | 147 | | |
150 | | - | |
151 | 148 | | |
152 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
153 | 169 | | |
154 | 170 | | |
155 | 171 | | |
| |||
306 | 322 | | |
307 | 323 | | |
308 | 324 | | |
309 | | - | |
| 325 | + | |
| 326 | + | |
310 | 327 | | |
311 | 328 | | |
312 | 329 | | |
| |||
413 | 430 | | |
414 | 431 | | |
415 | 432 | | |
416 | | - | |
| 433 | + | |
| 434 | + | |
417 | 435 | | |
418 | 436 | | |
419 | 437 | | |
| |||
532 | 550 | | |
533 | 551 | | |
534 | 552 | | |
535 | | - | |
| 553 | + | |
| 554 | + | |
536 | 555 | | |
537 | 556 | | |
538 | 557 | | |
| |||
549 | 568 | | |
550 | 569 | | |
551 | 570 | | |
552 | | - | |
| 571 | + | |
| 572 | + | |
553 | 573 | | |
554 | 574 | | |
555 | 575 | | |
| |||
576 | 596 | | |
577 | 597 | | |
578 | 598 | | |
579 | | - | |
| 599 | + | |
| 600 | + | |
580 | 601 | | |
581 | 602 | | |
582 | 603 | | |
| |||
680 | 701 | | |
681 | 702 | | |
682 | 703 | | |
683 | | - | |
684 | | - | |
685 | | - | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
686 | 710 | | |
687 | 711 | | |
688 | 712 | | |
| |||
900 | 924 | | |
901 | 925 | | |
902 | 926 | | |
903 | | - | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
909 | 935 | | |
910 | | - | |
911 | | - | |
912 | | - | |
913 | | - | |
914 | | - | |
| 936 | + | |
| 937 | + | |
915 | 938 | | |
916 | 939 | | |
917 | 940 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
0 commit comments