Skip to content
Snippets Groups Projects
Commit dcfcd15b authored by Tobias Hangleiter's avatar Tobias Hangleiter
Browse files

Use proper type annotations for bounds

parent 13dc7907
No related branches found
No related tags found
1 merge request!102Feature: domains
......@@ -85,15 +85,15 @@ class ReciprocalBound(Bound[_RealT]):
class Bounded(Protocol[_RealT]):
"""An object with bounds."""
lower: _BoundT
upper: _BoundT
lower: Bound[_RealT]
upper: Bound[_RealT]
class Domain(Container[_RealT], Bounded[_RealT]):
"""A domain that constrains the allowed values a parameter can take."""
lower: _BoundT[_RealT]
upper: _BoundT[_RealT]
precision: int = 16
lower: Bound[_RealT]
upper: Bound[_RealT]
precision: int
def __lt__(self, value: _RealT) -> bool:
return self.round(self.max() - value) < 0
......@@ -137,8 +137,8 @@ class Domain(Container[_RealT], Bounded[_RealT]):
@dataclass
class Interval(Domain[_RealT], Bounded[_RealT], ABC):
"""An interval with lower and upper bounds."""
lower: _BoundT[_RealT] = field(default=Bound[_RealT](-inf, integral=False))
upper: _BoundT[_RealT] = field(default=Bound[_RealT](inf, integral=False))
lower: Bound[_RealT] = Bound[_RealT](default=-inf, integral=False)
upper: Bound[_RealT] = Bound[_RealT](default=+inf, integral=False)
precision: int = field(default=16, repr=False)
def __and__(self, other: object) -> Domain:
......@@ -165,8 +165,8 @@ class Interval(Domain[_RealT], Bounded[_RealT], ABC):
@dataclass
class DiscreteInterval(Interval[_IntegralT], Bounded[_IntegralT]):
"""A discrete interval that only allows integral values."""
lower: _BoundT[_IntegralT] = field(default=Bound[_IntegralT](-inf, integral=True))
upper: _BoundT[_IntegralT] = field(default=Bound[_IntegralT](inf, integral=True))
lower: Bound[_IntegralT] = Bound[_IntegralT](default=-inf, integral=True)
upper: Bound[_IntegralT] = Bound[_IntegralT](default=+inf, integral=True)
def __contains__(self, value: object) -> bool:
return isinstance(value, numbers.Integral) and super().__contains__(value)
......@@ -216,7 +216,7 @@ class ContinuousInterval(Interval[_RealT]):
@dataclass
class ReciprocalMixin:
numerator: _RealT = 1.0
numerator: _RealT
def _to_denominator(self, it):
result = set()
......@@ -231,8 +231,8 @@ class ReciprocalMixin:
@dataclass
class ReciprocalDiscreteInterval(DiscreteInterval[_RealT], ReciprocalMixin):
"""A rational interval that only allows integral denominators."""
lower: _BoundT[_IntegralT] = field(default=ReciprocalBound[_IntegralT](1, integral=True))
upper: _BoundT[_IntegralT] = field(default=ReciprocalBound[_IntegralT](inf, integral=True))
lower: Bound[_IntegralT] = ReciprocalBound[_IntegralT](default=1, integral=True)
upper: Bound[_IntegralT] = ReciprocalBound[_IntegralT](default=inf, integral=True)
numerator: _RealT = 1.0
def __post_init__(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment