Skip to content

Commit

Permalink
Fix methods with annotations on py3
Browse files Browse the repository at this point in the history
  • Loading branch information
akatrevorjay committed Jan 2, 2017
1 parent 33b4872 commit a3c0a37
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions mainline/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from mainline.exceptions import DiError
from mainline.utils import OBJECT_INIT, classproperty

try:
from inspect import FullArgSpec as ArgSpec
from inspect import getfullargspec as getargspec
except ImportError:
from inspect import ArgSpec, getargspec


class Injector(object):
def __init__(self, di):
Expand Down Expand Up @@ -52,12 +58,11 @@ def __call__(self, wrapped):
class SpecInjector(CallableInjector):
def decorate(self, wrapped):
# Remove the number of args from the wrapped function's argspec
spec = inspect.getargspec(wrapped)
spec = getargspec(wrapped)
new_args = spec.args[len(self.args):]
spec.keywords

# Update argspec
spec = inspect.ArgSpec(new_args, *spec[1:])
spec = ArgSpec(new_args, *spec[1:])

@wrapt.decorator(adapter=spec)
def decorator(wrapped, instance, args, kwargs):
Expand All @@ -76,7 +81,7 @@ def decorator(wrapped, instance, args, kwargs):

class AutoSpecInjector(CallableInjector):
def decorate(self, wrapped):
spec = inspect.getargspec(wrapped)
spec = getargspec(wrapped)

def decorator(*args, **kwargs):
if self.injectables:
Expand All @@ -93,7 +98,12 @@ def decorator(*args, **kwargs):
obj = self.di.resolve(arg)
injected_args.append(obj)
else:
injected_args.append(args[args_cur_index])
try:
injected_args.append(args[args_cur_index])
except IndexError:
# This means there aren't enough args given, which means this will most likely result in
# TypeError. Just let that happen, python is an adults only playground ;)
continue
args_cur_index += 1
remaining_args = args[args_cur_index:]
if remaining_args:
Expand Down

0 comments on commit a3c0a37

Please sign in to comment.