| 1 |
[case testPEP695Basics] |
| 2 |
from enum import Enum |
| 3 |
from typing import Any, Literal, TypeAliasType, cast |
| 4 |
|
| 5 |
from testutil import assertRaises |
| 6 |
|
| 7 |
def id[T](x: T) -> T: |
| 8 |
return x |
| 9 |
|
| 10 |
def test_call_generic_function() -> None: |
| 11 |
assert id(2) == 2 |
| 12 |
assert id('x') == 'x' |
| 13 |
|
| 14 |
class C[T]: |
| 15 |
x: T |
| 16 |
|
| 17 |
def __init__(self, x: T) -> None: |
| 18 |
self.x = x |
| 19 |
|
| 20 |
class D[T, S]: |
| 21 |
x: T |
| 22 |
y: S |
| 23 |
|
| 24 |
def __init__(self, x: T, y: S) -> None: |
| 25 |
self.x = x |
| 26 |
self.y = y |
| 27 |
|
| 28 |
def set(self, x: object, y: object) -> None: |
| 29 |
self.x = cast(T, x) |
| 30 |
self.y = cast(S, y) |
| 31 |
|
| 32 |
def test_generic_class() -> None: |
| 33 |
c = C(5) |
| 34 |
assert c.x == 5 |
| 35 |
c2 = C[str]('x') |
| 36 |
assert c2.x == 'x' |
| 37 |
d = D[str, int]('a', 5) |
| 38 |
assert d.x == 'a' |
| 39 |
assert d.y == 5 |
| 40 |
d.set('b', 6) |
| 41 |
assert d.x == 'b' |
| 42 |
assert d.y == 6 |
| 43 |
|
| 44 |
def test_generic_class_via_any() -> None: |
| 45 |
c_any: Any = C |
| 46 |
c = c_any(2) |
| 47 |
assert c.x == 2 |
| 48 |
c2 = c_any[str]('y') |
| 49 |
assert c2.x == 'y' |
| 50 |
assert str(c_any[str]) == 'native.C[str]' |
| 51 |
|
| 52 |
d_any: Any = D |
| 53 |
d = d_any(1, 'x') |
| 54 |
assert d.x == 1 |
| 55 |
assert d.y == 'x' |
| 56 |
d2 = d_any[int, str](2, 'y') |
| 57 |
assert d2.x == 2 |
| 58 |
assert d2.y == 'y' |
| 59 |
|
| 60 |
with assertRaises(TypeError): |
| 61 |
c_any[int, str] |
| 62 |
with assertRaises(TypeError): |
| 63 |
d_any[int] |
| 64 |
|
| 65 |
class E[*Ts]: pass |
| 66 |
|
| 67 |
def test_type_var_tuple() -> None: |
| 68 |
e: E[int, str] = E() |
| 69 |
e_any: Any = E |
| 70 |
assert isinstance(e_any(), E) |
| 71 |
assert isinstance(e_any[int](), E) |
| 72 |
assert isinstance(e_any[int, str](), E) |
| 73 |
|
| 74 |
class F[**P]: pass |
| 75 |
|
| 76 |
def test_param_spec() -> None: |
| 77 |
f: F[[int, str]] = F() |
| 78 |
f_any: Any = F |
| 79 |
assert isinstance(f_any(), F) |
| 80 |
assert isinstance(f_any[[int, str]](), F) |
| 81 |
|
| 82 |
class SubC[S](C[S]): |
| 83 |
def __init__(self, x: S) -> None: |
| 84 |
super().__init__(x) |
| 85 |
|
| 86 |
def test_generic_subclass() -> None: |
| 87 |
s = SubC(1) |
| 88 |
assert s.x == 1 |
| 89 |
s2 = SubC[str]('y') |
| 90 |
assert s2.x == 'y' |
| 91 |
sub_any: Any = SubC |
| 92 |
assert sub_any(1).x == 1 |
| 93 |
assert sub_any[str]('x').x == 'x' |
| 94 |
assert isinstance(s, SubC) |
| 95 |
assert isinstance(s, C) |
| 96 |
|
| 97 |
class SubD[ |
| 98 |
T, # Put everything on separate lines |
| 99 |
S]( |
| 100 |
D[T, |
| 101 |
S]): pass |
| 102 |
|
| 103 |
def test_generic_subclass_two_params() -> None: |
| 104 |
s = SubD(3, 'y') |
| 105 |
assert s.x == 3 |
| 106 |
assert s.y == 'y' |
| 107 |
s2 = SubD[str, int]('z', 4) |
| 108 |
assert s2.x == 'z' |
| 109 |
assert s2.y == 4 |
| 110 |
sub_any: Any = SubD |
| 111 |
assert sub_any(3, 'y').y == 'y' |
| 112 |
assert sub_any[int, str](3, 'y').y == 'y' |
| 113 |
assert isinstance(s, SubD) |
| 114 |
assert isinstance(s, D) |
| 115 |
|
| 116 |
class SubE[*Ts](E[*Ts]): pass |
| 117 |
|
| 118 |
def test_type_var_tuple_subclass() -> None: |
| 119 |
sub_any: Any = SubE |
| 120 |
assert isinstance(sub_any(), SubE) |
| 121 |
assert isinstance(sub_any(), E) |
| 122 |
assert isinstance(sub_any[int](), SubE) |
| 123 |
assert isinstance(sub_any[int, str](), SubE) |
| 124 |
|
| 125 |
|
| 126 |
class SubF[**P](F[P]): pass |
| 127 |
|
| 128 |
def test_param_spec_subclass() -> None: |
| 129 |
sub_any: Any = SubF |
| 130 |
assert isinstance(sub_any(), SubF) |
| 131 |
assert isinstance(sub_any(), F) |
| 132 |
assert isinstance(sub_any[[int]](), SubF) |
| 133 |
assert isinstance(sub_any[[int, str]](), SubF) |
| 134 |
|
| 135 |
# We test that upper bounds and restricted values can be used, but not that |
| 136 |
# they are introspectable |
| 137 |
|
| 138 |
def bound[T: C](x: T) -> T: |
| 139 |
return x |
| 140 |
|
| 141 |
def test_function_with_upper_bound() -> None: |
| 142 |
c = C(1) |
| 143 |
assert bound(c) is c |
| 144 |
|
| 145 |
def restriction[T: (int, str)](x: T) -> T: |
| 146 |
return x |
| 147 |
|
| 148 |
def test_function_with_value_restriction() -> None: |
| 149 |
assert restriction(1) == 1 |
| 150 |
assert restriction('x') == 'x' |
| 151 |
|
| 152 |
class Bound[T: C]: |
| 153 |
def __init__(self, x: T) -> None: |
| 154 |
self.x = x |
| 155 |
|
| 156 |
def test_class_with_upper_bound() -> None: |
| 157 |
c = C(1) |
| 158 |
b = Bound(c) |
| 159 |
assert b.x is c |
| 160 |
b2 = Bound[C](c) |
| 161 |
assert b2.x is c |
| 162 |
|
| 163 |
class Restriction[T: (int, str)]: |
| 164 |
def __init__(self, x: T) -> None: |
| 165 |
self.x = x |
| 166 |
|
| 167 |
def test_class_with_value_restriction() -> None: |
| 168 |
r = Restriction(1) |
| 169 |
assert r.x == 1 |
| 170 |
r2 = Restriction[str]('a') |
| 171 |
assert r2.x == 'a' |
| 172 |
|
| 173 |
type A = int |
| 174 |
|
| 175 |
def test_simple_type_alias() -> None: |
| 176 |
assert isinstance(A, TypeAliasType) |
| 177 |
assert getattr(A, "__value__") is int |
| 178 |
assert str(A) == "A" |
| 179 |
|
| 180 |
type B = Fwd[int] |
| 181 |
Fwd = list |
| 182 |
|
| 183 |
def test_forward_reference_in_alias() -> None: |
| 184 |
assert isinstance(B, TypeAliasType) |
| 185 |
assert getattr(B, "__value__") == list[int] |
| 186 |
|
| 187 |
type R = int | list[R] |
| 188 |
|
| 189 |
def test_recursive_type_alias() -> None: |
| 190 |
assert isinstance(R, TypeAliasType) |
| 191 |
assert getattr(R, "__value__") == (int | list[R]) |
| 192 |
|
| 193 |
class SomeEnum(Enum): |
| 194 |
AVALUE = "a" |
| 195 |
|
| 196 |
type EnumLiteralAlias1 = Literal[SomeEnum.AVALUE] |
| 197 |
type EnumLiteralAlias2 = Literal[SomeEnum.AVALUE] | None |
| 198 |
EnumLiteralAlias3 = Literal[SomeEnum.AVALUE] | None |
| 199 |
[typing fixtures/typing-full.pyi] |
| 200 |
|
| 201 |
[case testPEP695GenericTypeAlias] |
| 202 |
import sys |
| 203 |
from typing import Callable |
| 204 |
from types import GenericAlias |
| 205 |
|
| 206 |
from testutil import assertRaises |
| 207 |
|
| 208 |
type A[T] = list[T] |
| 209 |
|
| 210 |
def test_generic_alias() -> None: |
| 211 |
assert type(A[str]) is GenericAlias |
| 212 |
if sys.version_info >= (3, 15): # type: ignore[operator] |
| 213 |
assert str(A[str]) == "_frozen_importlib.A[str]" |
| 214 |
else: |
| 215 |
assert str(A[str]) == "A[str]" |
| 216 |
assert str(getattr(A, "__value__")) == "list[T]" |
| 217 |
|
| 218 |
type B[T, S] = dict[S, T] |
| 219 |
|
| 220 |
def test_generic_alias_with_two_args() -> None: |
| 221 |
if sys.version_info >= (3, 15): # type: ignore[operator] |
| 222 |
assert str(B[str, int]) == "_frozen_importlib.B[str, int]" |
| 223 |
else: |
| 224 |
assert str(B[str, int]) == "B[str, int]" |
| 225 |
assert str(getattr(B, "__value__")) == "dict[S, T]" |
| 226 |
|
| 227 |
type C[*Ts] = tuple[*Ts] |
| 228 |
|
| 229 |
def test_type_var_tuple_type_alias() -> None: |
| 230 |
if sys.version_info >= (3, 15): # type: ignore[operator] |
| 231 |
assert str(C[int, str]) == "_frozen_importlib.C[int, str]" |
| 232 |
assert str(getattr(C, "__value__")) == "tuple[typing.Unpack[~Ts]]" |
| 233 |
else: |
| 234 |
assert str(C[int, str]) == "C[int, str]" |
| 235 |
assert str(getattr(C, "__value__")) == "tuple[typing.Unpack[Ts]]" |
| 236 |
|
| 237 |
type D[**P] = Callable[P, int] |
| 238 |
|
| 239 |
def test_param_spec_type_alias() -> None: |
| 240 |
if sys.version_info >= (3, 15): # type: ignore[operator] |
| 241 |
assert str(D[[int, str]]) == "_frozen_importlib.D[[int, str]]" |
| 242 |
else: |
| 243 |
assert str(D[[int, str]]) == "D[[int, str]]" |
| 244 |
assert str(getattr(D, "__value__")) == "typing.Callable[P, int]" |
| 245 |
[typing fixtures/typing-full.pyi] |