Skip to content

Commit

Permalink
Complete "Int.gcd" method
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Nov 13, 2024
1 parent 9da43b5 commit 73aa85c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions rithm/_hints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

from numbers import Rational as _Rational
from typing import TYPE_CHECKING, overload as _overload
from typing import (
SupportsIndex as _SupportsIndex,
TYPE_CHECKING,
overload as _overload,
)

from typing_extensions import Any as _Any, Self as _Self, final as _final

Expand Down Expand Up @@ -130,7 +134,7 @@ def numerator(self, /) -> _Self: ...

def bit_length(self, /) -> _Self: ...

def gcd(self, other: _Self, /) -> _Self: ...
def gcd(self, other: _SupportsIndex, /) -> _Self: ...

def is_power_of_two(self, /) -> bool: ...

Expand Down
6 changes: 3 additions & 3 deletions rithm/_rithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from numbers import Integral, Rational
from operator import mul
from sys import hash_info
from typing import Any, NoReturn, overload
from typing import Any, NoReturn, SupportsIndex, overload

from typing_extensions import Self, final

Expand All @@ -25,8 +25,8 @@ def numerator(self, /) -> Self:
def bit_length(self, /) -> Self:
return Int(self._value.bit_length())

def gcd(self, other: Self, /) -> Self:
return Int(_gcd(self._value, other._value))
def gcd(self, other: SupportsIndex, /) -> Self:
return Int(_gcd(self._value, other))

def is_power_of_two(self, /) -> bool:
return self._value > 0 and not (self._value & (self._value - 1))
Expand Down
4 changes: 2 additions & 2 deletions src/python_binding/py_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl PyInt {
}

#[pyo3(text_signature = "($self, other, /)")]
fn gcd(&self, other: &Self) -> PyInt {
Self((&self.0).gcd(&other.0))
fn gcd(&self, other: &Bound<'_, PyAny>) -> PyResult<PyInt> {
Ok(Self((&self.0).gcd(&try_big_int_from_py_integral(other)?)))
}

#[pyo3(signature = (endianness, /))]
Expand Down

0 comments on commit 73aa85c

Please sign in to comment.