| 1 |
import pytest |
| 2 |
import sys |
| 3 |
from types import GenericAlias, UnionType |
| 4 |
try: |
| 5 |
from _pypy_generic_alias import _create_union |
| 6 |
except ModuleNotFoundError: |
| 7 |
_create_union = None |
| 8 |
|
| 9 |
from typing import TypeVar, Any, Union, TypeVarTuple |
| 10 |
T = TypeVar('T') |
| 11 |
K = TypeVar('K') |
| 12 |
V = TypeVar('V') |
| 13 |
|
| 14 |
def test_ga_init(): |
| 15 |
g = GenericAlias(list, int) |
| 16 |
assert g.__origin__ is list |
| 17 |
assert g.__args__ == (int, ) |
| 18 |
assert g.__parameters__ == () |
| 19 |
g = GenericAlias(list, (int, )) |
| 20 |
assert g.__origin__ is list |
| 21 |
assert g.__args__ == (int, ) |
| 22 |
assert g.__parameters__ == () |
| 23 |
|
| 24 |
def test_ga_instantiate(): |
| 25 |
g = GenericAlias(list, int) |
| 26 |
assert g("abc") == list("abc") |
| 27 |
|
| 28 |
def test_ga_subclass(): |
| 29 |
g = GenericAlias(list, int) |
| 30 |
class l(g): pass |
| 31 |
assert l.__bases__ == (list, ) |
| 32 |
|
| 33 |
def test_ga_unbound_methods(): |
| 34 |
g = GenericAlias(list, int) |
| 35 |
l = [1, 2, 3] |
| 36 |
g.append(l, 4) |
| 37 |
assert l == [1, 2, 3, 4] |
| 38 |
|
| 39 |
def test_ga_classmethod(): |
| 40 |
g = GenericAlias(dict, int) |
| 41 |
d = g.fromkeys([1, 2, 3]) |
| 42 |
assert d == dict.fromkeys([1, 2, 3]) |
| 43 |
|
| 44 |
def test_ga_no_chaining(): |
| 45 |
g = GenericAlias(dict, int) |
| 46 |
with pytest.raises(TypeError): |
| 47 |
g[int] |
| 48 |
|
| 49 |
def test_ga_repr(): |
| 50 |
g = GenericAlias(dict, int) |
| 51 |
assert repr(g) == "dict[int]" |
| 52 |
g = GenericAlias(dict, (int, ...)) |
| 53 |
assert repr(g) == "dict[int, ...]" |
| 54 |
g = GenericAlias(dict, ()) |
| 55 |
assert repr(g) == "dict[()]" |
| 56 |
|
| 57 |
def test_ga_repr_bug(): |
| 58 |
l = list[list[int]] |
| 59 |
assert repr(l) == 'list[list[int]]' |
| 60 |
|
| 61 |
def test_ga_equality(): |
| 62 |
g = GenericAlias(dict, int) |
| 63 |
assert g == GenericAlias(dict, int) |
| 64 |
assert g != GenericAlias(dict, float) |
| 65 |
|
| 66 |
def test_ga_hash(): |
| 67 |
g = GenericAlias(dict, int) |
| 68 |
assert hash(g) == hash(GenericAlias(dict, int)) |
| 69 |
assert hash(g) != hash(GenericAlias(dict, float)) |
| 70 |
|
| 71 |
def test_ga_dir(): |
| 72 |
g = GenericAlias(dict, int) |
| 73 |
assert set(dir(dict)).issubset(set(dir(g))) |
| 74 |
assert "__origin__" in dir(g) |
| 75 |
|
| 76 |
assert len(set(dir(g))) == len(dir(g)) |
| 77 |
|
| 78 |
def test_ga_parameters(): |
| 79 |
g = GenericAlias(dict, (int, V)) |
| 80 |
assert g.__parameters__ == (V, ) |
| 81 |
g = GenericAlias(dict, (V, V)) |
| 82 |
assert g.__parameters__ == (V, ) |
| 83 |
g1 = GenericAlias(list, g) |
| 84 |
assert g1.__parameters__ == (V, ) |
| 85 |
|
| 86 |
def test_ga_parameters_instantiate(): |
| 87 |
g = GenericAlias(dict, (int, V)) |
| 88 |
assert g.__parameters__ == (V, ) |
| 89 |
g1 = g[float] |
| 90 |
assert g1.__origin__ is dict |
| 91 |
assert g1.__args__ == (int, float) |
| 92 |
|
| 93 |
g = GenericAlias(dict, (K, V)) |
| 94 |
assert g.__parameters__ == (K, V, ) |
| 95 |
g1 = g[float, int] |
| 96 |
assert g1.__origin__ is dict |
| 97 |
assert g1.__args__ == (float, int) |
| 98 |
|
| 99 |
g = GenericAlias(list, GenericAlias(dict, (K, V))) |
| 100 |
assert g.__parameters__ == (K, V, ) |
| 101 |
g1 = g[float, int] |
| 102 |
assert g1.__origin__ is list |
| 103 |
assert g1.__args__[0].__origin__ == dict |
| 104 |
assert g1.__args__[0].__args__ == (float, int) |
| 105 |
|
| 106 |
def test_ga_subclasscheck(): |
| 107 |
with pytest.raises(TypeError): |
| 108 |
issubclass(dict, GenericAlias(dict, int)) |
| 109 |
|
| 110 |
def test_ga_instancescheck(): |
| 111 |
with pytest.raises(TypeError): |
| 112 |
isinstance({}, GenericAlias(dict, int)) |
| 113 |
|
| 114 |
def test_ga_new(): |
| 115 |
g = GenericAlias.__new__(GenericAlias, list, int) |
| 116 |
assert g.__origin__ is list |
| 117 |
assert g.__args__ == (int, ) |
| 118 |
|
| 119 |
def test_ga_reduce(): |
| 120 |
g = GenericAlias.__new__(GenericAlias, list, int) |
| 121 |
assert g.__reduce__() == (GenericAlias, (list, (int, ))) |
| 122 |
|
| 123 |
def test_ga_orig_class(): |
| 124 |
class A: |
| 125 |
pass |
| 126 |
|
| 127 |
g = GenericAlias(A, int) |
| 128 |
assert g().__orig_class__ is g |
| 129 |
|
| 130 |
def test_ga_cmp_not_implemented(): |
| 131 |
g = GenericAlias(list, int) |
| 132 |
assert not (g == Any) |
| 133 |
assert g != Any |
| 134 |
|
| 135 |
def test_ga_cant_write_attributes(): |
| 136 |
g = GenericAlias(list, int) |
| 137 |
with pytest.raises(AttributeError): |
| 138 |
g.__origin__ = dict |
| 139 |
with pytest.raises(AttributeError): |
| 140 |
g.__args__ = (1, ) |
| 141 |
with pytest.raises(AttributeError): |
| 142 |
g.__parameters__ = (2, ) |
| 143 |
with pytest.raises(AttributeError): |
| 144 |
g.test = 127 |
| 145 |
|
| 146 |
def test_ga_orig_class_writing_gives_typeerror(): |
| 147 |
class A: |
| 148 |
def __new__(cls): |
| 149 |
return int |
| 150 |
|
| 151 |
g = GenericAlias(A, int) |
| 152 |
assert g() is int |
| 153 |
|
| 154 |
def test_ga_or_does_not_use_typing(): |
| 155 |
union1 = int | float |
| 156 |
union2 = list[int] | float |
| 157 |
assert type(union1) is type(union2) |
| 158 |
|
| 159 |
@pytest.mark.skipif(sys.implementation.name != 'pypy', reason='pypy only') |
| 160 |
def test_ga_or(): |
| 161 |
assert list[int] | float == UnionType((list[int], float)) |
| 162 |
assert list[int] | None == UnionType((list[int], None)) |
| 163 |
|
| 164 |
@pytest.mark.skipif(sys.implementation.name != 'pypy', reason='pypy only') |
| 165 |
def test_ga_ror(): |
| 166 |
assert float | list[int] == UnionType((float, list[int])) |
| 167 |
assert None | list[int] == UnionType((None, list[int])) |
| 168 |
|
| 169 |
def test_ga_subclass_repr(): |
| 170 |
import types |
| 171 |
a = list[int] |
| 172 |
class SubClass(type(a)): ... |
| 173 |
d = SubClass(list, float) |
| 174 |
assert repr(a | d) == repr(a) + " | " + repr(d) |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
def test_union_create(): |
| 179 |
u = int | list |
| 180 |
assert u.__args__ == (int, list) |
| 181 |
u = int | list | int |
| 182 |
assert u.__args__ == (int, list) |
| 183 |
u = int | (list | int | str | float) |
| 184 |
assert u.__args__ == (int, list, str, float) |
| 185 |
|
| 186 |
@pytest.mark.skipif(not _create_union, reason='pypy only') |
| 187 |
def test_union_forbidden_args(): |
| 188 |
assert _create_union(1, int) is NotImplemented |
| 189 |
assert _create_union(int, 1) is NotImplemented |
| 190 |
|
| 191 |
@pytest.mark.skipif(not _create_union, reason='pypy only') |
| 192 |
def test_union_with_itself(): |
| 193 |
assert _create_union(int, int) is int |
| 194 |
|
| 195 |
def test_union_hash_eq(): |
| 196 |
u1 = int | list |
| 197 |
u2 = int | list |
| 198 |
u3 = int | str |
| 199 |
u4 = list | int |
| 200 |
assert u1 == u2 |
| 201 |
assert u1 == u4 |
| 202 |
assert u3 != u1 |
| 203 |
assert u3 != u4 |
| 204 |
|
| 205 |
assert hash(u1) == hash(u2) |
| 206 |
assert hash(u1) == hash(u4) |
| 207 |
assert hash(u3) != hash(u1) |
| 208 |
assert hash(u3) != hash(u4) |
| 209 |
|
| 210 |
def test_union_isinstance(): |
| 211 |
u = int | list |
| 212 |
assert isinstance(1, u) |
| 213 |
assert isinstance([], u) |
| 214 |
assert issubclass(int, u) |
| 215 |
assert issubclass(list, u) |
| 216 |
|
| 217 |
u2 = int | None |
| 218 |
assert isinstance(None, u2) |
| 219 |
assert isinstance(6, u2) |
| 220 |
assert issubclass(int, u2) |
| 221 |
assert issubclass(type(None), u2) |
| 222 |
|
| 223 |
if sys.implementation.name == 'pypy': |
| 224 |
|
| 225 |
with pytest.raises(TypeError): |
| 226 |
issubclass(int, int | GenericAlias(dict, int)) |
| 227 |
with pytest.raises(TypeError): |
| 228 |
isinstance(2, int | GenericAlias(dict, int)) |
| 229 |
|
| 230 |
def test_union_repr(): |
| 231 |
u = int | list |
| 232 |
assert repr(u) == "int | list" |
| 233 |
|
| 234 |
@pytest.mark.skipif(sys.implementation.name != 'pypy', reason='pypy only') |
| 235 |
def test_union_or(): |
| 236 |
u = int | list |
| 237 |
assert u | int == UnionType((int, list)) |
| 238 |
|
| 239 |
@pytest.mark.skipif(sys.implementation.name != 'pypy', reason='pypy only') |
| 240 |
def test_union_ror(): |
| 241 |
assert None | int == UnionType((None, int)) |
| 242 |
assert None | (int | float) == UnionType((None, int, float)) |
| 243 |
|
| 244 |
def test_union_parameters(): |
| 245 |
assert (int | list[T]).__parameters__ == (T, ) |
| 246 |
|
| 247 |
def test_union_typevars(): |
| 248 |
assert (float | list[T])[int] == float | list[int] |
| 249 |
|
| 250 |
def test_union_type_none(): |
| 251 |
assert int | type(None) == int | None |
| 252 |
|
| 253 |
def test_unpacked(): |
| 254 |
ga = GenericAlias(tuple, (int, )) |
| 255 |
starred = list(ga)[0] |
| 256 |
assert starred.__unpacked__ is True |
| 257 |
assert repr(starred) == '*tuple[int]' |
| 258 |
assert starred != ga |
| 259 |
func, args = starred.__reduce__() |
| 260 |
reconstructed = func(*args) |
| 261 |
assert reconstructed == starred |
| 262 |
assert reconstructed != ga |
| 263 |
|
| 264 |
def test_unpacked_subst(): |
| 265 |
T = TypeVar('T') |
| 266 |
ga = GenericAlias(tuple, (T, )) |
| 267 |
starred_generic = list(ga)[0] |
| 268 |
assert starred_generic[int].__unpacked__ |
| 269 |
|
| 270 |
def test_isinstance_type(): |
| 271 |
ga = GenericAlias(tuple, (int, )) |
| 272 |
assert ga.__class__ is GenericAlias |
| 273 |
assert not isinstance(ga, type) |
| 274 |
|
| 275 |
def test_typevartuple_subst(): |
| 276 |
Ts = TypeVarTuple('Ts') |
| 277 |
|
| 278 |
A = GenericAlias(tuple, (*Ts, )) |
| 279 |
assert A[int] == GenericAlias(tuple, (int, )) |
| 280 |
assert A[int, float] == GenericAlias(tuple, (int, float)) |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
B = GenericAlias(list, (*Ts, )) |
| 285 |
|
| 286 |
tupinner = GenericAlias(tuple, (int, float, bool)) |
| 287 |
tup = B[tupinner] |
| 288 |
assert tup == GenericAlias(list, tupinner) |
| 289 |
tup = B[*tupinner] |
| 290 |
assert tup == GenericAlias(list, (int, float, bool)) |
| 291 |
|
| 292 |
def test_collect_parameters_bug(): |
| 293 |
from typing import TypeVar |
| 294 |
T = TypeVar('T') |
| 295 |
class A: |
| 296 |
__parameters__ = (T,) |
| 297 |
g = GenericAlias(list, (A, )) |
| 298 |
assert g.__parameters__ == () |
| 299 |
with pytest.raises(TypeError): |
| 300 |
g[str] |
| 301 |
|
| 302 |
|