| 1 |
[case testI64BasicOps] |
| 2 |
from typing import List, Any, Tuple, Union |
| 3 |
|
| 4 |
from mypy_extensions import i64, i32, i16 |
| 5 |
|
| 6 |
from testutil import assertRaises |
| 7 |
|
| 8 |
def inc(n: i64) -> i64: |
| 9 |
return n + 1 |
| 10 |
|
| 11 |
def test_inc() -> None: |
| 12 |
# Use int() to avoid constant folding |
| 13 |
n = 1 + int() |
| 14 |
m = 2 + int() |
| 15 |
assert inc(n) == m |
| 16 |
|
| 17 |
def min_ll(x: i64, y: i64) -> i64: |
| 18 |
if x < y: |
| 19 |
return x |
| 20 |
else: |
| 21 |
return y |
| 22 |
|
| 23 |
def test_min() -> None: |
| 24 |
assert min_ll(1 + int(), 2) == 1 |
| 25 |
assert min_ll(2 + int(), 1) == 1 |
| 26 |
assert min_ll(1 + int(), 1) == 1 |
| 27 |
assert min_ll(-2 + int(), 1) == -2 |
| 28 |
assert min_ll(1 + int(), -2) == -2 |
| 29 |
|
| 30 |
def eq(x: i64, y: i64) -> bool: |
| 31 |
return x == y |
| 32 |
|
| 33 |
def test_eq() -> None: |
| 34 |
assert eq(int(), int()) |
| 35 |
assert eq(5 + int(), 5 + int()) |
| 36 |
assert eq(-5 + int(), -5 + int()) |
| 37 |
assert not eq(int(), 1 + int()) |
| 38 |
assert not eq(5 + int(), 6 + int()) |
| 39 |
assert not eq(-5 + int(), -6 + int()) |
| 40 |
assert not eq(-5 + int(), 5 + int()) |
| 41 |
|
| 42 |
def test_comparisons() -> None: |
| 43 |
one: i64 = 1 + int() |
| 44 |
one2: i64 = 1 + int() |
| 45 |
two: i64 = 2 + int() |
| 46 |
assert one < two |
| 47 |
assert not (one < one2) |
| 48 |
assert not (two < one) |
| 49 |
assert two > one |
| 50 |
assert not (one > one2) |
| 51 |
assert not (one > two) |
| 52 |
assert one <= two |
| 53 |
assert one <= one2 |
| 54 |
assert not (two <= one) |
| 55 |
assert two >= one |
| 56 |
assert one >= one2 |
| 57 |
assert not (one >= two) |
| 58 |
assert one == one2 |
| 59 |
assert not (one == two) |
| 60 |
assert one != two |
| 61 |
assert not (one != one2) |
| 62 |
|
| 63 |
def is_true(x: i64) -> bool: |
| 64 |
if x: |
| 65 |
return True |
| 66 |
else: |
| 67 |
return False |
| 68 |
|
| 69 |
def is_true2(x: i64) -> bool: |
| 70 |
return bool(x) |
| 71 |
|
| 72 |
def is_false(x: i64) -> bool: |
| 73 |
if not x: |
| 74 |
return True |
| 75 |
else: |
| 76 |
return False |
| 77 |
|
| 78 |
def test_i64_as_bool() -> None: |
| 79 |
assert not is_true(0) |
| 80 |
assert not is_true2(0) |
| 81 |
assert is_false(0) |
| 82 |
for x in 1, 55, -1, -7, 1 << 40, -(1 << 50): |
| 83 |
assert is_true(x) |
| 84 |
assert is_true2(x) |
| 85 |
assert not is_false(x) |
| 86 |
|
| 87 |
def bool_as_i64(b: bool) -> i64: |
| 88 |
return b |
| 89 |
|
| 90 |
def test_bool_as_i64() -> None: |
| 91 |
assert bool_as_i64(False) == 0 |
| 92 |
assert bool_as_i64(True) == 1 |
| 93 |
|
| 94 |
def div_by_3(x: i64) -> i64: |
| 95 |
return x // 3 |
| 96 |
|
| 97 |
def div_by_neg_3(x: i64) -> i64: |
| 98 |
return x // -3 |
| 99 |
|
| 100 |
def div(x: i64, y: i64) -> i64: |
| 101 |
return x // y |
| 102 |
|
| 103 |
def test_divide_by_constant() -> None: |
| 104 |
for i in range(-1000, 1000): |
| 105 |
assert div_by_3(i) == i // 3 |
| 106 |
for i in range(-2**63, -2**63 + 1000): |
| 107 |
assert div_by_3(i) == i // 3 |
| 108 |
for i in range(2**63 - 1000, 2**63): |
| 109 |
assert div_by_3(i) == i // 3 |
| 110 |
|
| 111 |
def test_divide_by_negative_constant() -> None: |
| 112 |
for i in range(-1000, 1000): |
| 113 |
assert div_by_neg_3(i) == i // -3 |
| 114 |
for i in range(-2**63, -2**63 + 1000): |
| 115 |
assert div_by_neg_3(i) == i // -3 |
| 116 |
for i in range(2**63 - 1000, 2**63): |
| 117 |
assert div_by_neg_3(i) == i // -3 |
| 118 |
|
| 119 |
def test_divide_by_variable() -> None: |
| 120 |
values = (list(range(-50, 50)) + |
| 121 |
list(range(-2**63, -2**63 + 10)) + |
| 122 |
list(range(2**63 - 10, 2**63))) |
| 123 |
for x in values: |
| 124 |
for y in values: |
| 125 |
if y != 0: |
| 126 |
if x // y == 2**63: |
| 127 |
with assertRaises(OverflowError, "integer division overflow"): |
| 128 |
div(x, y) |
| 129 |
else: |
| 130 |
assert div(x, y) == x // y |
| 131 |
else: |
| 132 |
with assertRaises(ZeroDivisionError, "integer division or modulo by zero"): |
| 133 |
div(x, y) |
| 134 |
|
| 135 |
def mod_by_7(x: i64) -> i64: |
| 136 |
return x % 7 |
| 137 |
|
| 138 |
def mod_by_neg_7(x: i64) -> i64: |
| 139 |
return x // -7 |
| 140 |
|
| 141 |
def mod(x: i64, y: i64) -> i64: |
| 142 |
return x % y |
| 143 |
|
| 144 |
def test_mod_by_constant() -> None: |
| 145 |
for i in range(-1000, 1000): |
| 146 |
assert mod_by_7(i) == i % 7 |
| 147 |
for i in range(-2**63, -2**63 + 1000): |
| 148 |
assert mod_by_7(i) == i % 7 |
| 149 |
for i in range(2**63 - 1000, 2**63): |
| 150 |
assert mod_by_7(i) == i % 7 |
| 151 |
|
| 152 |
def test_mod_by_negative_constant() -> None: |
| 153 |
for i in range(-1000, 1000): |
| 154 |
assert mod_by_neg_7(i) == i // -7 |
| 155 |
for i in range(-2**63, -2**63 + 1000): |
| 156 |
assert mod_by_neg_7(i) == i // -7 |
| 157 |
for i in range(2**63 - 1000, 2**63): |
| 158 |
assert mod_by_neg_7(i) == i // -7 |
| 159 |
|
| 160 |
def test_mod_by_variable() -> None: |
| 161 |
values = (list(range(-50, 50)) + |
| 162 |
list(range(-2**63, -2**63 + 10)) + |
| 163 |
list(range(2**63 - 10, 2**63))) |
| 164 |
for x in values: |
| 165 |
for y in values: |
| 166 |
if y != 0: |
| 167 |
assert mod(x, y) == x % y |
| 168 |
else: |
| 169 |
with assertRaises(ZeroDivisionError, "integer division or modulo by zero"): |
| 170 |
mod(x, y) |
| 171 |
|
| 172 |
def get_item(a: List[i64], n: i64) -> i64: |
| 173 |
return a[n] |
| 174 |
|
| 175 |
def test_get_list_item() -> None: |
| 176 |
a = [1, 6, -2] |
| 177 |
assert get_item(a, 0) == 1 |
| 178 |
assert get_item(a, 1) == 6 |
| 179 |
assert get_item(a, 2) == -2 |
| 180 |
assert get_item(a, -1) == -2 |
| 181 |
assert get_item(a, -2) == 6 |
| 182 |
assert get_item(a, -3) == 1 |
| 183 |
with assertRaises(IndexError, "list index out of range"): |
| 184 |
get_item(a, 3) |
| 185 |
with assertRaises(IndexError, "list index out of range"): |
| 186 |
get_item(a, -4) |
| 187 |
# TODO: Very large/small values and indexes |
| 188 |
|
| 189 |
def test_simple_arithmetic_ops() -> None: |
| 190 |
zero: i64 = int() |
| 191 |
one: i64 = zero + 1 |
| 192 |
two: i64 = one + 1 |
| 193 |
neg_one: i64 = -one |
| 194 |
assert one + one == 2 |
| 195 |
assert one + two == 3 |
| 196 |
assert one + neg_one == 0 |
| 197 |
assert one - one == 0 |
| 198 |
assert one - two == -1 |
| 199 |
assert one * one == 1 |
| 200 |
assert one * two == 2 |
| 201 |
assert two * two == 4 |
| 202 |
assert two * neg_one == -2 |
| 203 |
assert neg_one * one == -1 |
| 204 |
assert neg_one * neg_one == 1 |
| 205 |
assert two * 0 == 0 |
| 206 |
assert 0 * two == 0 |
| 207 |
assert -one == -1 |
| 208 |
assert -two == -2 |
| 209 |
assert -neg_one == 1 |
| 210 |
assert -zero == 0 |
| 211 |
|
| 212 |
def test_bitwise_ops() -> None: |
| 213 |
x: i64 = 7997307308812232241 + int() |
| 214 |
y: i64 = 4333433528471475340 + int() |
| 215 |
z: i64 = -2462230749488444526 + int() |
| 216 |
zero: i64 = int() |
| 217 |
one: i64 = zero + 1 |
| 218 |
two: i64 = zero + 2 |
| 219 |
neg_one: i64 = -one |
| 220 |
|
| 221 |
assert x & y == 3179577071592752128 |
| 222 |
assert x & z == 5536089561888850448 |
| 223 |
assert z & z == z |
| 224 |
assert x & zero == 0 |
| 225 |
|
| 226 |
assert x | y == 9151163765690955453 |
| 227 |
assert x | z == -1013002565062733 |
| 228 |
assert z | z == z |
| 229 |
assert x | 0 == x |
| 230 |
|
| 231 |
assert x ^ y == 5971586694098203325 |
| 232 |
assert x ^ z == -5537102564453913181 |
| 233 |
assert z ^ z == 0 |
| 234 |
assert z ^ 0 == z |
| 235 |
|
| 236 |
assert x << one == -2452129456085087134 |
| 237 |
assert x << two == -4904258912170174268 |
| 238 |
assert z << two == 8597821075755773512 |
| 239 |
assert z << 0 == z |
| 240 |
|
| 241 |
assert x >> one == 3998653654406116120 |
| 242 |
assert x >> two == 1999326827203058060 |
| 243 |
assert z >> two == -615557687372111132 |
| 244 |
assert z >> 0 == z |
| 245 |
|
| 246 |
assert ~x == -7997307308812232242 |
| 247 |
assert ~z == 2462230749488444525 |
| 248 |
assert ~zero == -1 |
| 249 |
assert ~neg_one == 0 |
| 250 |
|
| 251 |
def test_coerce_to_and_from_int() -> None: |
| 252 |
for shift in range(0, 64): |
| 253 |
for sign in 1, -1: |
| 254 |
for delta in range(-5, 5): |
| 255 |
n = sign * (1 << shift) + delta |
| 256 |
if -(1 << 63) <= n < (1 << 63): |
| 257 |
x: i64 = n |
| 258 |
m: int = x |
| 259 |
assert m == n |
| 260 |
|
| 261 |
def test_coerce_to_and_from_int2() -> None: |
| 262 |
for shift in range(0, 64): |
| 263 |
for sign in 1, -1: |
| 264 |
for delta in range(-5, 5): |
| 265 |
n = sign * (1 << shift) + delta |
| 266 |
if -(1 << 63) <= n < (1 << 63): |
| 267 |
x: i64 = i64(n) |
| 268 |
m: int = int(x) |
| 269 |
assert m == n |
| 270 |
|
| 271 |
def test_explicit_conversion_to_i64() -> None: |
| 272 |
x = i64(5) |
| 273 |
assert x == 5 |
| 274 |
y = int() - 113 |
| 275 |
x = i64(y) |
| 276 |
assert x == -113 |
| 277 |
n32: i32 = 1733 |
| 278 |
x = i64(n32) |
| 279 |
assert x == 1733 |
| 280 |
n32 = -1733 |
| 281 |
x = i64(n32) |
| 282 |
assert x == -1733 |
| 283 |
z = i64(x) |
| 284 |
assert z == -1733 |
| 285 |
a: i16 = int() + 19764 |
| 286 |
assert i64(a) == 19764 |
| 287 |
a = int() - 1 |
| 288 |
assert i64(a) == -1 |
| 289 |
|
| 290 |
def test_explicit_conversion_overflow() -> None: |
| 291 |
max_i64 = int() + 2**63 - 1 |
| 292 |
x = i64(max_i64) |
| 293 |
assert x == 2**63 - 1 |
| 294 |
assert int(x) == max_i64 |
| 295 |
|
| 296 |
min_i64 = int() - 2**63 |
| 297 |
y = i64(min_i64) |
| 298 |
assert y == -2**63 |
| 299 |
assert int(y) == min_i64 |
| 300 |
|
| 301 |
too_big = int() + 2**63 |
| 302 |
with assertRaises(ValueError): |
| 303 |
x = i64(too_big) |
| 304 |
|
| 305 |
too_small = int() - 2**63 - 1 |
| 306 |
with assertRaises(ValueError): |
| 307 |
x = i64(too_small) |
| 308 |
|
| 309 |
def test_i64_from_large_small_literal() -> None: |
| 310 |
x = i64(2**63 - 1) |
| 311 |
assert x == 2**63 - 1 |
| 312 |
x = i64(-2**63) |
| 313 |
assert x == -2**63 |
| 314 |
|
| 315 |
def from_float(x: float) -> i64: |
| 316 |
return i64(x) |
| 317 |
|
| 318 |
def test_explicit_conversion_from_float() -> None: |
| 319 |
assert from_float(0.0) == 0 |
| 320 |
assert from_float(1.456) == 1 |
| 321 |
assert from_float(-1234.567) == -1234 |
| 322 |
# Subtract 1024 due to limited precision of 64-bit floats |
| 323 |
assert from_float(2**63 - 1024) == 2**63 - 1024 |
| 324 |
assert from_float(-2**63) == -2**63 |
| 325 |
# The error message could be better, but this is acceptable |
| 326 |
with assertRaises(ValueError, "int too large to convert to i64"): |
| 327 |
assert from_float(float(2**63)) |
| 328 |
with assertRaises(ValueError, "int too large to convert to i64"): |
| 329 |
# One ulp below the lowest valid i64 value |
| 330 |
from_float(float(-2**63 - 2048)) |
| 331 |
|
| 332 |
def from_str(s: str) -> i64: |
| 333 |
return i64(s) |
| 334 |
|
| 335 |
def test_explicit_conversion_from_str() -> None: |
| 336 |
assert from_str("0") == 0 |
| 337 |
assert from_str("1") == 1 |
| 338 |
assert from_str("-1234") == -1234 |
| 339 |
with assertRaises(ValueError): |
| 340 |
from_str("1.2") |
| 341 |
|
| 342 |
def from_str_with_base(s: str, base: int) -> i64: |
| 343 |
return i64(s, base) |
| 344 |
|
| 345 |
def test_explicit_conversion_from_str_with_base() -> None: |
| 346 |
assert from_str_with_base("101", 2) == 5 |
| 347 |
assert from_str_with_base("109", 10) == 109 |
| 348 |
assert from_str_with_base("-f0A", 16) == -3850 |
| 349 |
assert from_str_with_base("0x1a", 16) == 26 |
| 350 |
assert from_str_with_base("0X1A", 16) == 26 |
| 351 |
with assertRaises(ValueError): |
| 352 |
from_str_with_base("1.2", 16) |
| 353 |
|
| 354 |
def from_bool(b: bool) -> i64: |
| 355 |
return i64(b) |
| 356 |
|
| 357 |
def test_explicit_conversion_from_bool() -> None: |
| 358 |
assert from_bool(True) == 1 |
| 359 |
assert from_bool(False) == 0 |
| 360 |
|
| 361 |
class IntConv: |
| 362 |
def __init__(self, x: i64) -> None: |
| 363 |
self.x = x |
| 364 |
|
| 365 |
def __int__(self) -> i64: |
| 366 |
return self.x + 1 |
| 367 |
|
| 368 |
def test_explicit_conversion_from_instance() -> None: |
| 369 |
assert i64(IntConv(0)) == 1 |
| 370 |
assert i64(IntConv(12345)) == 12346 |
| 371 |
assert i64(IntConv(-23)) == -22 |
| 372 |
|
| 373 |
def test_explicit_conversion_from_any() -> None: |
| 374 |
# This can't be specialized |
| 375 |
a: Any = "101" |
| 376 |
assert i64(a, base=2) == 5 |
| 377 |
|
| 378 |
def test_tuple_i64() -> None: |
| 379 |
a: i64 = 1 |
| 380 |
b: i64 = 2 |
| 381 |
t = (a, b) |
| 382 |
a, b = t |
| 383 |
assert a == 1 |
| 384 |
assert b == 2 |
| 385 |
x: Any = t |
| 386 |
tt: Tuple[i64, i64] = x |
| 387 |
assert tt == (1, 2) |
| 388 |
|
| 389 |
def test_list_set_item() -> None: |
| 390 |
a: List[i64] = [0, 2, 6] |
| 391 |
z: i64 = int() |
| 392 |
a[z] = 1 |
| 393 |
assert a == [1, 2, 6] |
| 394 |
a[z + 2] = 9 |
| 395 |
assert a == [1, 2, 9] |
| 396 |
a[-(z + 1)] = 10 |
| 397 |
assert a == [1, 2, 10] |
| 398 |
a[-(z + 3)] = 3 |
| 399 |
assert a == [3, 2, 10] |
| 400 |
with assertRaises(IndexError): |
| 401 |
a[z + 3] = 0 |
| 402 |
with assertRaises(IndexError): |
| 403 |
a[-(z + 4)] = 0 |
| 404 |
assert a == [3, 2, 10] |
| 405 |
|
| 406 |
class C: |
| 407 |
def __init__(self, x: i64) -> None: |
| 408 |
self.x = x |
| 409 |
|
| 410 |
def test_attributes() -> None: |
| 411 |
i: i64 |
| 412 |
for i in range(-1000, 1000): |
| 413 |
c = C(i) |
| 414 |
assert c.x == i |
| 415 |
c.x = i + 1 |
| 416 |
assert c.x == i + 1 |
| 417 |
|
| 418 |
def test_mixed_comparisons() -> None: |
| 419 |
i64_3: i64 = int() + 3 |
| 420 |
int_5 = int() + 5 |
| 421 |
assert i64_3 < int_5 |
| 422 |
assert int_5 > i64_3 |
| 423 |
b = i64_3 > int_5 |
| 424 |
assert not b |
| 425 |
|
| 426 |
int_largest = int() + (1 << 63) - 1 |
| 427 |
assert int_largest > i64_3 |
| 428 |
int_smallest = int() - (1 << 63) |
| 429 |
assert i64_3 > int_smallest |
| 430 |
|
| 431 |
int_too_big = int() + (1 << 63) |
| 432 |
int_too_small = int() - (1 << 63) - 1 |
| 433 |
with assertRaises(ValueError): |
| 434 |
assert i64_3 < int_too_big |
| 435 |
with assertRaises(ValueError): |
| 436 |
assert int_too_big < i64_3 |
| 437 |
with assertRaises(ValueError): |
| 438 |
assert i64_3 > int_too_small |
| 439 |
with assertRaises(ValueError): |
| 440 |
assert int_too_small < i64_3 |
| 441 |
|
| 442 |
def test_mixed_comparisons_32bit() -> None: |
| 443 |
# Test edge cases on 32-bit platforms |
| 444 |
i64_3: i64 = int() + 3 |
| 445 |
int_5 = int() + 5 |
| 446 |
|
| 447 |
int_largest_short = int() + (1 << 30) - 1 |
| 448 |
int_largest_short_i64: i64 = int_largest_short |
| 449 |
assert int_largest_short > i64_3 |
| 450 |
int_smallest_short = int() - (1 << 30) |
| 451 |
int_smallest_short_i64: i64 = int_smallest_short |
| 452 |
assert i64_3 > int_smallest_short |
| 453 |
|
| 454 |
int_big = int() + (1 << 30) |
| 455 |
assert int_big > i64_3 |
| 456 |
int_small = int() - (1 << 30) - 1 |
| 457 |
assert i64_3 > int_small |
| 458 |
|
| 459 |
assert int_smallest_short_i64 > int_small |
| 460 |
assert int_largest_short_i64 < int_big |
| 461 |
|
| 462 |
def test_mixed_arithmetic_and_bitwise_ops() -> None: |
| 463 |
i64_3: i64 = int() + 3 |
| 464 |
int_5 = int() + 5 |
| 465 |
assert i64_3 + int_5 == 8 |
| 466 |
assert int_5 - i64_3 == 2 |
| 467 |
assert i64_3 << int_5 == 96 |
| 468 |
assert int_5 << i64_3 == 40 |
| 469 |
assert i64_3 ^ int_5 == 6 |
| 470 |
assert int_5 | i64_3 == 7 |
| 471 |
|
| 472 |
int_largest = int() + (1 << 63) - 1 |
| 473 |
assert int_largest - i64_3 == 9223372036854775804 |
| 474 |
int_smallest = int() - (1 << 63) |
| 475 |
assert int_smallest + i64_3 == -9223372036854775805 |
| 476 |
|
| 477 |
int_too_big = int() + (1 << 63) |
| 478 |
int_too_small = int() - (1 << 63) - 1 |
| 479 |
with assertRaises(ValueError): |
| 480 |
assert i64_3 & int_too_big |
| 481 |
with assertRaises(ValueError): |
| 482 |
assert int_too_small & i64_3 |
| 483 |
|
| 484 |
def test_for_loop() -> None: |
| 485 |
n: i64 = 0 |
| 486 |
for i in range(i64(5 + int())): |
| 487 |
n += i |
| 488 |
assert n == 10 |
| 489 |
n = 0 |
| 490 |
for i in range(i64(5)): |
| 491 |
n += i |
| 492 |
assert n == 10 |
| 493 |
n = 0 |
| 494 |
for i in range(i64(2 + int()), 5 + int()): |
| 495 |
n += i |
| 496 |
assert n == 9 |
| 497 |
n = 0 |
| 498 |
for i in range(2, i64(5 + int())): |
| 499 |
n += i |
| 500 |
assert n == 9 |
| 501 |
assert sum([x * x for x in range(i64(4 + int()))]) == 1 + 4 + 9 |
| 502 |
|
| 503 |
def narrow1(x: Union[str, i64]) -> i64: |
| 504 |
if isinstance(x, i64): |
| 505 |
return x |
| 506 |
return len(x) |
| 507 |
|
| 508 |
def narrow2(x: Union[str, i64]) -> i64: |
| 509 |
if isinstance(x, int): |
| 510 |
return x |
| 511 |
return len(x) |
| 512 |
|
| 513 |
def test_isinstance() -> None: |
| 514 |
assert narrow1(123) == 123 |
| 515 |
assert narrow1("foobar") == 6 |
| 516 |
assert narrow2(123) == 123 |
| 517 |
assert narrow2("foobar") == 6 |
| 518 |
|
| 519 |
[case testI64ErrorValuesAndUndefined] |
| 520 |
from typing import Any, Final, Tuple |
| 521 |
import sys |
| 522 |
|
| 523 |
from mypy_extensions import mypyc_attr, i64 |
| 524 |
|
| 525 |
from testutil import assertRaises |
| 526 |
|
| 527 |
def maybe_raise(n: i64, error: bool) -> i64: |
| 528 |
if error: |
| 529 |
raise ValueError() |
| 530 |
return n |
| 531 |
|
| 532 |
def test_error_value() -> None: |
| 533 |
for i in range(-1000, 1000): |
| 534 |
assert maybe_raise(i, False) == i |
| 535 |
with assertRaises(ValueError): |
| 536 |
maybe_raise(0, True) |
| 537 |
|
| 538 |
class C: |
| 539 |
def maybe_raise(self, n: i64, error: bool) -> i64: |
| 540 |
if error: |
| 541 |
raise ValueError() |
| 542 |
return n |
| 543 |
|
| 544 |
def test_method_error_value() -> None: |
| 545 |
for i in range(-1000, 1000): |
| 546 |
assert C().maybe_raise(i, False) == i |
| 547 |
with assertRaises(ValueError): |
| 548 |
C().maybe_raise(0, True) |
| 549 |
|
| 550 |
def maybe_raise_tuple(n: i64, error: bool) -> Tuple[i64, i64]: |
| 551 |
if error: |
| 552 |
raise ValueError() |
| 553 |
return n, n+ 1 |
| 554 |
|
| 555 |
def test_tuple_error_value() -> None: |
| 556 |
for i in range(-1000, 1000): |
| 557 |
assert maybe_raise_tuple(i, False) == (i, i + 1) |
| 558 |
with assertRaises(ValueError): |
| 559 |
maybe_raise_tuple(0, True) |
| 560 |
f: Any = maybe_raise_tuple |
| 561 |
for i in range(-1000, 1000): |
| 562 |
assert f(i, False) == (i, i + 1) |
| 563 |
with assertRaises(ValueError): |
| 564 |
f(0, True) |
| 565 |
|
| 566 |
def maybe_raise_tuple2(n: i64, error: bool) -> Tuple[i64, int]: |
| 567 |
if error: |
| 568 |
raise ValueError() |
| 569 |
return n, n+ 1 |
| 570 |
|
| 571 |
def test_tuple_error_value_2() -> None: |
| 572 |
for i in range(-1000, 1000): |
| 573 |
assert maybe_raise_tuple2(i, False) == (i, i + 1) |
| 574 |
with assertRaises(ValueError): |
| 575 |
maybe_raise_tuple(0, True) |
| 576 |
|
| 577 |
def test_unbox_int() -> None: |
| 578 |
for i in list(range(-1000, 1000)) + [-(1 << 63), (1 << 63) - 1]: |
| 579 |
o: Any = i |
| 580 |
x: i64 = i |
| 581 |
assert x == i |
| 582 |
y: i64 = o |
| 583 |
assert y == i |
| 584 |
|
| 585 |
def test_unbox_int_fails() -> None: |
| 586 |
o: Any = 'x' |
| 587 |
if sys.version_info[0] == 3 and sys.version_info[1] < 10: |
| 588 |
msg = "an integer is required (got type str)" |
| 589 |
else: |
| 590 |
msg = "'str' object cannot be interpreted as an integer" |
| 591 |
with assertRaises(TypeError, msg): |
| 592 |
x: i64 = o |
| 593 |
o2: Any = 1 << 63 |
| 594 |
with assertRaises(ValueError, "int too large to convert to i64"): |
| 595 |
y: i64 = o2 |
| 596 |
o3: Any = -(1 << 63 + 1) |
| 597 |
with assertRaises(ValueError, "int too large to convert to i64"): |
| 598 |
z: i64 = o3 |
| 599 |
|
| 600 |
class Uninit: |
| 601 |
x: i64 |
| 602 |
y: i64 = 0 |
| 603 |
z: i64 |
| 604 |
|
| 605 |
class Derived(Uninit): |
| 606 |
a: i64 = 1 |
| 607 |
b: i64 |
| 608 |
c: i64 = 2 |
| 609 |
|
| 610 |
class Derived2(Derived): |
| 611 |
h: i64 |
| 612 |
|
| 613 |
def test_uninitialized_attr() -> None: |
| 614 |
o = Uninit() |
| 615 |
assert o.y == 0 |
| 616 |
with assertRaises(AttributeError): |
| 617 |
o.x |
| 618 |
with assertRaises(AttributeError): |
| 619 |
o.z |
| 620 |
o.x = 1 |
| 621 |
assert o.x == 1 |
| 622 |
with assertRaises(AttributeError): |
| 623 |
o.z |
| 624 |
o.z = 2 |
| 625 |
assert o.z == 2 |
| 626 |
|
| 627 |
# This is the error value, but it's also a valid normal value |
| 628 |
MAGIC: Final = -113 |
| 629 |
|
| 630 |
def test_magic_value() -> None: |
| 631 |
o = Uninit() |
| 632 |
o.x = MAGIC |
| 633 |
assert o.x == MAGIC |
| 634 |
with assertRaises(AttributeError): |
| 635 |
o.z |
| 636 |
o.z = MAGIC |
| 637 |
assert o.x == MAGIC |
| 638 |
assert o.z == MAGIC |
| 639 |
|
| 640 |
def test_magic_value_via_any() -> None: |
| 641 |
o: Any = Uninit() |
| 642 |
with assertRaises(AttributeError): |
| 643 |
o.x |
| 644 |
with assertRaises(AttributeError): |
| 645 |
o.z |
| 646 |
o.x = MAGIC |
| 647 |
assert o.x == MAGIC |
| 648 |
with assertRaises(AttributeError): |
| 649 |
o.z |
| 650 |
o.z = MAGIC |
| 651 |
assert o.z == MAGIC |
| 652 |
|
| 653 |
def test_magic_value_and_inheritance() -> None: |
| 654 |
o = Derived2() |
| 655 |
o.x = MAGIC |
| 656 |
assert o.x == MAGIC |
| 657 |
with assertRaises(AttributeError): |
| 658 |
o.z |
| 659 |
with assertRaises(AttributeError): |
| 660 |
o.b |
| 661 |
with assertRaises(AttributeError): |
| 662 |
o.h |
| 663 |
o.z = MAGIC |
| 664 |
assert o.z == MAGIC |
| 665 |
with assertRaises(AttributeError): |
| 666 |
o.b |
| 667 |
with assertRaises(AttributeError): |
| 668 |
o.h |
| 669 |
o.h = MAGIC |
| 670 |
assert o.h == MAGIC |
| 671 |
with assertRaises(AttributeError): |
| 672 |
o.b |
| 673 |
o.b = MAGIC |
| 674 |
assert o.b == MAGIC |
| 675 |
|
| 676 |
@mypyc_attr(allow_interpreted_subclasses=True) |
| 677 |
class MagicInit: |
| 678 |
x: i64 = MAGIC |
| 679 |
|
| 680 |
def test_magic_value_as_initializer() -> None: |
| 681 |
o = MagicInit() |
| 682 |
assert o.x == MAGIC |
| 683 |
|
| 684 |
class ManyUninit: |
| 685 |
a1: i64 |
| 686 |
a2: i64 |
| 687 |
a3: i64 |
| 688 |
a4: i64 |
| 689 |
a5: i64 |
| 690 |
a6: i64 |
| 691 |
a7: i64 |
| 692 |
a8: i64 |
| 693 |
a9: i64 |
| 694 |
a10: i64 |
| 695 |
a11: i64 |
| 696 |
a12: i64 |
| 697 |
a13: i64 |
| 698 |
a14: i64 |
| 699 |
a15: i64 |
| 700 |
a16: i64 |
| 701 |
a17: i64 |
| 702 |
a18: i64 |
| 703 |
a19: i64 |
| 704 |
a20: i64 |
| 705 |
a21: i64 |
| 706 |
a22: i64 |
| 707 |
a23: i64 |
| 708 |
a24: i64 |
| 709 |
a25: i64 |
| 710 |
a26: i64 |
| 711 |
a27: i64 |
| 712 |
a28: i64 |
| 713 |
a29: i64 |
| 714 |
a30: i64 |
| 715 |
a31: i64 |
| 716 |
a32: i64 |
| 717 |
a33: i64 |
| 718 |
a34: i64 |
| 719 |
a35: i64 |
| 720 |
a36: i64 |
| 721 |
a37: i64 |
| 722 |
a38: i64 |
| 723 |
a39: i64 |
| 724 |
a40: i64 |
| 725 |
a41: i64 |
| 726 |
a42: i64 |
| 727 |
a43: i64 |
| 728 |
a44: i64 |
| 729 |
a45: i64 |
| 730 |
a46: i64 |
| 731 |
a47: i64 |
| 732 |
a48: i64 |
| 733 |
a49: i64 |
| 734 |
a50: i64 |
| 735 |
a51: i64 |
| 736 |
a52: i64 |
| 737 |
a53: i64 |
| 738 |
a54: i64 |
| 739 |
a55: i64 |
| 740 |
a56: i64 |
| 741 |
a57: i64 |
| 742 |
a58: i64 |
| 743 |
a59: i64 |
| 744 |
a60: i64 |
| 745 |
a61: i64 |
| 746 |
a62: i64 |
| 747 |
a63: i64 |
| 748 |
a64: i64 |
| 749 |
a65: i64 |
| 750 |
a66: i64 |
| 751 |
a67: i64 |
| 752 |
a68: i64 |
| 753 |
a69: i64 |
| 754 |
a70: i64 |
| 755 |
a71: i64 |
| 756 |
a72: i64 |
| 757 |
a73: i64 |
| 758 |
a74: i64 |
| 759 |
a75: i64 |
| 760 |
a76: i64 |
| 761 |
a77: i64 |
| 762 |
a78: i64 |
| 763 |
a79: i64 |
| 764 |
a80: i64 |
| 765 |
a81: i64 |
| 766 |
a82: i64 |
| 767 |
a83: i64 |
| 768 |
a84: i64 |
| 769 |
a85: i64 |
| 770 |
a86: i64 |
| 771 |
a87: i64 |
| 772 |
a88: i64 |
| 773 |
a89: i64 |
| 774 |
a90: i64 |
| 775 |
a91: i64 |
| 776 |
a92: i64 |
| 777 |
a93: i64 |
| 778 |
a94: i64 |
| 779 |
a95: i64 |
| 780 |
a96: i64 |
| 781 |
a97: i64 |
| 782 |
a98: i64 |
| 783 |
a99: i64 |
| 784 |
a100: i64 |
| 785 |
|
| 786 |
def test_many_uninitialized_attributes() -> None: |
| 787 |
o = ManyUninit() |
| 788 |
with assertRaises(AttributeError): |
| 789 |
o.a1 |
| 790 |
with assertRaises(AttributeError): |
| 791 |
o.a10 |
| 792 |
with assertRaises(AttributeError): |
| 793 |
o.a20 |
| 794 |
with assertRaises(AttributeError): |
| 795 |
o.a30 |
| 796 |
with assertRaises(AttributeError): |
| 797 |
o.a31 |
| 798 |
with assertRaises(AttributeError): |
| 799 |
o.a32 |
| 800 |
with assertRaises(AttributeError): |
| 801 |
o.a33 |
| 802 |
with assertRaises(AttributeError): |
| 803 |
o.a40 |
| 804 |
with assertRaises(AttributeError): |
| 805 |
o.a50 |
| 806 |
with assertRaises(AttributeError): |
| 807 |
o.a60 |
| 808 |
with assertRaises(AttributeError): |
| 809 |
o.a62 |
| 810 |
with assertRaises(AttributeError): |
| 811 |
o.a63 |
| 812 |
with assertRaises(AttributeError): |
| 813 |
o.a64 |
| 814 |
with assertRaises(AttributeError): |
| 815 |
o.a65 |
| 816 |
with assertRaises(AttributeError): |
| 817 |
o.a80 |
| 818 |
with assertRaises(AttributeError): |
| 819 |
o.a100 |
| 820 |
o.a30 = MAGIC |
| 821 |
assert o.a30 == MAGIC |
| 822 |
o.a31 = MAGIC |
| 823 |
assert o.a31 == MAGIC |
| 824 |
o.a32 = MAGIC |
| 825 |
assert o.a32 == MAGIC |
| 826 |
o.a33 = MAGIC |
| 827 |
assert o.a33 == MAGIC |
| 828 |
with assertRaises(AttributeError): |
| 829 |
o.a34 |
| 830 |
o.a62 = MAGIC |
| 831 |
assert o.a62 == MAGIC |
| 832 |
o.a63 = MAGIC |
| 833 |
assert o.a63 == MAGIC |
| 834 |
o.a64 = MAGIC |
| 835 |
assert o.a64 == MAGIC |
| 836 |
o.a65 = MAGIC |
| 837 |
assert o.a65 == MAGIC |
| 838 |
with assertRaises(AttributeError): |
| 839 |
o.a66 |
| 840 |
|
| 841 |
class BaseNoBitmap: |
| 842 |
x: int = 5 |
| 843 |
|
| 844 |
class DerivedBitmap(BaseNoBitmap): |
| 845 |
# Subclass needs a bitmap, but base class doesn't have it. |
| 846 |
y: i64 |
| 847 |
|
| 848 |
def test_derived_adds_bitmap() -> None: |
| 849 |
d = DerivedBitmap() |
| 850 |
d.x = 643 |
| 851 |
b: BaseNoBitmap = d |
| 852 |
assert b.x == 643 |
| 853 |
|
| 854 |
class Delete: |
| 855 |
__deletable__ = ['x', 'y'] |
| 856 |
x: i64 |
| 857 |
y: i64 |
| 858 |
|
| 859 |
def test_del() -> None: |
| 860 |
o = Delete() |
| 861 |
o.x = MAGIC |
| 862 |
o.y = -1 |
| 863 |
assert o.x == MAGIC |
| 864 |
assert o.y == -1 |
| 865 |
del o.x |
| 866 |
with assertRaises(AttributeError): |
| 867 |
o.x |
| 868 |
assert o.y == -1 |
| 869 |
del o.y |
| 870 |
with assertRaises(AttributeError): |
| 871 |
o.y |
| 872 |
o.x = 5 |
| 873 |
assert o.x == 5 |
| 874 |
with assertRaises(AttributeError): |
| 875 |
o.y |
| 876 |
del o.x |
| 877 |
with assertRaises(AttributeError): |
| 878 |
o.x |
| 879 |
|
| 880 |
class UndefinedTuple: |
| 881 |
def __init__(self, x: i64, y: i64) -> None: |
| 882 |
if x != 0: |
| 883 |
self.t = (x, y) |
| 884 |
|
| 885 |
def test_undefined_native_int_tuple() -> None: |
| 886 |
o = UndefinedTuple(MAGIC, MAGIC) |
| 887 |
assert o.t[0] == MAGIC |
| 888 |
assert o.t[1] == MAGIC |
| 889 |
o = UndefinedTuple(0, 0) |
| 890 |
with assertRaises(AttributeError): |
| 891 |
o.t |
| 892 |
o = UndefinedTuple(-13, 45) |
| 893 |
assert o.t == (-13, 45) |
| 894 |
|
| 895 |
def test_undefined_native_int_tuple_via_any() -> None: |
| 896 |
cls: Any = UndefinedTuple |
| 897 |
o: Any = cls(MAGIC, MAGIC) |
| 898 |
assert o.t[0] == MAGIC |
| 899 |
assert o.t[1] == MAGIC |
| 900 |
o = cls(0, 0) |
| 901 |
with assertRaises(AttributeError): |
| 902 |
o.t |
| 903 |
o = UndefinedTuple(-13, 45) |
| 904 |
assert o.t == (-13, 45) |
| 905 |
|
| 906 |
[case testI64DefaultArgValues] |
| 907 |
from typing import Any, Final, Iterator, Tuple |
| 908 |
|
| 909 |
MAGIC: Final = -113 |
| 910 |
|
| 911 |
from mypy_extensions import i64 |
| 912 |
|
| 913 |
def f(x: i64, y: i64 = 5) -> i64: |
| 914 |
return x + y |
| 915 |
|
| 916 |
def test_simple_default_arg() -> None: |
| 917 |
assert f(3) == 8 |
| 918 |
assert f(4, 9) == 13 |
| 919 |
assert f(5, MAGIC) == -108 |
| 920 |
for i in range(-1000, 1000): |
| 921 |
assert f(1, i) == 1 + i |
| 922 |
f2: Any = f |
| 923 |
assert f2(3) == 8 |
| 924 |
assert f2(4, 9) == 13 |
| 925 |
assert f2(5, MAGIC) == -108 |
| 926 |
|
| 927 |
def g(a: i64, b: i64 = 1, c: int = 2, d: i64 = 3) -> i64: |
| 928 |
return a + b + c + d |
| 929 |
|
| 930 |
def test_two_default_args() -> None: |
| 931 |
assert g(10) == 16 |
| 932 |
assert g(10, 2) == 17 |
| 933 |
assert g(10, 2, 3) == 18 |
| 934 |
assert g(10, 2, 3, 4) == 19 |
| 935 |
g2: Any = g |
| 936 |
assert g2(10) == 16 |
| 937 |
assert g2(10, 2) == 17 |
| 938 |
assert g2(10, 2, 3) == 18 |
| 939 |
assert g2(10, 2, 3, 4) == 19 |
| 940 |
|
| 941 |
class C: |
| 942 |
def __init__(self) -> None: |
| 943 |
self.i: i64 = 1 |
| 944 |
|
| 945 |
def m(self, a: i64, b: i64 = 1, c: int = 2, d: i64 = 3) -> i64: |
| 946 |
return self.i + a + b + c + d |
| 947 |
|
| 948 |
class D(C): |
| 949 |
def m(self, a: i64, b: i64 = 2, c: int = 3, d: i64 = 4) -> i64: |
| 950 |
return self.i + a + b + c + d |
| 951 |
|
| 952 |
def mm(self, a: i64 = 2, b: i64 = 1) -> i64: |
| 953 |
return self.i + a + b |
| 954 |
|
| 955 |
@staticmethod |
| 956 |
def s(a: i64 = 2, b: i64 = 1) -> i64: |
| 957 |
return a + b |
| 958 |
|
| 959 |
@classmethod |
| 960 |
def c(cls, a: i64 = 2, b: i64 = 3) -> i64: |
| 961 |
assert cls is D |
| 962 |
return a + b |
| 963 |
|
| 964 |
def test_method_default_args() -> None: |
| 965 |
a = [C(), D()] |
| 966 |
assert a[0].m(4) == 11 |
| 967 |
d = D() |
| 968 |
assert d.mm() == 4 |
| 969 |
assert d.mm(5) == 7 |
| 970 |
assert d.mm(MAGIC) == MAGIC + 2 |
| 971 |
assert d.mm(b=5) == 8 |
| 972 |
assert D.mm(d) == 4 |
| 973 |
assert D.mm(d, 6) == 8 |
| 974 |
assert D.mm(d, MAGIC) == MAGIC + 2 |
| 975 |
assert D.mm(d, b=6) == 9 |
| 976 |
dd: Any = d |
| 977 |
assert dd.mm() == 4 |
| 978 |
assert dd.mm(5) == 7 |
| 979 |
assert dd.mm(MAGIC) == MAGIC + 2 |
| 980 |
assert dd.mm(b=5) == 8 |
| 981 |
|
| 982 |
def test_static_method_default_args() -> None: |
| 983 |
d = D() |
| 984 |
assert d.s() == 3 |
| 985 |
assert d.s(5) == 6 |
| 986 |
assert d.s(MAGIC) == MAGIC + 1 |
| 987 |
assert d.s(5, 6) == 11 |
| 988 |
assert D.s() == 3 |
| 989 |
assert D.s(5) == 6 |
| 990 |
assert D.s(MAGIC) == MAGIC + 1 |
| 991 |
assert D.s(5, 6) == 11 |
| 992 |
dd: Any = d |
| 993 |
assert dd.s() == 3 |
| 994 |
assert dd.s(5) == 6 |
| 995 |
assert dd.s(MAGIC) == MAGIC + 1 |
| 996 |
assert dd.s(5, 6) == 11 |
| 997 |
|
| 998 |
def test_class_method_default_args() -> None: |
| 999 |
d = D() |
| 1000 |
assert d.c() == 5 |
| 1001 |
assert d.c(5) == 8 |
| 1002 |
assert d.c(MAGIC) == MAGIC + 3 |
| 1003 |
assert d.c(b=5) == 7 |
| 1004 |
assert D.c() == 5 |
| 1005 |
assert D.c(5) == 8 |
| 1006 |
assert D.c(MAGIC) == MAGIC + 3 |
| 1007 |
assert D.c(b=5) == 7 |
| 1008 |
dd: Any = d |
| 1009 |
assert dd.c() == 5 |
| 1010 |
assert dd.c(5) == 8 |
| 1011 |
assert dd.c(MAGIC) == MAGIC + 3 |
| 1012 |
assert dd.c(b=5) == 7 |
| 1013 |
|
| 1014 |
class Init: |
| 1015 |
def __init__(self, x: i64 = 2, y: i64 = 5) -> None: |
| 1016 |
self.x = x |
| 1017 |
self.y = y |
| 1018 |
|
| 1019 |
def test_init_default_args() -> None: |
| 1020 |
o = Init() |
| 1021 |
assert o.x == 2 |
| 1022 |
assert o.y == 5 |
| 1023 |
o = Init(7, 8) |
| 1024 |
assert o.x == 7 |
| 1025 |
assert o.y == 8 |
| 1026 |
o = Init(4) |
| 1027 |
assert o.x == 4 |
| 1028 |
assert o.y == 5 |
| 1029 |
o = Init(MAGIC, MAGIC) |
| 1030 |
assert o.x == MAGIC |
| 1031 |
assert o.y == MAGIC |
| 1032 |
o = Init(3, MAGIC) |
| 1033 |
assert o.x == 3 |
| 1034 |
assert o.y == MAGIC |
| 1035 |
o = Init(MAGIC, 11) |
| 1036 |
assert o.x == MAGIC |
| 1037 |
assert o.y == 11 |
| 1038 |
o = Init(MAGIC) |
| 1039 |
assert o.x == MAGIC |
| 1040 |
assert o.y == 5 |
| 1041 |
o = Init(y=MAGIC) |
| 1042 |
assert o.x == 2 |
| 1043 |
assert o.y == MAGIC |
| 1044 |
|
| 1045 |
def kw_only(*, a: i64 = 1, b: int = 2, c: i64 = 3) -> i64: |
| 1046 |
return a + b + c * 2 |
| 1047 |
|
| 1048 |
def test_kw_only_default_args() -> None: |
| 1049 |
assert kw_only() == 9 |
| 1050 |
assert kw_only(a=2) == 10 |
| 1051 |
assert kw_only(b=4) == 11 |
| 1052 |
assert kw_only(c=11) == 25 |
| 1053 |
assert kw_only(a=2, c=4) == 12 |
| 1054 |
assert kw_only(c=4, a=2) == 12 |
| 1055 |
kw_only2: Any = kw_only |
| 1056 |
assert kw_only2() == 9 |
| 1057 |
assert kw_only2(a=2) == 10 |
| 1058 |
assert kw_only2(b=4) == 11 |
| 1059 |
assert kw_only2(c=11) == 25 |
| 1060 |
assert kw_only2(a=2, c=4) == 12 |
| 1061 |
assert kw_only2(c=4, a=2) == 12 |
| 1062 |
|
| 1063 |
def tuples(t: Tuple[i64, i64] = (MAGIC, MAGIC)) -> i64: |
| 1064 |
return t[0] + t[1] |
| 1065 |
|
| 1066 |
def test_tuple_arg_defaults() -> None: |
| 1067 |
assert tuples() == 2 * MAGIC |
| 1068 |
assert tuples((1, 2)) == 3 |
| 1069 |
assert tuples((MAGIC, MAGIC)) == 2 * MAGIC |
| 1070 |
tuples2: Any = tuples |
| 1071 |
assert tuples2() == 2 * MAGIC |
| 1072 |
assert tuples2((1, 2)) == 3 |
| 1073 |
assert tuples2((MAGIC, MAGIC)) == 2 * MAGIC |
| 1074 |
|
| 1075 |
class TupleInit: |
| 1076 |
def __init__(self, t: Tuple[i64, i64] = (MAGIC, MAGIC)) -> None: |
| 1077 |
self.t = t[0] + t[1] |
| 1078 |
|
| 1079 |
def test_tuple_init_arg_defaults() -> None: |
| 1080 |
assert TupleInit().t == 2 * MAGIC |
| 1081 |
assert TupleInit((1, 2)).t == 3 |
| 1082 |
assert TupleInit((MAGIC, MAGIC)).t == 2 * MAGIC |
| 1083 |
o: Any = TupleInit |
| 1084 |
assert o().t == 2 * MAGIC |
| 1085 |
assert o((1, 2)).t == 3 |
| 1086 |
assert o((MAGIC, MAGIC)).t == 2 * MAGIC |
| 1087 |
|
| 1088 |
def many_args( |
| 1089 |
a1: i64 = 0, |
| 1090 |
a2: i64 = 1, |
| 1091 |
a3: i64 = 2, |
| 1092 |
a4: i64 = 3, |
| 1093 |
a5: i64 = 4, |
| 1094 |
a6: i64 = 5, |
| 1095 |
a7: i64 = 6, |
| 1096 |
a8: i64 = 7, |
| 1097 |
a9: i64 = 8, |
| 1098 |
a10: i64 = 9, |
| 1099 |
a11: i64 = 10, |
| 1100 |
a12: i64 = 11, |
| 1101 |
a13: i64 = 12, |
| 1102 |
a14: i64 = 13, |
| 1103 |
a15: i64 = 14, |
| 1104 |
a16: i64 = 15, |
| 1105 |
a17: i64 = 16, |
| 1106 |
a18: i64 = 17, |
| 1107 |
a19: i64 = 18, |
| 1108 |
a20: i64 = 19, |
| 1109 |
a21: i64 = 20, |
| 1110 |
a22: i64 = 21, |
| 1111 |
a23: i64 = 22, |
| 1112 |
a24: i64 = 23, |
| 1113 |
a25: i64 = 24, |
| 1114 |
a26: i64 = 25, |
| 1115 |
a27: i64 = 26, |
| 1116 |
a28: i64 = 27, |
| 1117 |
a29: i64 = 28, |
| 1118 |
a30: i64 = 29, |
| 1119 |
a31: i64 = 30, |
| 1120 |
a32: i64 = 31, |
| 1121 |
a33: i64 = 32, |
| 1122 |
a34: i64 = 33, |
| 1123 |
) -> i64: |
| 1124 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15 + a16 + a17 + a18 + a19 + a20 + a21 + a22 + a23 + a24 + a25 + a26 + a27 + a28 + a29 + a30 + a31 + a32 + a33 + a34 |
| 1125 |
|
| 1126 |
def test_many_args() -> None: |
| 1127 |
assert many_args() == 561 |
| 1128 |
assert many_args(a1=100) == 661 |
| 1129 |
assert many_args(a2=101) == 661 |
| 1130 |
assert many_args(a15=114) == 661 |
| 1131 |
assert many_args(a31=130) == 661 |
| 1132 |
assert many_args(a32=131) == 661 |
| 1133 |
assert many_args(a33=232) == 761 |
| 1134 |
assert many_args(a34=333) == 861 |
| 1135 |
assert many_args(a1=100, a33=232) == 861 |
| 1136 |
f: Any = many_args |
| 1137 |
assert f() == 561 |
| 1138 |
assert f(a1=100) == 661 |
| 1139 |
assert f(a2=101) == 661 |
| 1140 |
assert f(a15=114) == 661 |
| 1141 |
assert f(a31=130) == 661 |
| 1142 |
assert f(a32=131) == 661 |
| 1143 |
assert f(a33=232) == 761 |
| 1144 |
assert f(a34=333) == 861 |
| 1145 |
assert f(a1=100, a33=232) == 861 |
| 1146 |
|
| 1147 |
def test_nested_function_defaults() -> None: |
| 1148 |
a: i64 = 1 |
| 1149 |
|
| 1150 |
def nested(x: i64 = 2, y: i64 = 3) -> i64: |
| 1151 |
return a + x + y |
| 1152 |
|
| 1153 |
assert nested() == 6 |
| 1154 |
assert nested(3) == 7 |
| 1155 |
assert nested(y=5) == 8 |
| 1156 |
assert nested(MAGIC) == MAGIC + 4 |
| 1157 |
a = 11 |
| 1158 |
assert nested() == 16 |
| 1159 |
|
| 1160 |
|
| 1161 |
def test_nested_function_defaults_via_any() -> None: |
| 1162 |
a: i64 = 1 |
| 1163 |
|
| 1164 |
def nested_native(x: i64 = 2, y: i64 = 3) -> i64: |
| 1165 |
return a + x + y |
| 1166 |
|
| 1167 |
nested: Any = nested_native |
| 1168 |
|
| 1169 |
assert nested() == 6 |
| 1170 |
assert nested(3) == 7 |
| 1171 |
assert nested(y=5) == 8 |
| 1172 |
assert nested(MAGIC) == MAGIC + 4 |
| 1173 |
a = 11 |
| 1174 |
assert nested() == 16 |
| 1175 |
|
| 1176 |
def gen(x: i64 = 1, y: i64 = 2) -> Iterator[i64]: |
| 1177 |
yield x + y |
| 1178 |
|
| 1179 |
def test_generator() -> None: |
| 1180 |
g = gen() |
| 1181 |
assert next(g) == 3 |
| 1182 |
g = gen(2) |
| 1183 |
assert next(g) == 4 |
| 1184 |
g = gen(2, 3) |
| 1185 |
assert next(g) == 5 |
| 1186 |
a: Any = gen |
| 1187 |
g = a() |
| 1188 |
assert next(g) == 3 |
| 1189 |
g = a(2) |
| 1190 |
assert next(g) == 4 |
| 1191 |
g = a(2, 3) |
| 1192 |
assert next(g) == 5 |
| 1193 |
|
| 1194 |
def magic_default(x: i64 = MAGIC) -> i64: |
| 1195 |
return x |
| 1196 |
|
| 1197 |
def test_magic_default() -> None: |
| 1198 |
assert magic_default() == MAGIC |
| 1199 |
assert magic_default(1) == 1 |
| 1200 |
assert magic_default(MAGIC) == MAGIC |
| 1201 |
a: Any = magic_default |
| 1202 |
assert a() == MAGIC |
| 1203 |
assert a(1) == 1 |
| 1204 |
assert a(MAGIC) == MAGIC |
| 1205 |
|
| 1206 |
[case testI64UndefinedLocal] |
| 1207 |
from typing import Final |
| 1208 |
|
| 1209 |
from mypy_extensions import i64, i32 |
| 1210 |
|
| 1211 |
from testutil import assertRaises |
| 1212 |
|
| 1213 |
MAGIC: Final = -113 |
| 1214 |
|
| 1215 |
|
| 1216 |
def test_conditionally_defined_local() -> None: |
| 1217 |
x = not int() |
| 1218 |
if x: |
| 1219 |
y: i64 = 5 |
| 1220 |
z: i32 = 6 |
| 1221 |
assert y == 5 |
| 1222 |
assert z == 6 |
| 1223 |
|
| 1224 |
def test_conditionally_undefined_local() -> None: |
| 1225 |
x = int() |
| 1226 |
if x: |
| 1227 |
y: i64 = 5 |
| 1228 |
z: i32 = 6 |
| 1229 |
else: |
| 1230 |
ok: i64 = 7 |
| 1231 |
assert ok == 7 |
| 1232 |
try: |
| 1233 |
print(y) |
| 1234 |
except NameError as e: |
| 1235 |
assert str(e) == 'local variable "y" referenced before assignment' |
| 1236 |
else: |
| 1237 |
assert False |
| 1238 |
try: |
| 1239 |
print(z) |
| 1240 |
except NameError as e: |
| 1241 |
assert str(e) == 'local variable "z" referenced before assignment' |
| 1242 |
else: |
| 1243 |
assert False |
| 1244 |
|
| 1245 |
def test_assign_error_value_conditionally() -> None: |
| 1246 |
x = int() |
| 1247 |
if not x: |
| 1248 |
y: i64 = MAGIC |
| 1249 |
z: i32 = MAGIC |
| 1250 |
assert y == MAGIC |
| 1251 |
assert z == MAGIC |
| 1252 |
|
| 1253 |
def tuple_case(x: i64, y: i64) -> None: |
| 1254 |
if not int(): |
| 1255 |
t = (x, y) |
| 1256 |
assert t == (x, y) |
| 1257 |
if int(): |
| 1258 |
t2 = (x, y) |
| 1259 |
try: |
| 1260 |
print(t2) |
| 1261 |
except NameError as e: |
| 1262 |
assert str(e) == 'local variable "t2" referenced before assignment' |
| 1263 |
else: |
| 1264 |
assert False |
| 1265 |
|
| 1266 |
def test_conditionally_undefined_tuple() -> None: |
| 1267 |
tuple_case(2, 3) |
| 1268 |
tuple_case(-2, -3) |
| 1269 |
tuple_case(MAGIC, MAGIC) |
| 1270 |
|
| 1271 |
def test_many_locals() -> None: |
| 1272 |
x = int() |
| 1273 |
if x: |
| 1274 |
a0: i64 = 0 |
| 1275 |
a1: i64 = 1 |
| 1276 |
a2: i64 = 2 |
| 1277 |
a3: i64 = 3 |
| 1278 |
a4: i64 = 4 |
| 1279 |
a5: i64 = 5 |
| 1280 |
a6: i64 = 6 |
| 1281 |
a7: i64 = 7 |
| 1282 |
a8: i64 = 8 |
| 1283 |
a9: i64 = 9 |
| 1284 |
a10: i64 = 10 |
| 1285 |
a11: i64 = 11 |
| 1286 |
a12: i64 = 12 |
| 1287 |
a13: i64 = 13 |
| 1288 |
a14: i64 = 14 |
| 1289 |
a15: i64 = 15 |
| 1290 |
a16: i64 = 16 |
| 1291 |
a17: i64 = 17 |
| 1292 |
a18: i64 = 18 |
| 1293 |
a19: i64 = 19 |
| 1294 |
a20: i64 = 20 |
| 1295 |
a21: i64 = 21 |
| 1296 |
a22: i64 = 22 |
| 1297 |
a23: i64 = 23 |
| 1298 |
a24: i64 = 24 |
| 1299 |
a25: i64 = 25 |
| 1300 |
a26: i64 = 26 |
| 1301 |
a27: i64 = 27 |
| 1302 |
a28: i64 = 28 |
| 1303 |
a29: i64 = 29 |
| 1304 |
a30: i64 = 30 |
| 1305 |
a31: i64 = 31 |
| 1306 |
a32: i64 = 32 |
| 1307 |
a33: i64 = 33 |
| 1308 |
with assertRaises(UnboundLocalError): |
| 1309 |
print(a0) |
| 1310 |
with assertRaises(UnboundLocalError): |
| 1311 |
print(a31) |
| 1312 |
with assertRaises(UnboundLocalError): |
| 1313 |
print(a32) |
| 1314 |
with assertRaises(UnboundLocalError): |
| 1315 |
print(a33) |
| 1316 |
a0 = 5 |
| 1317 |
assert a0 == 5 |
| 1318 |
with assertRaises(UnboundLocalError): |
| 1319 |
print(a31) |
| 1320 |
with assertRaises(UnboundLocalError): |
| 1321 |
print(a32) |
| 1322 |
with assertRaises(UnboundLocalError): |
| 1323 |
print(a33) |
| 1324 |
a32 = 55 |
| 1325 |
assert a0 == 5 |
| 1326 |
assert a32 == 55 |
| 1327 |
with assertRaises(UnboundLocalError): |
| 1328 |
print(a31) |
| 1329 |
with assertRaises(UnboundLocalError): |
| 1330 |
print(a33) |
| 1331 |
a31 = 10 |
| 1332 |
a33 = 20 |
| 1333 |
assert a0 == 5 |
| 1334 |
assert a31 == 10 |
| 1335 |
assert a32 == 55 |
| 1336 |
assert a33 == 20 |
| 1337 |
|
| 1338 |
[case testI64GlueMethodsAndInheritance] |
| 1339 |
from typing import Final, Any |
| 1340 |
|
| 1341 |
from mypy_extensions import i64, trait |
| 1342 |
|
| 1343 |
from testutil import assertRaises |
| 1344 |
|
| 1345 |
MAGIC: Final = -113 |
| 1346 |
|
| 1347 |
class Base: |
| 1348 |
def foo(self) -> i64: |
| 1349 |
return 5 |
| 1350 |
|
| 1351 |
def bar(self, x: i64 = 2) -> i64: |
| 1352 |
return x + 1 |
| 1353 |
|
| 1354 |
def hoho(self, x: i64) -> i64: |
| 1355 |
return x - 1 |
| 1356 |
|
| 1357 |
class Derived(Base): |
| 1358 |
def foo(self, x: i64 = 5) -> i64: |
| 1359 |
return x + 10 |
| 1360 |
|
| 1361 |
def bar(self, x: i64 = 3, y: i64 = 20) -> i64: |
| 1362 |
return x + y + 2 |
| 1363 |
|
| 1364 |
def hoho(self, x: i64 = 7) -> i64: |
| 1365 |
return x - 2 |
| 1366 |
|
| 1367 |
def test_derived_adds_bitmap() -> None: |
| 1368 |
b: Base = Derived() |
| 1369 |
assert b.foo() == 15 |
| 1370 |
|
| 1371 |
def test_derived_adds_another_default_arg() -> None: |
| 1372 |
b: Base = Derived() |
| 1373 |
assert b.bar() == 25 |
| 1374 |
assert b.bar(1) == 23 |
| 1375 |
assert b.bar(MAGIC) == MAGIC + 22 |
| 1376 |
|
| 1377 |
def test_derived_switches_arg_to_have_default() -> None: |
| 1378 |
b: Base = Derived() |
| 1379 |
assert b.hoho(5) == 3 |
| 1380 |
assert b.hoho(MAGIC) == MAGIC - 2 |
| 1381 |
|
| 1382 |
@trait |
| 1383 |
class T: |
| 1384 |
@property |
| 1385 |
def x(self) -> i64: ... |
| 1386 |
@property |
| 1387 |
def y(self) -> i64: ... |
| 1388 |
|
| 1389 |
class C(T): |
| 1390 |
x: i64 = 1 |
| 1391 |
y: i64 = 4 |
| 1392 |
|
| 1393 |
def test_read_only_property_in_trait_implemented_as_attribute() -> None: |
| 1394 |
c = C() |
| 1395 |
c.x = 5 |
| 1396 |
assert c.x == 5 |
| 1397 |
c.x = MAGIC |
| 1398 |
assert c.x == MAGIC |
| 1399 |
assert c.y == 4 |
| 1400 |
c.y = 6 |
| 1401 |
assert c.y == 6 |
| 1402 |
t: T = C() |
| 1403 |
assert t.y == 4 |
| 1404 |
t = c |
| 1405 |
assert t.x == MAGIC |
| 1406 |
c.x = 55 |
| 1407 |
assert t.x == 55 |
| 1408 |
assert t.y == 6 |
| 1409 |
a: Any = c |
| 1410 |
assert a.x == 55 |
| 1411 |
assert a.y == 6 |
| 1412 |
a.x = 7 |
| 1413 |
a.y = 8 |
| 1414 |
assert a.x == 7 |
| 1415 |
assert a.y == 8 |
| 1416 |
|
| 1417 |
class D(T): |
| 1418 |
xx: i64 |
| 1419 |
|
| 1420 |
@property |
| 1421 |
def x(self) -> i64: |
| 1422 |
return self.xx |
| 1423 |
|
| 1424 |
@property |
| 1425 |
def y(self) -> i64: |
| 1426 |
raise TypeError |
| 1427 |
|
| 1428 |
def test_read_only_property_in_trait_implemented_as_property() -> None: |
| 1429 |
d = D() |
| 1430 |
d.xx = 5 |
| 1431 |
assert d.x == 5 |
| 1432 |
d.xx = MAGIC |
| 1433 |
assert d.x == MAGIC |
| 1434 |
with assertRaises(TypeError): |
| 1435 |
d.y |
| 1436 |
t: T = d |
| 1437 |
assert t.x == MAGIC |
| 1438 |
d.xx = 6 |
| 1439 |
assert t.x == 6 |
| 1440 |
with assertRaises(TypeError): |
| 1441 |
t.y |
| 1442 |
|
| 1443 |
@trait |
| 1444 |
class T2: |
| 1445 |
x: i64 |
| 1446 |
y: i64 |
| 1447 |
|
| 1448 |
class C2(T2): |
| 1449 |
pass |
| 1450 |
|
| 1451 |
def test_inherit_trait_attribute() -> None: |
| 1452 |
c = C2() |
| 1453 |
c.x = 5 |
| 1454 |
assert c.x == 5 |
| 1455 |
c.x = MAGIC |
| 1456 |
assert c.x == MAGIC |
| 1457 |
with assertRaises(AttributeError): |
| 1458 |
c.y |
| 1459 |
c.y = 6 |
| 1460 |
assert c.y == 6 |
| 1461 |
t: T2 = C2() |
| 1462 |
with assertRaises(AttributeError): |
| 1463 |
t.y |
| 1464 |
t = c |
| 1465 |
assert t.x == MAGIC |
| 1466 |
c.x = 55 |
| 1467 |
assert t.x == 55 |
| 1468 |
assert t.y == 6 |
| 1469 |
a: Any = c |
| 1470 |
assert a.x == 55 |
| 1471 |
assert a.y == 6 |
| 1472 |
a.x = 7 |
| 1473 |
a.y = 8 |
| 1474 |
assert a.x == 7 |
| 1475 |
assert a.y == 8 |
| 1476 |
|
| 1477 |
class D2(T2): |
| 1478 |
x: i64 |
| 1479 |
y: i64 = 4 |
| 1480 |
|
| 1481 |
def test_implement_trait_attribute() -> None: |
| 1482 |
d = D2() |
| 1483 |
d.x = 5 |
| 1484 |
assert d.x == 5 |
| 1485 |
d.x = MAGIC |
| 1486 |
assert d.x == MAGIC |
| 1487 |
assert d.y == 4 |
| 1488 |
d.y = 6 |
| 1489 |
assert d.y == 6 |
| 1490 |
t: T2 = D2() |
| 1491 |
assert t.y == 4 |
| 1492 |
t = d |
| 1493 |
assert t.x == MAGIC |
| 1494 |
d.x = 55 |
| 1495 |
assert t.x == 55 |
| 1496 |
assert t.y == 6 |
| 1497 |
a: Any = d |
| 1498 |
assert a.x == 55 |
| 1499 |
assert a.y == 6 |
| 1500 |
a.x = 7 |
| 1501 |
a.y = 8 |
| 1502 |
assert a.x == 7 |
| 1503 |
assert a.y == 8 |
| 1504 |
|
| 1505 |
class DunderErr: |
| 1506 |
def __contains__(self, i: i64) -> bool: |
| 1507 |
raise IndexError() |
| 1508 |
|
| 1509 |
def test_dunder_arg_check() -> None: |
| 1510 |
o: Any = DunderErr() |
| 1511 |
with assertRaises(TypeError): |
| 1512 |
'x' in o |
| 1513 |
with assertRaises(TypeError): |
| 1514 |
2**63 in o |
| 1515 |
with assertRaises(IndexError): |
| 1516 |
1 in o |