-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6995c83
commit 4ffc811
Showing
13 changed files
with
196 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Custom Types | ||
|
||
::: dirty_equals._base.DirtyEquals | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
## Custom Type Example | ||
|
||
To demonstrate the use of custom types, we'll create a custom type that matches any even number. | ||
|
||
We won't inherit from [`IsNumeric`][dirty_equals.IsNumeric] in this case to keep the example simple. | ||
|
||
```py | ||
title="IsEven" | ||
from decimal import Decimal | ||
from typing import Any, Union | ||
from dirty_equals import IsOneOf | ||
from dirty_equals import DirtyEquals | ||
|
||
class IsEven(DirtyEquals[Union[int, float, Decimal]]): | ||
def equals(self, other: Any) -> bool: | ||
return other % 2 == 0 | ||
|
||
assert 2 == IsEven | ||
assert 3 != IsEven | ||
assert 'foobar' != IsEven | ||
assert 3 == IsEven | IsOneOf(3) | ||
``` | ||
|
||
There are a few advantages of inheriting from [`DirtyEquals`][dirty_equals.DirtyEquals] compared to just | ||
implementing your own class with an `__eq__` method: | ||
|
||
1. `TypeError` and `ValueError` in `equals` are caught and result in a not-equals result. | ||
2. A useful `__repr__` is generated, and modified if the `==` operation returns `True`, | ||
see [pytest compatibility](../usage.md#__repr__-and-pytest-compatibility) | ||
3. [boolean logic](../usage.md#boolean-logic) works out of the box | ||
4. [Uninitialised usage](../usage.md#initialised-vs-class-comparison) | ||
(`IsEven` rather than `IsEven()`) works out of the box |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Dictionary Types | ||
|
||
::: dirty_equals.IsDict | ||
|
||
::: dirty_equals.IsPartialDict | ||
|
||
::: dirty_equals.IsIgnoreDict | ||
|
||
::: dirty_equals.IsStrictDict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Numeric Types | ||
|
||
::: dirty_equals.IsInt | ||
rendering: | ||
merge_init_into_class: false | ||
separate_signature: false | ||
|
||
::: dirty_equals.IsFloat | ||
rendering: | ||
merge_init_into_class: false | ||
separate_signature: false | ||
|
||
::: dirty_equals.IsPositive | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNegative | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNonNegative | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNonPositive | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsPositiveInt | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNegativeInt | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsPositiveFloat | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNegativeFloat | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsApprox | ||
|
||
::: dirty_equals.IsNumber | ||
rendering: | ||
merge_init_into_class: false | ||
|
||
::: dirty_equals.IsNumeric |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Other Types | ||
|
||
::: dirty_equals.FunctionCheck | ||
|
||
::: dirty_equals.IsInstance | ||
|
||
::: dirty_equals.IsJson | ||
|
||
::: dirty_equals.IsUUID | ||
|
||
::: dirty_equals.AnyThing | ||
|
||
::: dirty_equals.IsOneOf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Sequence Types | ||
|
||
::: dirty_equals.IsListOrTuple | ||
|
||
::: dirty_equals.IsList | ||
|
||
::: dirty_equals.IsTuple | ||
|
||
::: dirty_equals.HasLen | ||
|
||
::: dirty_equals.Contains |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# String Types | ||
|
||
::: dirty_equals.IsAnyStr | ||
|
||
::: dirty_equals.IsStr | ||
|
||
::: dirty_equals.IsBytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.