Skip to content

Commit ae04fa1

Browse files
authored
fixed simple deprecation warnings (issue #61) (#63)
* fixed simple deprecation warnings * fixed the second assertRaisesRegex * deprecation fix that allows successful build in Python 2.7 for Xenial * added conditional for use of getargspec Python2.7 in Xenial * getfullargspec returns 7 values instead of 4 like getargspec
1 parent e3ceab6 commit ae04fa1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

osrf_pycommon/cli_utils/verb_pattern.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""API for implementing commands and verbs which used the verb pattern."""
1616

17+
import sys
1718
import inspect
1819

1920
import pkg_resources
@@ -41,18 +42,36 @@ def call_prepare_arguments(func, parser, sysargs=None):
4142
func_args = [parser]
4243
# If the provided function takes two arguments and args were given
4344
# also give the args to the function
44-
arguments, _, _, defaults = inspect.getargspec(func)
45+
46+
# Remove the following if condition and keep else condition once Xenial is
47+
# dropped
48+
if sys.version_info[0] < 3:
49+
arguments, _, _, defaults = inspect.getargspec(func)
50+
51+
else:
52+
arguments, _, _, defaults, _, _, _ = inspect.getfullargspec(func)
53+
4554
if arguments[0] == 'self':
4655
del arguments[0]
4756
if defaults:
4857
arguments = arguments[:-len(defaults)]
4958
if len(arguments) not in [1, 2]:
59+
# Remove the following if condition once Xenial is dropped
60+
if sys.version_info[0] < 3:
61+
raise ValueError("Given function '{0}' must have one or two "
62+
"parameters (excluding self), but got '{1}' "
63+
"parameters: '{2}'"
64+
.format(func.__name__,
65+
len(arguments),
66+
', '.join(inspect.getargspec(func)[0])))
67+
5068
raise ValueError("Given function '{0}' must have one or two "
5169
"parameters (excluding self), but got '{1}' "
5270
"parameters: '{2}'"
5371
.format(func.__name__,
5472
len(arguments),
55-
', '.join(inspect.getargspec(func)[0])))
73+
', '.join(inspect.getfullargspec(func)[0])))
74+
5675
if len(arguments) == 2:
5776
func_args.append(sysargs or [])
5877
return func(*func_args) or parser

tests/unit/test_cli_utils/test_verb_pattern.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import unittest
23

34
from osrf_pycommon.cli_utils import verb_pattern
@@ -66,8 +67,16 @@ def fake_prepare_arguments(self, parser, args, extra):
6667
return parser
6768

6869
f = Foo()
69-
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
70-
r = cpa(f.fake_prepare_arguments, None)
70+
71+
# Remove the following if condition and keep else condition once
72+
# Xenial is dropped
73+
if sys.version_info[0] < 3:
74+
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
75+
r = cpa(f.fake_prepare_arguments, None)
76+
77+
else:
78+
with self.assertRaisesRegex(ValueError, 'one or two parameters'):
79+
r = cpa(f.fake_prepare_arguments, None)
7180

7281
# Try with less than needed
7382
called = False
@@ -81,8 +90,16 @@ def fake_prepare_arguments(self):
8190
return 'Should not get here'
8291

8392
f = Foo()
84-
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
85-
r = cpa(f.fake_prepare_arguments, None)
93+
94+
# Remove the following if condition and keep else condition once
95+
# Xenial is dropped
96+
if sys.version_info[0] < 3:
97+
with self.assertRaisesRegexp(ValueError, 'one or two parameters'):
98+
r = cpa(f.fake_prepare_arguments, None)
99+
100+
else:
101+
with self.assertRaisesRegex(ValueError, 'one or two parameters'):
102+
r = cpa(f.fake_prepare_arguments, None)
86103

87104
# Try with additional optional argument
88105
called = False

0 commit comments

Comments
 (0)