<commit_before># encoding: utf-8

from inspect import getargspec, ismethod

try:
    from inspect import getfullargspec as getargspec
except ImportError:
    pass


class CastExtension(object):
    """Typecast the arguments to your controllers using Python 3 function annotations."""

    provides = ['typecast']

    def __init__(self, config):
        super(CastExtension, self).__init__()

    def mutate(self, context, handler, args, kw):
        """Inspect and potentially mutate the given handler's arguments.

        The args list and kw dictionary may be freely modified, though invalid arguments to the handler will fail.
        """
        annotations = getattr(handler.__func__ if hasattr(handler, '__func__') else handler, '__annotations__', None)
        if not annotations:
            return 

        argspec = getargspec(handler)
        arglist = list(argspec.args)
        
        if arglist[0] == 'self':
            del arglist[0]

        for i, value in enumerate(list(args)):
            key = arglist[i]
            if key in annotations:
                args[i] = annotations[key](value)

        # Convert keyword arguments
        for key, value in list(kw.items()):
            if key in annotations:
                kw[key] = annotations[key](value)<commit_msg>Now detecting bound instance methods correctly.<commit_after># encoding: utf-8

from inspect import getargspec, ismethod

try:
    from inspect import getfullargspec as getargspec
except ImportError:
    pass


class CastExtension(object):
    """Typecast the arguments to your controllers using Python 3 function annotations."""

    provides = ['typecast']

    def __init__(self, config):
        super(CastExtension, self).__init__()

    def mutate(self, context, handler, args, kw):
        """Inspect and potentially mutate the given handler's arguments.

        The args list and kw dictionary may be freely modified, though invalid arguments to the handler will fail.
        """
        annotations = getattr(handler.__func__ if hasattr(handler, '__func__') else handler, '__annotations__', None)
        if not annotations:
            return 

        argspec = getargspec(handler)
        arglist = list(argspec.args)
        
        # This should handle Python 3's method of binding, and Python 2's.
        if hasattr(handler.__call__, '__self__') or hasattr(handler, '__self__'):
            del arglist[0]

        for i, value in enumerate(list(args)):
            key = arglist[i]
            if key in annotations:
                args[i] = annotations[key](value)

        # Convert keyword arguments
        for key, value in list(kw.items()):
            if key in annotations:
                kw[key] = annotations[key](value)
{'fraction_non_alphanumeric': 0.08703568827385287, 'fraction_numerical': 0.0032774945375091042, 'mean_word_length': 2.5721716514954487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4407015', 'n_tokens_mistral': 745, 'n_tokens_neox': 698, 'n_words': 256}