Skip to content

Commit

Permalink
Raise a clear exception on functools.partial
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Jun 6, 2022
1 parent 6c45b81 commit 7392ec2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion test/modules/partial_function.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import functools
import pytest
import xmod


def base(a, b, c, d):
return a, b, c, d


xmod(functools.partial(base, 0, d=3))
8 changes: 6 additions & 2 deletions test/test_xmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ def test_decorator_with_parameter(self):
assert decorator_with_parameter.BAR == 99

def test_partial_function(self):
from .modules import partial_function
try:
from .modules import partial_function
except ValueError as e:
assert e.args == ('`name` parameter must be set',)
else:
assert False

assert partial_function(1, 2) == (0, 1, 2, 3)

COMMON = [
'__builtins__',
Expand Down
8 changes: 3 additions & 5 deletions xmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ def prop(k):
mutator(lambda v: setattr(extension, k, v)),
mutator(lambda: delattr(extension, k)),
)
if name is None:
if isinstance(extension, functools.partial):
name = extension.func.__module__
else:
name = extension.__module__
name = name or getattr(extension, '__module__', None)
if not name:
raise ValueError('`name` parameter must be set')

module = sys.modules[name]

Expand Down

0 comments on commit 7392ec2

Please sign in to comment.