|
1 | | -# mypy: ignore-errors |
2 | 1 | """ |
3 | 2 | TypeScript-style utility type operators: Pick, Omit, Partial. |
4 | 3 |
|
5 | 4 | See https://www.typescriptlang.org/docs/handbook/utility-types.html |
6 | 5 | """ |
7 | 6 |
|
8 | 7 | import textwrap |
9 | | -from typing import Literal, Never, NotRequired, Required, TypedDict, Union |
| 8 | +from typing import ( |
| 9 | + assert_type, |
| 10 | + Literal, |
| 11 | + Never, |
| 12 | + NotRequired, |
| 13 | + Required, |
| 14 | + TypedDict, |
| 15 | + Union, |
| 16 | +) |
10 | 17 |
|
11 | 18 | import typemap_extensions as typing |
12 | 19 | from typemap.type_eval import eval_typing |
@@ -105,6 +112,60 @@ class TodoTD(TypedDict): |
105 | 112 | # End PEP section: Utility types |
106 | 113 |
|
107 | 114 |
|
| 115 | +def _check_exclude(x0: Exclude[str | int | bool, int]) -> None: |
| 116 | + assert_type(x0, str) |
| 117 | + |
| 118 | + |
| 119 | +def _check_extract(x1: Extract[str | int | bool, int]) -> None: |
| 120 | + assert_type(x1, int | bool) |
| 121 | + |
| 122 | + |
| 123 | +def _check_keyof(x2: KeyOf[Todo]) -> None: |
| 124 | + assert_type( |
| 125 | + x2, Literal["title"] | Literal["description"] | Literal["completed"] |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def _check_pick( |
| 130 | + x3: Pick[Todo, Literal["title"] | Literal["completed"]], |
| 131 | +) -> None: |
| 132 | + assert_type(x3.title, str) |
| 133 | + assert_type(x3.completed, bool) |
| 134 | + assert_type( |
| 135 | + x3, |
| 136 | + typing.NewProtocol[ |
| 137 | + typing.Member[Literal["title"], str], |
| 138 | + typing.Member[Literal["completed"], bool], |
| 139 | + ], |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def _check_omit(x4: Omit[Todo, Literal["description"]]) -> None: |
| 144 | + assert_type(x4.title, str) |
| 145 | + assert_type(x4.completed, bool) |
| 146 | + assert_type( |
| 147 | + x4, |
| 148 | + typing.NewProtocol[ |
| 149 | + typing.Member[Literal["title"], str], |
| 150 | + typing.Member[Literal["completed"], bool], |
| 151 | + ], |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +def _check_partial(x5: Partial[Todo]) -> None: |
| 156 | + assert_type(x5.title, str | None) |
| 157 | + assert_type(x5.description, str | None) |
| 158 | + assert_type(x5.completed, bool | None) |
| 159 | + assert_type( |
| 160 | + x5, |
| 161 | + typing.NewProtocol[ |
| 162 | + typing.Member[Literal["title"], str | None], |
| 163 | + typing.Member[Literal["description"], str | None], |
| 164 | + typing.Member[Literal["completed"], bool | None], |
| 165 | + ], |
| 166 | + ) |
| 167 | + |
| 168 | + |
108 | 169 | def test_pick(): |
109 | 170 | tgt = eval_typing(Pick[Todo, Literal["title"] | Literal["completed"]]) |
110 | 171 | fmt = format_helper.format_class(tgt) |
|
0 commit comments