Skip to content

Commit

Permalink
Fix inspect in consistent way and use py23_compat (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Oct 1, 2018
1 parent 18616e8 commit d1beb97
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
8 changes: 2 additions & 6 deletions napalm/base/mock.py
Expand Up @@ -17,9 +17,9 @@
from __future__ import unicode_literals

from napalm.base.base import NetworkDriver
from napalm.base.utils import py23_compat
import napalm.base.exceptions

import inspect
import json
import os
import re
Expand All @@ -28,10 +28,6 @@
from pydoc import locate


# inspect.getargspec deprecated in Python 3.5, use getfullargspec if available
inspect_getargspec = getattr(inspect, "getfullargspec", inspect.getargspec)


def raise_exception(result):
exc = locate(result["exception"])
if exc:
Expand All @@ -49,7 +45,7 @@ def is_mocked_method(method):

def mocked_method(path, name, count):
parent_method = getattr(NetworkDriver, name)
parent_method_args = inspect_getargspec(parent_method)
parent_method_args = py23_compat.argspec(parent_method)
modifier = 0 if 'self' not in parent_method_args.args else 1

def _mocked_method(*args, **kwargs):
Expand Down
12 changes: 4 additions & 8 deletions napalm/base/netmiko_helpers.py
Expand Up @@ -10,8 +10,7 @@
# License for the specific language governing permissions and limitations under
# the License.
from __future__ import unicode_literals
import inspect
import sys
from napalm.base.utils import py23_compat
from netmiko import BaseConnection


Expand All @@ -21,12 +20,9 @@ def netmiko_args(optional_args):
Return a dictionary of these optional args that will be passed into the Netmiko
ConnectHandler call.
"""
if sys.version_info < (3,):
# (args, varargs, keywords, defaults)
args, _, _, defaults = inspect.getargspec(BaseConnection.__init__)
else:
# (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)
args, _, _, defaults, _, _, _ = inspect.getfullargspec(BaseConnection.__init__)
fields = py23_compat.argspec(BaseConnection.__init__)
args = fields[0]
defaults = fields[3]

check_self = args.pop(0)
if check_self != 'self':
Expand Down
14 changes: 5 additions & 9 deletions napalm/base/test/getters.py
Expand Up @@ -19,15 +19,11 @@
from napalm.base import NetworkDriver

# text_type is 'unicode' for py2 and 'str' for py3
from napalm.base.utils.py23_compat import text_type
from napalm.base.utils.py23_compat import text_type, argspec

from napalm.base.test import conftest


# inspect.getargspec deprecated in Python 3.5, use getfullargspec if available
inspect_getargspec = getattr(inspect, "getfullargspec", inspect.getargspec)


def list_dicts_diff(prv, nxt):
"""Compare two lists of dicts."""
result = []
Expand Down Expand Up @@ -137,17 +133,17 @@ def test_method_signatures(self):
continue
try:
orig = getattr(NetworkDriver, attr)
orig_spec = inspect_getargspec(orig)
orig_spec = argspec(orig)
except AttributeError:
orig_spec = 'Method does not exist in napalm.base'
func_spec = inspect_getargspec(func)
func_spec = argspec(func)
if orig_spec != func_spec:
errors[attr] = (orig_spec, func_spec)

EXTRA_METHODS = ['__init__', ]
for method in EXTRA_METHODS:
orig_spec = inspect_getargspec(getattr(NetworkDriver, method))
func_spec = inspect_getargspec(getattr(cls, method))
orig_spec = argspec(getattr(NetworkDriver, method))
func_spec = argspec(getattr(cls, method))
if orig_spec != func_spec:
errors[attr] = (orig_spec, func_spec)

Expand Down
17 changes: 15 additions & 2 deletions napalm/base/utils/py23_compat.py
Expand Up @@ -3,13 +3,26 @@
from __future__ import unicode_literals

import sys
import inspect

PY2 = sys.version_info.major == 2
PY3 = sys.version_info.major == 3

if sys.version_info.major == 3:

def argspec(*args, **kwargs):
if PY2:
# (args, varargs, keywords, defaults)
return inspect.getargspec(*args, **kwargs)
elif PY3:
# (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)
return inspect.getfullargspec(*args, **kwargs)


if PY3:
string_types = (str,)
text_type = str
else:
elif PY2:
string_types = (basestring,) # noqa
text_type = unicode # noqa
else:
raise ValueError("Invalid version of Python dectected")

0 comments on commit d1beb97

Please sign in to comment.