Esempio n. 1
0
 def __call__(self, obj):
     func = obj
     poks = []
     while isinstance(func, _PokTranslator):
         poks.append(func)
         func = func.func
     sig = _signatures.forged_signature(func)
     parameters = []
     to_use = self.to_use.copy()
     for name, parameter in sig.parameters.items():
         if name in self.annotations:
             parameters.append(parameter.replace(
                 annotation=self.annotations[name]))
             to_use.remove(name)
         else:
             parameters.append(parameter)
     if to_use:
         raise ValueError(
             'the following parameters to be annotated '
             'were not found in {0}: {1}'
             .format(func.__name__, ', '.join(to_use)))
     if self.ret is _util.UNSET:
         sig = sig.replace(parameters=parameters)
     else:
         sig = sig.replace(parameters=parameters,
                           return_annotation=self.ret)
     func.__signature__ = sig
     for pok in reversed(poks):
         pok._prepare()
     return obj
Esempio n. 2
0
def test_roundtrip(self, sig_str, old_fmt=None):
    sig = support.s(sig_str)
    p_sig_str = str(sig)
    try:
        self.assertEqual('(' + sig_str + ')', p_sig_str)
    except AssertionError:
        if old_fmt is None: raise
        self.assertEqual('(' + old_fmt + ')', p_sig_str)

    pf_sig_str = str(_signatures.forged_signature(support.func_from_sig(sig)))
    try:
        self.assertEqual('(' + sig_str + ')', pf_sig_str)
    except AssertionError:
        if old_fmt is None: raise
        self.assertEqual('(' + old_fmt + ')', pf_sig_str)
Esempio n. 3
0
    def _prepare(self):
        intersection = self.posoarg_names & self.kwoarg_names
        if intersection:
            raise ValueError(
                'Parameters marked as both positional-only and keyword-only: '
                + ' '.join(repr(name) for name in intersection))
        to_use = self.posoarg_names | self.kwoarg_names

        sig = _signatures.forged_signature(self.func)
        params = []
        kwoparams = []
        kwopos = self.kwopos = []
        found_pok = found_kws = False
        for i, param in enumerate(sig.parameters.values()):
            if param.kind == param.POSITIONAL_OR_KEYWORD:
                if param.name in self.posoarg_names:
                    if found_pok:
                        raise ValueError(
                            '{0.name!r} was requested to become a positional-'
                            'only parameter, but comes after a regular '
                            'parameter'.format(param))
                    params.append(
                        param.replace(kind=param.POSITIONAL_ONLY))
                    to_use.remove(param.name)
                elif param.name in self.kwoarg_names:
                    kwoparams.append(
                        param.replace(kind=param.KEYWORD_ONLY))
                    kwopos.append((i, param))
                    to_use.remove(param.name)
                else:
                    found_pok = True
                    params.append(param)
            else: # not a POK param
                if param.name in to_use:
                    raise ValueError(
                        '{0.name!r} is not of kind POSITIONAL_OR_KEYWORD, but:'
                        ' {0.kind}'.format(param))
                if param.kind == param.VAR_KEYWORD:
                    found_kws = True
                    params.extend(kwoparams)
                params.append(param)
        if not found_kws:
            params.extend(kwoparams)
        if to_use:
            raise ValueError("Parameters not found: " + ' '.join(to_use))
        self.__signature__ = sig.replace(parameters=params)
Esempio n. 4
0
def _kwoargs_start(start, _kwoargs, func, *args, **kwargs):
    kwoarg_names = set(_kwoargs)
    found = False
    sig = _signatures.forged_signature(func).parameters.values()
    for param in sig:
        if param.kind == param.POSITIONAL_OR_KEYWORD:
            if found or param.name == start:
                found = True
                kwoarg_names.add(param.name)
        elif param.kind != param.POSITIONAL_ONLY:
            break # no more POKs now
    if not found:
        raise ValueError('{0!r} not found in {1.__name__}{2}'.format(
            start, func, sig))
    return _PokTranslator(
        func, kwoargs=kwoarg_names,
        get=partial(_kwoargs_start, start, _kwoargs))
Esempio n. 5
0
def _autokwoargs(exceptions, func):
    sig = _signatures.forged_signature(func)
    args = []
    exceptions = set(exceptions)
    for param in sig.parameters.values():
        if (
                param.kind == param.POSITIONAL_OR_KEYWORD
                and param.default != param.empty
            ):
            try:
                exceptions.remove(param.name)
            except KeyError:
                args.append(param.name)
    if exceptions:
        raise ValueError(
            "parameters referred to by 'exceptions' not present: "
            + ' '.join(repr(name) for name in exceptions))
    return kwoargs(*args)(func)
Esempio n. 6
0
def _posoargs_end(end, _posoargs, func, *args, **kwargs):
    posoarg_names = set(_posoargs)
    found = False
    sig = _signatures.forged_signature(func).parameters.values()
    for param in sig:
        if param.kind == param.POSITIONAL_OR_KEYWORD:
            if not found:
                posoarg_names.add(param.name)
            if param.name == end:
                found = True
        elif param.kind != param.POSITIONAL_ONLY:
            break # no more POKs now
    if not found:
        raise ValueError('{0!r} not found in {1.__name__}{2}'.format(
            end, func, sig))
    return _PokTranslator(
        func, posoargs=posoarg_names,
        get=partial(_posoargs_end, end, _posoargs))