Skip to content

Commit

Permalink
Re-allow cursing dunder methods with non-functions. (clarete#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian authored and clarete committed Oct 1, 2019
1 parent 4c0c4c9 commit a282725
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion forbiddenfruit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _curse_special(klass, attr, func):
Curse one of the "dunder" methods, i.e. methods beginning with __ which have a
precial resolution code path
"""
assert isinstance(func, FunctionType)
assert callable(func)

@wraps(func)
def wrapper(*args, **kwargs):
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/test_forbidden_fruit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def almost_equal(a, b, e=0.001):

skip_legacy = nottest if sys.version_info < (3, 3) else istest

def test_cursing_a_builting_class():
def test_cursing_a_builtin_class():

# Given that I have a function that returns *blah*
def words_of_wisdom(self):
Expand All @@ -30,7 +30,7 @@ def words_of_wisdom(self):
assert 'words_of_wisdom' in dir(int)


def test_cursing_a_builting_class_with_a_class_method():
def test_cursing_a_builtin_class_with_a_class_method():

# Given that I have a function that returns *blah*
def hello(self):
Expand All @@ -44,6 +44,20 @@ def hello(self):
assert 'hello' in dir(str)


@skip_legacy
def test_cursing_a_builtin_class_dunder_with_a_random_callable():
# Given that I have an object that returns *blah*
class Twelver(object):
def __call__(self, one, two):
return 12

# When I try to curse a built-in class's __sub__ with that function
curse(str, "__sub__", Twelver())

# Then I see that the class was cursed
assert ("hello" - "world") == 12


def test_reversing_a_builtin():
# Given that I have a cursed object
curse(str, 'stuff', property(lambda s: s * 2))
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
envlist = py27, py30, py33, py34, py35, py36, py37

[testenv]
whitelist_externals = make
commands = make
deps =
nose
Expand Down

0 comments on commit a282725

Please sign in to comment.