示例#1
0
文件: legacy.py 项目: prodigeni/clize
def _clize(fn, alias={}, force_positional=(), coerce={},
           require_excess=False, extra=(),
           use_kwoargs=None):
    sig = util.funcsigs.signature(fn)
    has_kwoargs = False
    annotations = defaultdict(list)
    ann_positional = []
    for param in sig.parameters.values():
        if param.kind == param.KEYWORD_ONLY:
            has_kwoargs = True
        elif require_excess and param.kind == param.VAR_POSITIONAL:
            annotations[param.name].append(parser.Parameter.REQUIRED)
        if param.annotation != param.empty:
            ann = util.maybe_iter(param.annotation)
            annotations[param.name].extend(ann)
            if clize.POSITIONAL in ann:
                ann_positional.append(param.name)
                annotations[param.name].remove(clize.POSITIONAL)
    for name, aliases in alias.items():
        annotations[name].extend(aliases)
    for name, func in coerce.items():
        annotations[name].append(func)
    annotate(**annotations)(fn)
    use_kwoargs = has_kwoargs if use_kwoargs is None else use_kwoargs
    if not use_kwoargs:
        fn = autokwoargs(
            exceptions=chain(ann_positional, force_positional))(fn)
    return runner.Clize(fn, extra=extra)
示例#2
0
 def test_success(self):
     self.assertSigsEqual(s('a, b:1'),
                          signature(modifiers.annotate(b=1)(f('a, b'))))
     self.assertSigsEqual(
         s('a:1, b:2'), signature(modifiers.annotate(a=1, b=2)(f('a, b'))))
     self.assertSigsEqual(s('a:1, b', 2),
                          signature(modifiers.annotate(2, a=1)(f('a, b'))))
示例#3
0
 def test_success(self):
     self.assertSigsEqual(
         s('a, b:1'),
         signature(modifiers.annotate(b=1)(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1, b:2'),
         signature(modifiers.annotate(a=1, b=2)(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1, b', 2),
         signature(modifiers.annotate(2, a=1)(f('a, b')))
         )
示例#4
0
 def test_use_twice(self):
     annotator = modifiers.annotate(a=1)
     self.assertSigsEqual(
         s('a:1, b'),
         signature(annotator(f('a, b')))
         )
     self.assertSigsEqual(
         s('a:1'),
         signature(annotator(f('a')))
         )
示例#5
0
 def test_pok_interact(self):
     pok = f('self, a, *, b')
     annotated = modifiers.annotate(a=1, b=2)(pok)
     self.assertSigsEqual(
         s('self, a:1, *, b:2'),
         signature(annotated)
         )
     self.assertSigsEqual(
         s('a:1, *, b:2'),
         signature(safe_get(annotated, object(), object))
         )
示例#6
0
文件: legacy.py 项目: mgielda/clize
def _clize(fn,
           alias={},
           force_positional=(),
           coerce={},
           require_excess=False,
           extra=(),
           use_kwoargs=None):
    sig = specifiers.signature(fn)
    has_kwoargs = False
    annotations = defaultdict(list)
    ann_positional = []
    for param in sig.parameters.values():
        coerce_set = False
        if param.kind == param.KEYWORD_ONLY:
            has_kwoargs = True
        elif param.kind == param.VAR_KEYWORD:
            annotations[param.name].append(parser.Parameter.IGNORE)
        elif require_excess and param.kind == param.VAR_POSITIONAL:
            annotations[param.name].append(parser.Parameter.REQUIRED)
        if param.annotation != param.empty:
            for thing in util.maybe_iter(param.annotation):
                if thing == clize.POSITIONAL:
                    ann_positional.append(param.name)
                    continue
                elif callable(thing):
                    coerce_set = True
                    thing = _convert_coerce(thing)
                annotations[param.name].append(thing)
        try:
            func = coerce[param.name]
        except KeyError:
            pass
        else:
            annotations[param.name].append(_convert_coerce(func))
            coerce_set = True
        annotations[param.name].extend(alias.get(param.name, ()))
        if not coerce_set and param.default != param.empty:
            annotations[param.name].append(_convert_coerce(type(
                param.default)))
    fn = modifiers.annotate(**annotations)(fn)
    use_kwoargs = has_kwoargs if use_kwoargs is None else use_kwoargs
    if not use_kwoargs:
        fn = modifiers.autokwoargs(
            exceptions=chain(ann_positional, force_positional))(fn)
    return runner.Clize(fn, extra=extra)
示例#7
0
def _clize(fn, alias={}, force_positional=(), coerce={},
           require_excess=False, extra=(),
           use_kwoargs=None):
    sig = specifiers.signature(fn)
    has_kwoargs = False
    annotations = defaultdict(list)
    ann_positional = []
    for param in sig.parameters.values():
        coerce_set = False
        if param.kind == param.KEYWORD_ONLY:
            has_kwoargs = True
        elif param.kind == param.VAR_KEYWORD:
            annotations[param.name].append(parser.Parameter.IGNORE)
        elif require_excess and param.kind == param.VAR_POSITIONAL:
            annotations[param.name].append(parser.Parameter.REQUIRED)
        if param.annotation != param.empty:
            for thing in util.maybe_iter(param.annotation):
                if thing == clize.POSITIONAL:
                    ann_positional.append(param.name)
                    continue
                elif callable(thing):
                    coerce_set = True
                    thing = _convert_coerce(thing)
                annotations[param.name].append(thing)
        try:
            func = coerce[param.name]
        except KeyError:
            pass
        else:
            annotations[param.name].append(_convert_coerce(func))
            coerce_set = True
        annotations[param.name].extend(alias.get(param.name, ()))
        if not coerce_set and param.default != param.empty:
            annotations[param.name].append(
                _convert_coerce(type(param.default)))
    fn = modifiers.annotate(**annotations)(fn)
    use_kwoargs = has_kwoargs if use_kwoargs is None else use_kwoargs
    if not use_kwoargs:
        fn = modifiers.autokwoargs(
            exceptions=chain(ann_positional, force_positional))(fn)
    return runner.Clize(fn, extra=extra)
示例#8
0
 def test_pok_interact(self):
     pok = f('self, a, *, b')
     annotated = modifiers.annotate(a=1, b=2)(pok)
     self.assertSigsEqual(s('self, a:1, *, b:2'), signature(annotated))
     self.assertSigsEqual(s('a:1, *, b:2'),
                          signature(safe_get(annotated, object(), object)))
示例#9
0
 def test_unused_annotation(self):
     self.assertRaises(ValueError, modifiers.annotate(a=1, c=2), f('a, b'))
示例#10
0
 def test_use_twice(self):
     annotator = modifiers.annotate(a=1)
     self.assertSigsEqual(s('a:1, b'), signature(annotator(f('a, b'))))
     self.assertSigsEqual(s('a:1'), signature(annotator(f('a'))))
示例#11
0
 def test_unused_annotation(self):
     self.assertRaises(
         ValueError,
         modifiers.annotate(a=1, c=2), f('a, b')
         )