Skip to content

Commit

Permalink
Add 'same_method' compatibility wrapper as workaround for pydantic/py…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 3, 2023
1 parent e7b5780 commit 4293219
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 3 additions & 2 deletions inflect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@


from .compat.pydantic1 import validate_call
from .compat.pydantic import same_method


class UnknownClassicalModeError(Exception):
Expand Down Expand Up @@ -2604,12 +2605,12 @@ def _plequal(self, word1: str, word2: str, pl) -> Union[str, bool]: # noqa: C90
return "s:p"
self.classical_dict = classval.copy()

if pl == self.plural or pl == self.plural_noun:
if same_method(pl, self.plural) or same_method(pl, self.plural_noun):
if self._pl_check_plurals_N(word1, word2):
return "p:p"
if self._pl_check_plurals_N(word2, word1):
return "p:p"
if pl == self.plural or pl == self.plural_adj:
if same_method(pl, self.plural) or same_method(pl, self.plural_adj):
if self._pl_check_plurals_adj(word1, word2):
return "p:p"
return False
Expand Down
19 changes: 19 additions & 0 deletions inflect/compat/pydantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ValidateCallWrapperWrapper:
def __init__(self, wrapped):
self.orig = wrapped

def __eq__(self, other):
return self.raw_function == other.raw_function

@property
def raw_function(self):
return getattr(self.orig, 'raw_function') or self.orig


def same_method(m1, m2) -> bool:
"""
Return whether m1 and m2 are the same method.
Workaround for pydantic/pydantic#6390.
"""
return ValidateCallWrapperWrapper(m1) == ValidateCallWrapperWrapper(m2)
10 changes: 3 additions & 7 deletions tests/test_pwd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

import pytest
import pydantic

from inflect import (
BadChunkingOptionError,
Expand All @@ -11,6 +10,7 @@
UnknownClassicalModeError,
)
import inflect
from inflect.compat.pydantic import same_method


missing = object()
Expand Down Expand Up @@ -806,14 +806,10 @@ def test_a(self):
with pytest.raises(Exception):
p.a("")

@pytest.mark.xfail(
pydantic.version.VERSION >= "2",
reason="pydantic/pydantic#6390",
)
def test_a_and_an_same_method(self):
assert inflect.engine.a is inflect.engine.an
assert same_method(inflect.engine.a, inflect.engine.an)
p = inflect.engine()
assert p.a == p.an
assert same_method(p.a, p.an)

def test_no(self):
p = inflect.engine()
Expand Down

0 comments on commit 4293219

Please sign in to comment.