10000 `Fraction.__new__` now accepts anything with `.as_integer_ratio()` by InSyncWithFoo · Pull Request #12994 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Fraction.__new__ now accepts anything with .as_integer_ratio() #12994

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 7 commits into from
Nov 12, 2024
Merged
Changes from 2 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
16 changes: 14 additions & 2 deletions stdlib/fractions.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
from collections.abc import Callable
from collections.abc import Callable, Iterable
from decimal import Decimal
from numbers import Integral, Rational, Real
from typing import Any, Literal, SupportsIndex, overload
from typing import Any, Literal, Protocol, SupportsIndex, TypeVar, overload
from typing_extensions import Self, TypeAlias

_ComparableNum: TypeAlias = int | float | Decimal | Real
Expand All @@ -20,11 +20,23 @@ else:
@overload
def gcd(a: Integral, b: Integral) -> Integral: ...

_IR = TypeVar("_IR", covariant=True)

class ConvertibleToIntegerRatio(Protocol[_IR]):
def as_integer_ratio(self) -> _IR: ...

class Fraction(Rational):
@overload
def __new__(cls, numerator: int | Rational = 0, denominator: int | Rational | None = None) -> Self: ...
@overload
def __new__(cls, value: float | Decimal | str, /) -> Self: ...

if sys.version_info >= (3, 14):
@overload
def __new__(cls, value: ConvertibleToIntegerRatio[tuple[int | Rational, int | Rational]]) -> Self: ...
@overload
def __new__(cls, value: ConvertibleToIntegerRatio[Iterable[int | Rational]]) -> Self: ...

@classmethod
def from_float(cls, f: float) -> Self: ...
@classmethod
Expand Down
Loading
0