Skip to content

Commit

Permalink
Passing keyword only args as positional should fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjosephwilson committed Jun 15, 2024
1 parent 9b7bf5d commit c8acb9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions mako/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def get_argument_expressions(self, as_call=False):
else:
namedecls.append(name)

# A bare `*` immediately followed by `**kwargs` is not valid python.
# Ie. Using a bare `*` without any named keyword arguments.
# A bare `*` and `*args` together is not valid python.
# Ie. Using variable positional arguments already means everything
# following that argument must be keyword only.
if not as_call and kwargnames and not self.varargs:
namedecls.append("*")

# Positional arguments
if self.varargs:
namedecls.append("*" + argnames.pop(0))
Expand Down
19 changes: 18 additions & 1 deletion test/test_def.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mako import lookup
from mako.template import Template
from mako.testing.assertions import assert_raises
from mako.testing.assertions import assert_raises, assert_raises_message
from mako.testing.assertions import eq_
from mako.testing.fixtures import TemplateTest
from mako.testing.helpers import flatten_result
Expand Down Expand Up @@ -62,6 +62,23 @@ def test_def_py3k_args(self):
"""look at all these args: one two three four 5 seven""",
)

def test_def_py3k_kwonly_as_pos(self):
"""Test that using position arg for kwonly argument fails."""
template = Template(
"""
<%def name="kwonly(one, *, two)">
look at all these args: ${one} ${two}"""
"""
</%def>
${kwonly('one', 'two')}"""
)
assert_raises_message(
TypeError,
"takes 1 positional argument but 2 were given",
template.render,
)

def test_inter_def(self):
"""test defs calling each other"""
template = Template(
Expand Down

0 comments on commit c8acb9a

Please sign in to comment.