Refactor code to allow only keyword-only arguments to the main function.

This commit is contained in:
Dave Halter
2017-05-18 17:16:42 -04:00
parent 02278305f7
commit c1010008af
2 changed files with 21 additions and 3 deletions

View File

@@ -47,9 +47,7 @@ def load_grammar(version=None):
return load_grammar()
def parse(code=None, path=None, grammar=None, error_recovery=True,
start_symbol='file_input', cache=False, diff_cache=False,
cache_path=None):
def parse(code=None, **kwargs):
"""
If you want to parse a Python file you want to start here, most likely.
@@ -70,6 +68,21 @@ def parse(code=None, path=None, grammar=None, error_recovery=True,
:return: A syntax tree node. Typically the module.
"""
# Wanted python3.5 * operator and keyword only arguments.
path = kwargs.pop('path', None)
grammar = kwargs.pop('grammar', None)
error_recovery = kwargs.pop('error_recovery', True)
start_symbol = kwargs.pop('start_symbol', 'file_input')
cache = kwargs.pop('cache', False)
diff_cache = kwargs.pop('diff_cache', False)
cache_path = kwargs.pop('cache_path', None)
if kwargs:
raise TypeError(
"parse() got an unexpected keyword argument '%s'"
% next(iter(kwargs)))
# Start with actual code.
if code is None and path is None:
raise TypeError("Please provide either code or a path.")

View File

@@ -178,3 +178,8 @@ def test_open_string_literal(code):
module = parse(code)
assert module.get_code() == code
assert module.end_pos == end_pos == module.children[1].end_pos
def test_too_many_params():
with pytest.raises(TypeError):
parse('asdf', hello=3)