8000 Python 3.14: bump fractions stubs by hrimov · Pull Request #14215 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Python 3.14: bump fractions stubs #14215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions stdlib/@tests/stubtest_allowlists/py314.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ compression.gzip.GzipFile.readinto
compression.gzip.GzipFile.readinto1
compression.gzip.GzipFile.readinto1
compression.gzip.compress
fractions.Fraction.__pow__
fractions.Fraction.__rpow__
gzip.GzipFile.readinto
gzip.GzipFile.readinto1
gzip.compress
Expand Down
16 changes: 12 additions & 4 deletions stdlib/@tests/test_cases/builtins/check_pow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from decimal import Decimal
from fractions import Fraction
from typing import Any, Literal
Expand Down Expand Up @@ -47,9 +48,6 @@
assert_type(complex(6) ** 6.2, complex)
assert_type(pow(complex(9), 7.3, None), complex)

# pyright infers Fraction | float | complex, while mypy infers Fraction.
# This is probably because of differences in @overload handling.
assert_type(pow(Fraction(), 4, None), Fraction) # pyright: ignore[reportAssertTypeFailure]
assert_type(Fraction() ** 4, Fraction)

assert_type(pow(Fraction(3, 7), complex(1, 8)), complex)
Expand Down Expand Up @@ -85,12 +83,22 @@
# See #7046 -- float for a positive 1st arg, complex otherwise
assert_type((-95) ** 8.42, Any)

# Fraction.__pow__/__rpow__ with modulo parameter
# With the None parameter, we get the correct type, but with a non-None parameter, we receive TypeError
if sys.version_info >= (3, 14):
# pyright infers Fraction | float | complex, while mypy infers Fraction.
# This is probably because of differences in @overload handling.
assert_type(pow(Fraction(3, 4), 2, None), Fraction) # pyright: ignore[reportAssertTypeFailure]
# Non-none modulo should fail
pow(Fraction(3, 4), 2, 1) # type: ignore[misc]
else:
pow(Fraction(), 5, 8) # type: ignore

# All of the following cases should fail a type-checker.
pow(1.9, 4, 6) # type: ignore
pow(4, 7, 4.32) # type: ignore
pow(6.2, 5.9, 73) # type: ignore
pow(complex(6), 6.2, 7) # type: ignore
pow(Fraction(), 5, 8) # type: ignore
Decimal("8.7") ** 3.14 # type: ignore

# TODO: This fails at runtime, but currently passes mypy and pyright:
Expand Down
35 changes: 25 additions & 10 deletions stdlib/fractions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,31 @@ class Fraction(Rational):
def __rdivmod__(a, b: int | Fraction) -> tuple[int, Fraction]: ...
@overload
def __rdivmod__(a, b: float) -> tuple[float, Fraction]: ...
@overload
def __pow__(a, b: int) -> Fraction: ...
@overload
def __pow__(a, b: float | Fraction) -> float: ...
@overload
def __pow__(a, b: complex) -> complex: ...
@overload
def __rpow__(b, a: float | Fraction) -> float: ...
@overload
def __rpow__(b, a: complex) -> complex: ...
if sys.version_info >= (3, 14):
@overload
def __pow__(a, b: int, modulo: None = None) -> Fraction: ...
@overload
def __pow__(a, b: float | Fraction, modulo: None = None) -> float: ...
@overload
def __pow__(a, b: complex, modulo: None = None) -> complex: ...
else:
@overload
def __pow__(a, b: int) -> Fraction: ...
@overload
def __pow__(a, b: float | Fraction) -> float: ...
@overload
def __pow__(a, b: complex) -> complex: ...
if sys.version_info >= (3, 14):
@overload
def __rpow__(b, a: float | Fraction, modulo: None = None) -> float: ...
@overload
def __rpow__(b, a: complex, modulo: None = None) -> complex: ...
else:
@overload
def __rpow__(b, a: float | Fraction) -> float: ...
@overload
def __rpow__(b, a: complex) -> complex: ...

def __pos__(a) -> Fraction: ...
def __neg__(a) -> Fraction: ...
def __abs__(a) -> Fraction: ...
Expand Down
0