Skip to content

Commit 986e9e3

Browse files
Fraction.__new__ now accepts anything with .as_integer_ratio() (#12994)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 074cef0 commit 986e9e3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

stdlib/fractions.pyi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from collections.abc import Callable
33
from decimal import Decimal
44
from numbers import Integral, Rational, Real
5-
from typing import Any, Literal, SupportsIndex, overload
5+
from typing import Any, Literal, Protocol, SupportsIndex, overload
66
from typing_extensions import Self, TypeAlias
77

88
_ComparableNum: TypeAlias = int | float | Decimal | Real
@@ -20,11 +20,19 @@ else:
2020
@overload
2121
def gcd(a: Integral, b: Integral) -> Integral: ...
2222

23+
class _ConvertibleToIntegerRatio(Protocol):
24+
def as_integer_ratio(self) -> tuple[int | Rational, int | Rational]: ...
25+
2326
class Fraction(Rational):
2427
@overload
2528
def __new__(cls, numerator: int | Rational = 0, denominator: int | Rational | None = None) -> Self: ...
2629
@overload
2730
def __new__(cls, value: float | Decimal | str, /) -> Self: ...
31+
32+
if sys.version_info >= (3, 14):
33+
@overload
34+
def __new__(cls, value: _ConvertibleToIntegerRatio) -> Self: ...
35+
2836
@classmethod
2937
def from_float(cls, f: float) -> Self: ...
3038
@classmethod

0 commit comments

Comments
 (0)