Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Fix linting and other code formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kotfic committed Oct 10, 2018
1 parent 4d27401 commit e8932e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
39 changes: 23 additions & 16 deletions girder_worker_utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@ def __init__(self, name, **kwargs):
for k, v in six.iteritems(kwargs):
setattr(self, k, v)


# No default value for this argument
class Arg(Argument): pass
class Arg(Argument):
pass


# Has a default argument for the value
class KWArg(Argument): pass
class Varargs(Argument): pass
class Kwargs(Argument): pass
class KWArg(Argument):
pass


class Varargs(Argument):
pass


class Kwargs(Argument):
pass
# class Return(Argument): pass


def _clean_function_doc(f):
doc = getdoc(f) or ''
if isinstance(doc, bytes):
doc = doc.decode('utf-8')
else:
doc = cleandoc(doc)
return doc


Expand Down Expand Up @@ -81,7 +90,7 @@ def __getitem__(self, key):
return self._construct_argument(
self._get_class(self._signature.parameters[key]), key)

def _construct_argument(self, cls, name, **kwargs):
def _construct_argument(self, parameter_cls, name):
p = self._signature.parameters[name]
metadata = {}

Expand All @@ -93,7 +102,7 @@ def _construct_argument(self, cls, name, **kwargs):

metadata.update(self._metadata.get(name, {}))

return cls(name, **metadata)
return parameter_cls(name, **metadata)

def _is_varargs(self, p):
return p.kind == Parameter.VAR_POSITIONAL
Expand Down Expand Up @@ -121,22 +130,20 @@ def _get_class(self, p):
else:
raise RuntimeError("Could not determine parameter type!")

def init_metadata(self, name):
if name not in self._metadata:
self._metadata[name] = {}

def set_metadata(self, name, key, value):
if name not in self._signature.parameters:
raise RuntimeError("{} is not a valid argument to this function!")

if name not in self._metadata:
self._metadata[name] = {}
self.init_metadata(name)

self._metadata[name][key] = value

@property
def arguments(self):
# Only return arguments if we've declared them as paramaters
# This prevents us from returning things like 'self' of bound
# methods (e.g. celery tasks) etc. This is a dubious design
# decision.
return [
self._construct_argument(
self._get_class(self._signature.parameters[name]), name)
Expand Down Expand Up @@ -170,7 +177,7 @@ def parameter(name, **kwargs):
raise TypeError('Expected argument name to be a string')

data_type = kwargs.get("data_type", None)
if data_type is not None and callable(data_type):
if callable(data_type):
kwargs['data_type'] = data_type(name, **kwargs)

def argument_wrapper(func):
Expand All @@ -184,7 +191,7 @@ def argument_wrapper(func):
# the full list of parameters that have been identified by the
# user (even if there is no actual metadata associated with
# the argument).
desc._metadata[name] = {}
desc.init_metadata(name)

for key, value in six.iteritems(kwargs):
desc.set_metadata(name, key, value)
Expand Down
31 changes: 14 additions & 17 deletions girder_worker_utils/tests/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

from girder_worker_utils import decorators
from girder_worker_utils import types
from girder_worker_utils.decorators import argument
from girder_worker_utils.decorators import (
argument,
parameter,
GWFuncDesc,
Varargs,
Kwargs,
Arg,
KWArg)


@argument('n', types.Integer, help='The element to return')
Expand Down Expand Up @@ -173,22 +180,8 @@ def test_unhandled_input_binding():
with pytest.raises(ValueError):
decorators.get_input_data(arg, {})



###########################


import six

from girder_worker_utils.decorators import parameter
from girder_worker_utils.decorators import (
GWFuncDesc,
Varargs,
Kwargs,
Arg,
KWArg)


def arg(a): pass # noqa
def varargs(*args): pass # noqa
def kwarg(a='test'): pass # noqa
Expand All @@ -205,7 +198,6 @@ def arg_kwarg_kwargs(a, b='test', **kwargs): pass # noqa
def arg_kwarg_varargs_kwargs(a, b='test', *args, **kwargs): pass # noqa



@pytest.mark.parametrize('func,classes', [
(arg, [Arg]),
(varargs, [Varargs]),
Expand Down Expand Up @@ -233,6 +225,7 @@ def test_GWFuncDesc_arguments_returns_expected_classes(func, classes):
arg_kwargs, kwarg_kwarg, kwarg_kwargs,
arg_kwarg_kwargs]


@pytest.mark.parametrize('func', no_varargs)
def test_GWFuncDesc_varargs_returns_None(func):
spec = GWFuncDesc(func)
Expand All @@ -242,6 +235,7 @@ def test_GWFuncDesc_varargs_returns_None(func):
with_varargs = [varargs, arg_varargs, kwarg_varargs,
arg_kwarg_varargs, arg_kwarg_varargs_kwargs]


@pytest.mark.parametrize('func', with_varargs)
def test_GWFuncDesc_varargs_returns_Vararg(func):
spec = GWFuncDesc(func)
Expand Down Expand Up @@ -284,6 +278,7 @@ def test_GWFuncDesc_keyword_args_correct_names(func, names):
assert isinstance(p, KWArg)
assert p.name == n


# TODO keyword_args returns None test
@pytest.mark.parametrize('func,defaults', [
(kwarg, ['test']),
Expand All @@ -302,7 +297,9 @@ def test_GWFuncDesc_keyword_args_have_defaults(func, defaults):
assert hasattr(p, 'default')
assert p.default == d

@pytest.mark.skip("Fix this to use API for accessing argument spec rather than using 'private' attribute")

@pytest.mark.skip("Fix this to use API for accessing argument "
"spec rather than using 'private' attribute")
def test_parameter_decorator_adds_metadata():
@parameter('a', test='TEST')
def arg(a):
Expand Down
17 changes: 10 additions & 7 deletions girder_worker_utils/tests/py3_decorators_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import pytest
import six
from girder_worker_utils.decorators import (
GWFuncDesc,
Varargs,
Kwargs,
# Kwargs,
Arg,
KWArg)

def arg_varargs_kwarg(a, *args, b='test'): pass
def arg_varargs_kwarg_no_default(a, *args, b): pass
def arg_emptyvarargs_kwarg(a, *, b='test'): pass
def arg_emptyvarargs_kwarg_no_default(a, *, b): pass
def arg_with_annotation(a: int): pass
# TODO write Kwargs test

def arg_varargs_kwarg(a, *args, b='test'): pass # noqa
def arg_varargs_kwarg_no_default(a, *args, b): pass # noqa
def arg_emptyvarargs_kwarg(a, *, b='test'): pass # noqa
def arg_emptyvarargs_kwarg_no_default(a, *, b): pass # noqa
def arg_with_annotation(a: int): pass # noqa


@pytest.mark.parametrize('func,classes', [
Expand Down Expand Up @@ -79,6 +80,7 @@ def test_GWFuncDesc_keyword_args_correct_names(func, names):

# TODO keyword_args returns None test


@pytest.mark.parametrize('func,defaults', [
(arg_varargs_kwarg, ['test']),
(arg_emptyvarargs_kwarg, ['test'])
Expand All @@ -90,6 +92,7 @@ def test_GWFuncDesc_keyword_args_have_defaults(func, defaults):
assert hasattr(p, 'default')
assert p.default == d


@pytest.mark.parametrize('func', [
arg_varargs_kwarg_no_default,
arg_emptyvarargs_kwarg_no_default
Expand Down

0 comments on commit e8932e1

Please sign in to comment.