|
| 1 | +from decimal import Decimal |
| 2 | +from fractions import Fraction |
| 3 | +from typing import Any, NoReturn |
| 4 | +from typing_extensions import Literal, assert_type |
| 5 | + |
| 6 | +assert_type(pow(1, 0), Literal[1]) # See #7163 |
| 7 | +assert_type(pow(1, 0, None), Literal[1]) # See #7163 |
| 8 | +assert_type(pow(2, 4, 0), NoReturn) |
| 9 | +assert_type(pow(2, 4), int) |
| 10 | +assert_type(pow(4, 6, None), int) |
| 11 | +assert_type(pow(5, -7), float) |
| 12 | +assert_type(pow(2, 4, 5), int) # pow(<smallint>, <smallint>, <smallint>) |
| 13 | +assert_type(pow(2, 35, 3), int) # pow(<smallint>, <bigint>, <smallint>) |
| 14 | +assert_type(pow(4.6, 8), float) |
| 15 | +assert_type(pow(5.1, 4, None), float) |
| 16 | +assert_type(pow(complex(6), 6.2), complex) |
| 17 | +assert_type(pow(complex(9), 7.3, None), complex) |
| 18 | +assert_type(pow(Fraction(), 4, None), Fraction) |
| 19 | +assert_type(pow(Fraction(3, 7), complex(1, 8)), complex) |
| 20 | +assert_type(pow(complex(4, -8), Fraction(2, 3)), complex) |
| 21 | +assert_type(pow(Decimal("1.0"), Decimal("1.6")), Decimal) |
| 22 | +assert_type(pow(Decimal("1.0"), Decimal("1.0"), Decimal("1.0")), Decimal) |
| 23 | +assert_type(pow(Decimal("4.6"), 7, None), Decimal) |
| 24 | + |
| 25 | +# These would ideally be more precise, but `Any` is acceptable |
| 26 | +# They have to be `Any` due to the fact that type-checkers can't distinguish between positive and negative numbers for the second argument to `pow()` |
| 27 | +# |
| 28 | +# int for positive 2nd-arg, float otherwise |
| 29 | +assert_type(pow(4, 65), Any) |
| 30 | +assert_type(pow(2, -45), Any) |
| 31 | +assert_type(pow(3, 57, None), Any) |
| 32 | +# pow(<pos-float>, <pos-or-neg-float>) -> float |
| 33 | +# pow(<neg-float>, <pos-or-neg-float>) -> complex |
| 34 | +assert_type(pow(4.7, 7.4), Any) |
| 35 | +assert_type(pow(-9.8, 8.3), Any) |
| 36 | +assert_type(pow(-9.3, -88.2), Any) |
| 37 | +assert_type(pow(8.2, -9.8), Any) |
| 38 | +assert_type(pow(4.7, 9.2, None), Any) |
| 39 | +# See #7046 -- float for a positive 1st arg, complex otherwise |
| 40 | +assert_type((-2) ** 0.5, Any) |
0 commit comments