Skip to content

Commit

Permalink
Add PY2 compat for getfullargspec (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hasier authored Feb 7, 2019
1 parent b2451c6 commit ffefc86
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion business_rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.4.1'
__version__ = '1.4.2'

from .engine import run_all, check_conditions_recursively
from .utils import export_rule_data, validate_rule_data
Expand Down
3 changes: 2 additions & 1 deletion business_rules/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .fields import FIELD_NO_INPUT
from .models import ConditionResult
from .util import method_type
from .util.compat import getfullargspec

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -230,7 +231,7 @@ def _build_variable_parameters(method, parameters, rule):


def _build_parameters(method, parameters, extra_parameters):
if inspect.getargspec(method).keywords is not None:
if getfullargspec(method).varkw is not None:
method_params = extra_parameters
else:
method_params = {}
Expand Down
33 changes: 33 additions & 0 deletions business_rules/util/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import absolute_import, unicode_literals

import sys
from collections import namedtuple

PY2 = sys.version_info[0] == 2

# Taken from https://github.com/HypothesisWorks/hypothesis/pull/625/files#diff-e84a85b835af44101e1986c47ba39630R264
if PY2:
FullArgSpec = namedtuple('FullArgSpec', 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')

def getfullargspec(func):
import inspect
args, varargs, varkw, defaults = inspect.getargspec(func)
return FullArgSpec(args, varargs, varkw, defaults, [], None, {})
else:
from inspect import getfullargspec

if sys.version_info[:2] == (3, 5):
# silence deprecation warnings on Python 3.5
# (un-deprecated in 3.6 to allow single-source 2/3 code like this)
def silence_warnings(func):
import warnings
import functools

@functools.wraps(func)
def inner(*args, **kwargs):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
return func(*args, **kwargs)
return inner

getfullargspec = silence_warnings(getfullargspec)
8 changes: 4 additions & 4 deletions tests/test_engine_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def test_check_operator_comparison(self):
# ##################################### #
def test_do_actions(self):
function_params_mock = MagicMock()
function_params_mock.keywords = None
with patch('inspect.getargspec', return_value=function_params_mock):
function_params_mock.varkw = None
with patch('business_rules.engine.getfullargspec', return_value=function_params_mock):
rule_actions = [
{
'name': 'action1'
Expand Down Expand Up @@ -232,8 +232,8 @@ def test_do_actions(self):

def test_do_actions_with_injected_parameters(self):
function_params_mock = MagicMock()
function_params_mock.keywords = True
with patch('inspect.getargspec', return_value=function_params_mock):
function_params_mock.varkw = True
with patch('business_rules.engine.getfullargspec', return_value=function_params_mock):
rule_actions = [
{
'name': 'action1'
Expand Down

0 comments on commit ffefc86

Please sign in to comment.