Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmtroffaes committed Dec 16, 2024
1 parent 3584e4f commit 3aabcf1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
22 changes: 8 additions & 14 deletions cython/mytype_gmp.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,12 @@ cdef _get_mytype(mytype target):
# trick: bytes(buf_ptr) removes everything after the null
return Fraction(bytes(buf_ptr).decode('ascii'))

# set target to Python Fraction or Python int
# set target to Python Fraction (or any type with numerator and denominator attributes)
cdef _set_mytype(mytype target, value):
# https://peps.python.org/pep-0484/#the-numeric-tower
# numbers.Real and numbers.Rational are broken with mypy
# so check (Fraction, int) instead of numbers.Rational
if isinstance(value, (Fraction, int)):
try:
mpq_set_si(target, value.numerator, value.denominator)
except OverflowError:
# in case of overflow, set it using mpq_set_str
buf = str(value).encode('ascii')
if mpq_set_str(target, buf, 10) == -1:
raise ValueError('could not convert %s to mpq_t' % value)
else:
raise TypeError(f"must be Fraction or int, not {type(value).__name__}")
try:
mpq_set_si(target, value.numerator, value.denominator)
except OverflowError:
# in case of overflow, set it using mpq_set_str
buf = str(value).encode('ascii')
if mpq_set_str(target, buf, 10) == -1:
raise ValueError('could not convert %s to mpq_t' % value)
4 changes: 2 additions & 2 deletions docs/source/cddgmp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ instead of :class:`float`, and all arithmetic is exact.

.. warning::

Passing a :class:`float` will result in a :exc:`TypeError`:
Passing a :class:`float` will result in a :exc:`AttributeError`:

>>> cdd.gmp.matrix_from_array([[1.12]])
Traceback (most recent call last):
...
TypeError: must be Fraction or int, not float
AttributeError: 'float' object has no attribute 'numerator'

If the float represents a fraction, you must pass it as a fraction explicitly:

Expand Down
9 changes: 7 additions & 2 deletions src/cdd/gmp.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from collections.abc import Container, Sequence, Set
from fractions import Fraction
from typing import Optional, Union
from typing import Optional, Protocol

from cdd import LPObjType, LPSolverType, LPStatusType, RepType, RowOrderType

NumberType = Fraction
SupportsNumberType = Union[Fraction, int]

class SupportsNumberType(Protocol):
@property
def numerator(self) -> int: ...
@property
def denominator(self) -> int: ...

class LinProg:
@property
Expand Down
8 changes: 6 additions & 2 deletions test/gmp/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
def test_gmp_matrix_typing() -> None:
cdd.gmp.matrix_from_array([[1]])
cdd.gmp.matrix_from_array([[Fraction(1, 1)]])
with pytest.raises(TypeError, match="must be Fraction or int"):
with pytest.raises(
AttributeError, match="'float' object has no attribute 'numerator'"
):
cdd.gmp.matrix_from_array([[1.0]]) # type: ignore
with pytest.raises(TypeError, match="must be Fraction or int"):
with pytest.raises(
AttributeError, match="'str' object has no attribute 'numerator'"
):
cdd.gmp.matrix_from_array([["1"]]) # type: ignore
5 changes: 3 additions & 2 deletions test/gmp/test_numpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import numpy.typing as npt

import cdd
import cdd.gmp


Expand All @@ -13,6 +14,6 @@ def test_numpy() -> None:
)
mat = cdd.gmp.matrix_from_array(arr) # type: ignore
mat.rep_type = cdd.RepType.GENERATOR
cdd_poly = cdd.polyhedron_from_matrix(mat)
ineq = np.array(cdd.copy_inequalities(cdd_poly).array)
cdd_poly = cdd.gmp.polyhedron_from_matrix(mat)
ineq = np.array(cdd.gmp.copy_inequalities(cdd_poly).array)
assert ((ref_ineq - ineq) == 0).all()

0 comments on commit 3aabcf1

Please sign in to comment.