Skip to content

Commit

Permalink
Use evaluated annotations from sigtools (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
epsy authored Jul 15, 2022
1 parent 85f3459 commit 6f42994
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
3 changes: 2 additions & 1 deletion clize/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,9 @@ def from_signature(cls, sig, extra=(), **kwargs):
@classmethod
def convert_parameter(cls, param):
"""Convert a Python parameter to a CLI parameter."""
param_annotation = param.upgraded_annotation.source_value()
if param.annotation != param.empty:
annotations = util.maybe_iter(param.annotation)
annotations = util.maybe_iter(param_annotation)
else:
annotations = []

Expand Down
64 changes: 32 additions & 32 deletions clize/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
from clize.tests.util import Fixtures


pathlib_name = "pathlib"


_ic = parser._implicit_converters


class FromSigTests(Fixtures):
def _test(self, sig_str, typ, str_rep, attrs, *, future_features=()):
pre_code = ("import pathlib; from clize import"
" Parameter")
sig = support.s(sig_str, pre=pre_code, future_features=future_features)
def _test(self, sig_str, typ, str_rep, attrs):
pre_code = "import pathlib; from clize import Parameter"
sig = self._support_s(sig_str, pre=pre_code)
return self._do_test(sig, typ, str_rep, attrs)

def _do_test(self, sig, typ, str_rep, attrs):
Expand All @@ -37,6 +33,8 @@ def _do_test(self, sig, typ, str_rep, attrs):
)
self.assertEqual(p_attrs, attrs)

def _support_s(self, *args, **kwargs):
return support.s(*args, **kwargs)

pos = 'one', parser.PositionalParameter, 'one', {
'conv': parser.identity, 'default': util.UNSET, 'required': True,
Expand Down Expand Up @@ -104,7 +102,7 @@ def test_vconverter(self):
@parser.value_converter
def converter(value):
raise NotImplementedError
sig = support.s('*, par: conv', locals={'conv': converter})
sig = self._support_s('*, par: conv', globals={'conv': converter})
self._do_test(sig, parser.OptionParameter, '--par=CONVERTER', {
'conv': converter,
})
Expand All @@ -115,7 +113,7 @@ class FancyDefault(object):
def __init__(self, arg):
self.arg = arg
deft = FancyDefault('ham')
sig = support.s('*, par=default', locals={'default': deft})
sig = self._support_s('*, par=default', globals={'default': deft})
self._do_test(sig, parser.OptionParameter, '[--par=FANCYDEFAULT]', {
'conv': FancyDefault,
'default': deft,
Expand All @@ -125,7 +123,7 @@ def test_bad_default_good_conv(self):
class UnknownDefault(object):
pass
deft = UnknownDefault()
sig = support.s('*, par:str=default', locals={'default': deft})
sig = self._support_s('*, par:str=default', globals={'default': deft})
self._do_test(sig, parser.OptionParameter, '[--par=STR]', {
'conv': parser.identity,
'default': deft,
Expand All @@ -137,7 +135,7 @@ def method(self, arg):
return self
s = Spam()
conv = parser.value_converter(s.method, name='TCONV')
sig = support.s('*, par: conv', locals={'conv': conv})
sig = self._support_s('*, par: conv', globals={'conv': conv})
self._do_test(sig, parser.OptionParameter, '--par=TCONV', {
'conv': conv
})
Expand All @@ -156,7 +154,7 @@ def conv(arg):
aliases=['--par']
)
self.assertEqual(param.get_all_names(), '--par[=CONV]')
sig = support.s('*, par: p, o=False', locals={'p': param})
sig = self._support_s('*, par: p, o=False', globals={'p': param})
self._do_test(sig, parser.FlagParameter, '[--par[=CONV]]', {
'conv': conv,
})
Expand All @@ -177,7 +175,7 @@ def conv(arg):
aliases=['--par', '-p']
)
self.assertEqual(param.get_all_names(), '-p, --par[=CONV]')
sig = support.s('*, par: p, o=False', locals={'p': param})
sig = self._support_s('*, par: p, o=False', globals={'p': param})
self._do_test(sig, parser.FlagParameter, '[-p]', {
'conv': conv,
})
Expand All @@ -189,7 +187,7 @@ def conv(arg):
{'par': 'eggs', 'o': True})

def test_alias_multi(self):
sig = support.s('*, one: a', locals={'a': ('a', 'b', 'abc')})
sig = self._support_s('*, one: a', globals={'a': ('a', 'b', 'abc')})
param = list(sig.parameters.values())[0]
cparam = parser.CliSignature.convert_parameter(param)
self.assertEqual(type(cparam), parser.OptionParameter)
Expand All @@ -199,7 +197,7 @@ def test_alias_multi(self):

def test_param_inst(self):
param = parser.Parameter('abc')
sig = support.s('xyz: p', locals={'p': param})
sig = self._support_s('xyz: p', globals={'p': param})
sparam = list(sig.parameters.values())[0]
cparam = parser.CliSignature.convert_parameter(sparam)
self.assertTrue(cparam is param)
Expand All @@ -215,8 +213,8 @@ def noop_converter(param, annotations):
raise NotImplementedError

sigs = [
support.s('o: c', locals={'c': converter}),
support.s('*, o: a', locals={'a': ("abc", converter)})
self._support_s('o: c', globals={'c': converter}),
self._support_s('*, o: a', globals={'a': ("abc", converter)})
]
for sig in sigs:
sparam = list(sig.parameters.values())[0]
Expand All @@ -231,7 +229,7 @@ def test_parameterflag_repr(self):

class SigTests(Fixtures):
def _test(self, sig_str, *args, **kwargs):
sig = support.s(sig_str, locals={'P': parser.Parameter})
sig = support.s(sig_str, globals={'P': parser.Parameter})
self._do_test(sig, *args, **kwargs)

def _do_test(self, sig, str_rep, args, posargs, kwargs):
Expand Down Expand Up @@ -339,7 +337,7 @@ def test_converter_ignore(self):
@parser.parameter_converter
def conv(param, annotations):
return parser.Parameter.IGNORE
sig = support.s('one:conv', locals={'conv': conv})
sig = support.s('one:conv', globals={'conv': conv})
csig = parser.CliSignature.from_signature(sig)
self.assertEqual(str(csig), '')

Expand Down Expand Up @@ -369,39 +367,39 @@ def test_posparam_set_value_parameter_not_present(self):

def test_posparam_set_value_only(self):
param = parser.PositionalParameter(argument_name='one', display_name='one')
sig = support.s('one:par', locals={'par': param})
sig = support.s('one:par', globals={'par': param})
csig = parser.CliSignature.from_signature(sig)
ba = parser.CliBoundArguments(csig, [], 'func', args=[])
param.set_value(ba, 'inserted')
self.assertEqual(ba.args, ['inserted'])

def test_posparam_set_value_already_set(self):
param = parser.PositionalParameter(argument_name='two', display_name='two')
sig = support.s('one, two:par', locals={'par': param})
sig = support.s('one, two:par', globals={'par': param})
csig = parser.CliSignature.from_signature(sig)
ba = parser.CliBoundArguments(csig, [], 'func', args=['one', 'two'])
param.set_value(ba, 'inserted')
self.assertEqual(ba.args, ['one', 'inserted'])

def test_posparam_set_value_after_set(self):
param = parser.PositionalParameter(argument_name='two', display_name='two')
sig = support.s('one, two:par', locals={'par': param})
sig = support.s('one, two:par', globals={'par': param})
csig = parser.CliSignature.from_signature(sig)
ba = parser.CliBoundArguments(csig, [], 'func', args=['one'])
param.set_value(ba, 'inserted')
self.assertEqual(ba.args, ['one', 'inserted'])

def test_posparam_set_value_after_default(self):
param = parser.PositionalParameter(argument_name='two', display_name='two', default="two")
sig = support.s('one="one", two:par="two"', locals={'par': param})
sig = support.s('one="one", two:par="two"', globals={'par': param})
csig = parser.CliSignature.from_signature(sig)
ba = parser.CliBoundArguments(csig, [], 'func', args=[])
param.set_value(ba, 'inserted')
self.assertEqual(ba.args, ['one', 'inserted'])

def test_posparam_set_value_after_missing(self):
param = parser.PositionalParameter(argument_name='two', display_name='two')
sig = support.s('one, two:par', locals={'par': param})
sig = support.s('one, two:par', globals={'par': param})
csig = parser.CliSignature.from_signature(sig)
ba = parser.CliBoundArguments(csig, [], 'func', args=[])
with self.assertRaises(ValueError):
Expand All @@ -411,29 +409,29 @@ def test_vconverter_keep_default(self):
@parser.value_converter
def conv(arg):
return 'converted'
sig = support.s('*, par:conv="default"', locals={'conv': conv})
sig = support.s('*, par:conv="default"', globals={'conv': conv})
self._do_test(sig, '[--par=CONV]', (), [], {})

def test_vconverter_convert_value(self):
@parser.value_converter(convert_default=True)
def conv(arg):
return 'c{}c'.format(arg)
sig = support.s('*, par:conv="default"', locals={'conv': conv})
sig = support.s('*, par:conv="default"', globals={'conv': conv})
self._do_test(sig, '[--par=CONV]', ('--par=A',), [], {'par': 'cAc'})
self._do_test(sig, '[--par=CONV]', ('--par', 'A',), [], {'par': 'cAc'})

def test_vconverter_convert_default(self):
@parser.value_converter(convert_default=True)
def conv(arg):
return 'converted'
sig = support.s('*, par:conv="default"', locals={'conv': conv})
sig = support.s('*, par:conv="default"', globals={'conv': conv})
self._do_test(sig, '[--par=CONV]', (), [], {'par': 'converted'})

def test_vconverter_convert_default_after_pos(self):
@parser.value_converter(convert_default=True)
def conv(arg):
return 'converted'
sig = support.s('first="otherdefault", par:conv="default"', locals={'conv': conv})
sig = support.s('first="otherdefault", par:conv="default"', globals={'conv': conv})
self._do_test(sig, '[first] [par]', (), ['otherdefault', 'converted'], {})


Expand Down Expand Up @@ -578,11 +576,13 @@ def func(*args):
)


@unittest.skip("mix of failing tests for now")
@unittest.skipUnless(has_future_annotations, "__future__.annotations only available certain python versions")
class FromSigTestsStringAnnotations(FromSigTests):
def _test(self, *args, **kwargs):
super()._test(*args, future_features=("annotations",), **kwargs)
return super()._test(*args, **kwargs)

def _support_s(self, *args, **kwargs):
return super()._support_s(*args, future_features=("annotations",), **kwargs)


class UnknownAnnotation(object):
Expand All @@ -595,8 +595,8 @@ def __init__(self, arg):


class BadParamTests(Fixtures):
def _test(self, sig_str, locals, exp_msg):
sig = support.s(sig_str, pre='from clize import Parameter', locals=locals)
def _test(self, sig_str, globals, exp_msg):
sig = support.s(sig_str, pre='from clize import Parameter', globals=globals)
params = list(sig.parameters.values())
with self.assertRaises(ValueError) as ar:
parser.CliSignature.convert_parameter(params[0])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
author_email='kaiser.yann@gmail.com',
python_requires='>=3.6',
install_requires=[
'sigtools ~= 3.0.0rc4',
'sigtools ~= 4.0.0a2',
'attrs>=19.1.0',
'od',
'docutils >= 0.17.0',
Expand Down

0 comments on commit 6f42994

Please sign in to comment.