Skip to content
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

FloatInf & FloatNan #57

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions dirty_equals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from ._numeric import (
IsApprox,
IsFloat,
IsFloatInf,
IsFloatInfNeg,
IsFloatInfPos,
IsFloatNan,
IsInt,
IsNegative,
IsNegativeFloat,
Expand Down Expand Up @@ -61,6 +65,10 @@
'IsFloat',
'IsPositiveFloat',
'IsNegativeFloat',
'IsFloatInf',
'IsFloatInfNeg',
'IsFloatInfPos',
'IsFloatNan',
# inspection
'HasAttributes',
'HasName',
Expand Down
90 changes: 90 additions & 0 deletions dirty_equals/_numeric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
from datetime import date, datetime, timedelta
from decimal import Decimal
from typing import Any, Optional, Tuple, Type, TypeVar, Union
Expand Down Expand Up @@ -389,3 +390,92 @@ class IsNegativeFloat(IsFloat):
def __init__(self) -> None:
super().__init__(lt=0)
self._repr_kwargs = {}


class IsFloatInf(IsFloat):
"""
Checks that a value is float and infinite (positive or negative).

Inherits from [`IsFloat`][dirty_equals.IsFloat].

```py title="IsFloatInf"
from dirty_equals import IsFloatInf

assert float('inf') == IsFloatInf
assert float('-inf') == IsFloatInf
assert 1.0 != IsFloatInf
```
"""

def equals(self, other: Any) -> bool:
other = self.prepare(other)
return math.isinf(other)


class IsFloatInfPos(IsFloatInf):
"""
Checks that a value is float and positive infinite.

Inherits from [`IsFloatInf`][dirty_equals.IsFloatInf].

```py title="IsFloatInfPos"
from dirty_equals import IsFloatInfPos

assert float('inf') == IsFloatInfPos
assert -float('-inf') == IsFloatInfPos
assert -float('inf') != IsFloatInfPos
assert float('-inf') != IsFloatInfPos
```
"""

def __init__(self) -> None:
super().__init__(gt=0)
self._repr_kwargs = {}

def equals(self, other: Any) -> bool:

return self.bounds_checks(other) and super().equals(other)


class IsFloatInfNeg(IsFloatInf):
"""
Checks that a value is float and negative infinite.

Inherits from [`IsFloatInf`][dirty_equals.IsFloatInf].

```py title="IsFloatInfNeg"
from dirty_equals import IsFloatInfNeg

assert -float('inf') == IsFloatInfNeg
assert float('-inf') == IsFloatInfNeg
assert float('inf') != IsFloatInfNeg
assert -float('-inf') != IsFloatInfNeg
```
"""

def __init__(self) -> None:
super().__init__(lt=0)
self._repr_kwargs = {}

def equals(self, other: Any) -> bool:

return self.bounds_checks(other) and super().equals(other)


class IsFloatNan(IsFloat):
"""
Checks that a value is float and nan (not a number).

Inherits from [`IsFloat`][dirty_equals.IsFloat].

```py title="IsFloatNan"
from dirty_equals import IsFloatNan

assert float('nan') == IsFloatNan
assert 1.0 != IsFloatNan
```
"""

def equals(self, other: Any) -> bool:
other = self.prepare(other)
return math.isnan(other)
16 changes: 16 additions & 0 deletions docs/types/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
rendering:
merge_init_into_class: false

::: dirty_equals.IsFloatInf
rendering:
merge_init_into_class: false

::: dirty_equals.IsFloatInfPos
rendering:
merge_init_into_class: false

::: dirty_equals.IsFloatInfNeg
rendering:
merge_init_into_class: false

::: dirty_equals.IsFloatNan
rendering:
merge_init_into_class: false

::: dirty_equals.IsApprox

::: dirty_equals.IsNumber
Expand Down
22 changes: 22 additions & 0 deletions tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from dirty_equals import (
IsApprox,
IsFloat,
IsFloatInf,
IsFloatInfNeg,
IsFloatInfPos,
IsFloatNan,
IsInt,
IsNegative,
IsNegativeFloat,
Expand Down Expand Up @@ -47,6 +51,16 @@
(-1.0, IsNonPositive),
(-1, IsNonPositive & IsInt),
(1, IsNonNegative & IsInt),
(float('inf'), IsFloatInf),
(-float('inf'), IsFloatInf),
(float('-inf'), IsFloatInf),
(float('inf'), IsFloatInfPos),
(-float('-inf'), IsFloatInfPos),
(-float('inf'), IsFloatInfNeg),
(float('-inf'), IsFloatInfNeg),
(float('nan'), IsFloatNan),
(-float('nan'), IsFloatNan),
(float('-nan'), IsFloatNan),
],
)
def test_dirty_equals(other, dirty):
Expand Down Expand Up @@ -82,6 +96,14 @@ def test_dirty_equals(other, dirty):
(1.0, IsNonPositive),
(-1.0, IsNonPositive & IsInt),
(1.0, IsNonNegative & IsInt),
(1, IsFloatNan),
(1.0, IsFloatNan),
(1, IsFloatInf),
(1.0, IsFloatInf),
(-float('inf'), IsFloatInfPos),
(float('-inf'), IsFloatInfPos),
(-float('-inf'), IsFloatInfNeg),
(-float('-inf'), IsFloatInfNeg),
],
)
def test_dirty_not_equals(other, dirty):
Expand Down