change keyword_arguments_for so it does not modify and return

This commit is contained in:
Anthony Sottile 2022-01-23 19:06:06 -05:00
parent 969e8f38d3
commit f9eb0fd6ea
3 changed files with 16 additions and 15 deletions

View file

@ -340,13 +340,15 @@ class FileChecker:
LOG.debug("Running %r with %r", plugin, arguments)
assert self.processor is not None
try:
self.processor.keyword_arguments_for(plugin.parameters, arguments)
params = self.processor.keyword_arguments_for(
plugin.parameters, arguments
)
except AttributeError as ae:
raise exceptions.PluginRequestedUnknownParameters(
plugin_name=plugin.display_name, exception=ae
)
try:
return plugin.obj(**arguments)
return plugin.obj(**arguments, **params)
except Exception as all_exc:
LOG.critical(
"Plugin %s raised an unexpected exception",

View file

@ -241,16 +241,15 @@ class FileProcessor:
def keyword_arguments_for(
self,
parameters: Dict[str, bool],
arguments: Optional[Dict[str, Any]] = None,
arguments: Dict[str, Any],
) -> Dict[str, Any]:
"""Generate the keyword arguments for a list of parameters."""
if arguments is None:
arguments = {}
ret = {}
for param, required in parameters.items():
if param in arguments:
continue
try:
arguments[param] = getattr(self, param)
ret[param] = getattr(self, param)
except AttributeError:
if required:
raise
@ -260,7 +259,7 @@ class FileProcessor:
"but this is not an available parameter.",
param,
)
return arguments
return ret
def generate_tokens(self) -> Generator[tokenize.TokenInfo, None, None]:
"""Tokenize the file and yield the tokens."""

View file

@ -209,21 +209,21 @@ def test_next_line(default_options):
[
(
{"blank_before": True, "blank_lines": True},
None,
{},
{"blank_before": 0, "blank_lines": 0},
),
(
{"noqa": True, "fake": True},
{"fake": "foo"},
{"noqa": False, "fake": "foo"},
{"noqa": False},
),
(
{"blank_before": True, "blank_lines": True, "noqa": True},
{"blank_before": 10, "blank_lines": 5, "noqa": True},
{"blank_before": 10, "blank_lines": 5, "noqa": True},
{},
),
({}, {"fake": "foo"}, {"fake": "foo"}),
({"non-existent": False}, {"fake": "foo"}, {"fake": "foo"}),
({}, {"fake": "foo"}, {}),
({"non-existent": False}, {"fake": "foo"}, {}),
],
)
def test_keyword_arguments_for(params, args, expected_kwargs, default_options):
@ -235,9 +235,9 @@ def test_keyword_arguments_for(params, args, expected_kwargs, default_options):
"Line 1",
],
)
kwargs_for = file_processor.keyword_arguments_for
ret = file_processor.keyword_arguments_for(params, args)
assert kwargs_for(params, args) == expected_kwargs
assert ret == expected_kwargs
def test_keyword_arguments_for_does_not_handle_attribute_errors(
@ -253,7 +253,7 @@ def test_keyword_arguments_for_does_not_handle_attribute_errors(
)
with pytest.raises(AttributeError):
file_processor.keyword_arguments_for({"fake": True})
file_processor.keyword_arguments_for({"fake": True}, {})
def test_processor_split_line(default_options):