mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 13:24:39 +08:00
Refactor code to allow only keyword-only arguments to the main function.
This commit is contained in:
@@ -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.")
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user