mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
added types to macros
This commit is contained in:
parent
9f8035ee49
commit
1d595e9ad9
3 changed files with 24 additions and 2 deletions
|
@ -199,7 +199,7 @@ call site execution context.
|
|||
|
||||
Macro Function Argument Annotations
|
||||
-----------------------------------
|
||||
There are five kinds of annotations that macros are able to interpret:
|
||||
There are six kinds of annotations that macros are able to interpret:
|
||||
|
||||
.. list-table:: Kinds of Annotation
|
||||
:header-rows: 1
|
||||
|
@ -234,6 +234,11 @@ There are five kinds of annotations that macros are able to interpret:
|
|||
- ``'x'`` or ``'exec'``
|
||||
- ``'exec'`` (default) or ``'single'``
|
||||
- Execs the argument and returns None.
|
||||
* - Type
|
||||
- ``type``
|
||||
- ``'t'`` or ``'type'``
|
||||
-
|
||||
- The type of the argument after ithas been evaluated.
|
||||
|
||||
These annotations allow you to hook into whichever stage of the compilation
|
||||
that you desire. It is important note that the string form of the arguments
|
||||
|
|
|
@ -157,7 +157,7 @@ def test_convert_macro_arg_eval(kind):
|
|||
|
||||
|
||||
@pytest.mark.parametrize('kind', [exec, 'x', 'exec'])
|
||||
def test_convert_macro_arg_eval(kind):
|
||||
def test_convert_macro_arg_exec(kind):
|
||||
# at global scope
|
||||
raw_arg = 'def f(x, y):\n return x + y'
|
||||
glbs = {}
|
||||
|
@ -177,6 +177,18 @@ def test_convert_macro_arg_eval(kind):
|
|||
assert locs['y'] == 43
|
||||
|
||||
|
||||
@pytest.mark.parametrize('kind', [type, 't', 'type'])
|
||||
def test_convert_macro_arg_eval(kind):
|
||||
# literals
|
||||
raw_arg = '42'
|
||||
arg = convert_macro_arg(raw_arg, kind, {}, None)
|
||||
assert arg is int
|
||||
# exprs
|
||||
raw_arg = 'x + 41'
|
||||
arg = convert_macro_arg(raw_arg, kind, {}, {'x': 1})
|
||||
assert arg is int
|
||||
|
||||
|
||||
def test_macro_context():
|
||||
def f():
|
||||
pass
|
||||
|
|
|
@ -696,6 +696,8 @@ def MACRO_FLAG_KINDS():
|
|||
'eval': eval,
|
||||
'x': exec,
|
||||
'exec': exec,
|
||||
't': type,
|
||||
'type': type,
|
||||
}
|
||||
|
||||
def _convert_kind_flag(x):
|
||||
|
@ -760,6 +762,9 @@ def convert_macro_arg(raw_arg, kind, glbs, locs, *, name='<arg>',
|
|||
raw_arg += '\n'
|
||||
arg = execer.exec(raw_arg, mode=mode, glbs=glbs, locs=locs,
|
||||
filename=filename)
|
||||
elif kind is type:
|
||||
arg = type(execer.eval(raw_arg, glbs=glbs, locs=locs,
|
||||
filename=filename))
|
||||
else:
|
||||
msg = ('kind={0!r} and mode={1!r} was not recongnized for macro '
|
||||
'argument {2!r}')
|
||||
|
|
Loading…
Add table
Reference in a new issue