|
1 | 1 | # String annotations
|
2 | 2 |
|
| 3 | +## Simple |
| 4 | + |
3 | 5 | ```py
|
4 | 6 | def f() -> "int":
|
5 | 7 | return 1
|
6 | 8 |
|
7 |
| -# TODO: We do not support string annotations, but we should not panic if we encounter them |
8 |
| -reveal_type(f()) # revealed: @Todo |
| 9 | +reveal_type(f()) # revealed: int |
| 10 | +``` |
| 11 | + |
| 12 | +## Nested |
| 13 | + |
| 14 | +```py |
| 15 | +def f() -> "'int'": |
| 16 | + return 1 |
| 17 | + |
| 18 | +reveal_type(f()) # revealed: int |
| 19 | +``` |
| 20 | + |
| 21 | +## Type expression |
| 22 | + |
| 23 | +```py |
| 24 | +def f1() -> "int | str": |
| 25 | + return 1 |
| 26 | + |
| 27 | +def f2() -> "tuple[int, str]": |
| 28 | + return 1 |
| 29 | + |
| 30 | +reveal_type(f1()) # revealed: int | str |
| 31 | +reveal_type(f2()) # revealed: tuple[int, str] |
| 32 | +``` |
| 33 | + |
| 34 | +## Partial |
| 35 | + |
| 36 | +```py |
| 37 | +def f() -> tuple[int, "str"]: |
| 38 | + return 1 |
| 39 | + |
| 40 | +reveal_type(f()) # revealed: tuple[int, str] |
| 41 | +``` |
| 42 | + |
| 43 | +## Deferred |
| 44 | + |
| 45 | +```py |
| 46 | +def f() -> "Foo": |
| 47 | + return Foo() |
| 48 | + |
| 49 | +class Foo: |
| 50 | + pass |
| 51 | + |
| 52 | +reveal_type(f()) # revealed: Foo |
| 53 | +``` |
| 54 | + |
| 55 | +## Deferred (undefined) |
| 56 | + |
| 57 | +```py |
| 58 | +# error: [unresolved-reference] |
| 59 | +def f() -> "Foo": |
| 60 | + pass |
| 61 | + |
| 62 | +reveal_type(f()) # revealed: Unknown |
| 63 | +``` |
| 64 | + |
| 65 | +## Partial deferred |
| 66 | + |
| 67 | +```py |
| 68 | +def f() -> int | "Foo": |
| 69 | + return 1 |
| 70 | + |
| 71 | +class Foo: |
| 72 | + pass |
| 73 | + |
| 74 | +reveal_type(f()) # revealed: int | Foo |
9 | 75 | ```
|
| 76 | + |
| 77 | +## `typing.Literal` |
| 78 | + |
| 79 | +```py |
| 80 | +from typing import Literal |
| 81 | + |
| 82 | +def f1() -> Literal["Foo", "Bar"]: |
| 83 | + return "Foo" |
| 84 | + |
| 85 | +def f2() -> 'Literal["Foo", "Bar"]': |
| 86 | + return "Foo" |
| 87 | + |
| 88 | +class Foo: |
| 89 | + pass |
| 90 | + |
| 91 | +reveal_type(f1()) # revealed: Literal["Foo", "Bar"] |
| 92 | +reveal_type(f2()) # revealed: Literal["Foo", "Bar"] |
| 93 | +``` |
| 94 | + |
| 95 | +## Various string kinds |
| 96 | + |
| 97 | +```py |
| 98 | +# error: [annotation-raw-string] "Type expressions cannot use raw string literal" |
| 99 | +def f1() -> r"int": |
| 100 | + return 1 |
| 101 | + |
| 102 | +# error: [annotation-f-string] "Type expressions cannot use f-strings" |
| 103 | +def f2() -> f"int": |
| 104 | + return 1 |
| 105 | + |
| 106 | +# error: [annotation-byte-string] "Type expressions cannot use bytes literal" |
| 107 | +def f3() -> b"int": |
| 108 | + return 1 |
| 109 | + |
| 110 | +def f4() -> "int": |
| 111 | + return 1 |
| 112 | + |
| 113 | +# error: [annotation-implicit-concat] "Type expressions cannot span multiple string literals" |
| 114 | +def f5() -> "in" "t": |
| 115 | + return 1 |
| 116 | + |
| 117 | +# error: [annotation-escape-character] "Type expressions cannot contain escape characters" |
| 118 | +def f6() -> "\N{LATIN SMALL LETTER I}nt": |
| 119 | + return 1 |
| 120 | + |
| 121 | +# error: [annotation-escape-character] "Type expressions cannot contain escape characters" |
| 122 | +def f7() -> "\x69nt": |
| 123 | + return 1 |
| 124 | + |
| 125 | +def f8() -> """int""": |
| 126 | + return 1 |
| 127 | + |
| 128 | +# error: [annotation-byte-string] "Type expressions cannot use bytes literal" |
| 129 | +def f9() -> "b'int'": |
| 130 | + return 1 |
| 131 | + |
| 132 | +reveal_type(f1()) # revealed: Unknown |
| 133 | +reveal_type(f2()) # revealed: Unknown |
| 134 | +reveal_type(f3()) # revealed: Unknown |
| 135 | +reveal_type(f4()) # revealed: int |
| 136 | +reveal_type(f5()) # revealed: Unknown |
| 137 | +reveal_type(f6()) # revealed: Unknown |
| 138 | +reveal_type(f7()) # revealed: Unknown |
| 139 | +reveal_type(f8()) # revealed: int |
| 140 | +reveal_type(f9()) # revealed: Unknown |
| 141 | +``` |
| 142 | + |
| 143 | +## Various string kinds in `typing.Literal` |
| 144 | + |
| 145 | +```py |
| 146 | +from typing import Literal |
| 147 | + |
| 148 | +def f() -> Literal["a", r"b", b"c", "d" "e", "\N{LATIN SMALL LETTER F}", "\x67", """h"""]: |
| 149 | + return "normal" |
| 150 | + |
| 151 | +reveal_type(f()) # revealed: Literal["a", "b", "de", "f", "g", "h"] | Literal[b"c"] |
| 152 | +``` |
| 153 | + |
| 154 | +## Class variables |
| 155 | + |
| 156 | +```py |
| 157 | +MyType = int |
| 158 | + |
| 159 | +class Aliases: |
| 160 | + MyType = str |
| 161 | + |
| 162 | + forward: "MyType" |
| 163 | + not_forward: MyType |
| 164 | + |
| 165 | +reveal_type(Aliases.forward) # revealed: str |
| 166 | +reveal_type(Aliases.not_forward) # revealed: str |
| 167 | +``` |
| 168 | + |
| 169 | +## Annotated assignment |
| 170 | + |
| 171 | +```py |
| 172 | +a: "int" = 1 |
| 173 | +b: "'int'" = 1 |
| 174 | +c: "Foo" |
| 175 | +# error: [invalid-assignment] "Object of type `Literal[1]` is not assignable to `Foo`" |
| 176 | +d: "Foo" = 1 |
| 177 | + |
| 178 | +class Foo: |
| 179 | + pass |
| 180 | + |
| 181 | +c = Foo() |
| 182 | + |
| 183 | +reveal_type(a) # revealed: Literal[1] |
| 184 | +reveal_type(b) # revealed: Literal[1] |
| 185 | +reveal_type(c) # revealed: Foo |
| 186 | +reveal_type(d) # revealed: Foo |
| 187 | +``` |
| 188 | + |
| 189 | +## Parameter |
| 190 | + |
| 191 | +TODO: Add tests once parameter inference is supported |
0 commit comments