Skip to content

Commit

Permalink
fix problem with functools.singledispatch (#104) (#105)
Browse files Browse the repository at this point in the history
solves #104
  • Loading branch information
15r10nk authored Sep 14, 2024
1 parent da29a2d commit 065162a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dirty_equals/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

class DirtyEqualsMeta(ABCMeta):
def __eq__(self, other: Any) -> bool:
if self is other:
return True

# this is required as fancy things happen when creating generics which include equals checks, without it,
# we get some recursive errors
if self is DirtyEquals or other is Generic or other is Protocol:
Expand Down
23 changes: 22 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import platform
import pprint
from functools import singledispatch

import packaging.version
import pytest

from dirty_equals import Contains, IsApprox, IsInt, IsList, IsNegative, IsOneOf, IsPositive, IsStr
from dirty_equals import Contains, DirtyEquals, IsApprox, IsInt, IsList, IsNegative, IsOneOf, IsPositive, IsStr
from dirty_equals.version import VERSION


Expand Down Expand Up @@ -192,3 +193,23 @@ def test_is_one_of(value, dirty):

def test_version():
packaging.version.parse(VERSION)


def test_singledispatch():
@singledispatch
def dispatch(value):
return 'generic'

assert dispatch(IsStr()) == 'generic'

@dispatch.register
def _(value: DirtyEquals):
return 'DirtyEquals'

assert dispatch(IsStr()) == 'DirtyEquals'

@dispatch.register
def _(value: IsStr):
return 'IsStr'

assert dispatch(IsStr()) == 'IsStr'

0 comments on commit 065162a

Please sign in to comment.