Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen] 🔧 Convert Function Calls to Use Named Kwargs #582

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pylsp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def main() -> None:
parser = argparse.ArgumentParser()
add_arguments(parser)
args = parser.parse_args()
_configure_logger(args.verbose, args.log_config, args.log_file)
_configure_logger(
verbose=args.verbose, log_config=args.log_config, log_file=args.log_file
)

if args.tcp:
start_tcp_lang_server(
Expand Down
4 changes: 3 additions & 1 deletion pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ def position_to_jedi_linecolumn(document, position):
code_position = {
"line": position["line"] + 1,
"column": clip_column(
position["character"], document.lines, position["line"]
column=position["character"],
lines=document.lines,
line_number=position["line"],
),
}
return code_position
Expand Down
6 changes: 3 additions & 3 deletions pylsp/plugins/autopep8_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def pylsp_format_document(config, workspace, document, options):
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
return _format(config=config, document=document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
Expand All @@ -31,11 +31,11 @@ def pylsp_format_range(config, workspace, document, range, options):

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range["start"]["line"] + 1, range["end"]["line"])
return _format(config, document, line_range=line_range)
return _format(config=config, document=document, line_range=line_range)


def _format(config, document, line_range=None):
options = _autopep8_config(config, document)
options = _autopep8_config(config=config, document=document)
if line_range:
options["line_range"] = list(line_range)

Expand Down
11 changes: 8 additions & 3 deletions pylsp/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def pylsp_definitions(
config: Config, document: Document, position: Dict[str, int]
) -> List[Dict[str, Any]]:
settings = config.plugin_settings("jedi_definition")
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
script = document.jedi_script(use_document_path=True)
auto_import_modules = jedi.settings.auto_import_modules

Expand All @@ -57,14 +59,17 @@ def pylsp_definitions(
follow_builtin_imports=settings.get("follow_builtin_imports", True),
**code_position,
)
definitions = [_resolve_definition(d, script, settings) for d in definitions]
definitions = [
_resolve_definition(maybe_defn=d, script=script, settings=settings)
for d in definitions
]
finally:
jedi.settings.auto_import_modules = auto_import_modules

follow_builtin_defns = settings.get("follow_builtin_definitions", True)
return [
{
"uri": uris.uri_with(document.uri, path=str(d.module_path)),
"uri": uris.uri_with(uri=document.uri, path=str(d.module_path)),
"range": {
"start": {"line": d.line - 1, "character": d.column},
"end": {"line": d.line - 1, "character": d.column + len(d.name)},
Expand Down
9 changes: 7 additions & 2 deletions pylsp/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ def pylsp_lint(workspace, document):
# ensure the same source is used for flake8 execution and result parsing;
# single source access improves performance as it is only one disk access
source = document.source
output = run_flake8(flake8_executable, args, document, source)
return parse_stdout(source, output)
output = run_flake8(
flake8_executable=flake8_executable,
args=args,
document=document,
source=source,
)
return parse_stdout(source=source, stdout=output)


def run_flake8(flake8_executable, args, document, source):
Expand Down
40 changes: 28 additions & 12 deletions pylsp/plugins/folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def pylsp_folding_range(document):
program = document.source + "\n"
lines = program.splitlines()
tree = parso.parse(program)
ranges = __compute_folding_ranges(tree, lines)
ranges = __compute_folding_ranges(tree=tree, lines=lines)

results = []
for start_line, end_line in ranges:
Expand Down Expand Up @@ -88,18 +88,28 @@ def __compute_folding_ranges_identation(text):
current_level = level
elif level < current_level:
identation_stack, folding_ranges = __match_identation_stack(
identation_stack, level, level_limits, folding_ranges, current_line
identation_stack=identation_stack,
level=level,
level_limits=level_limits,
folding_ranges=folding_ranges,
current_line=current_line,
)
current_level = level
else:
folding_ranges = __empty_identation_stack(
identation_stack, level_limits, current_line, folding_ranges
identation_stack=identation_stack,
level_limits=level_limits,
current_line=current_line,
folding_ranges=folding_ranges,
)
current_level = 0
if line.strip() != "":
current_line = i
folding_ranges = __empty_identation_stack(
identation_stack, level_limits, current_line, folding_ranges
identation_stack=identation_stack,
level_limits=level_limits,
current_line=current_line,
folding_ranges=folding_ranges,
)
return dict(folding_ranges)

Expand Down Expand Up @@ -136,25 +146,27 @@ def __handle_flow_nodes(node, end_line, stack):
if isinstance(node, tree_nodes.Keyword):
from_keyword = True
if node.value in {"if", "elif", "with", "while"}:
node, end_line = __handle_skip(stack, 2)
node, end_line = __handle_skip(stack=stack, skip=2)
elif node.value in {"except"}:
first_node = stack[0]
if isinstance(first_node, tree_nodes.Operator):
node, end_line = __handle_skip(stack, 1)
node, end_line = __handle_skip(stack=stack, skip=1)
else:
node, end_line = __handle_skip(stack, 2)
node, end_line = __handle_skip(stack=stack, skip=2)
elif node.value in {"for"}:
node, end_line = __handle_skip(stack, 4)
node, end_line = __handle_skip(stack=stack, skip=4)
elif node.value in {"else"}:
node, end_line = __handle_skip(stack, 1)
node, end_line = __handle_skip(stack=stack, skip=1)
return end_line, from_keyword, node, stack


def __compute_start_end_lines(node, stack):
start_line, _ = node.start_pos
end_line, _ = node.end_pos
modified = False
end_line, from_keyword, node, stack = __handle_flow_nodes(node, end_line, stack)
end_line, from_keyword, node, stack = __handle_flow_nodes(
node=node, end_line=end_line, stack=stack
)

last_leaf = node.get_last_leaf()
last_newline = isinstance(last_leaf, tree_nodes.Newline)
Expand Down Expand Up @@ -194,12 +206,16 @@ def __compute_folding_ranges(tree, lines):
padding = [""] * start_line
text = "\n".join(padding + lines[start_line:]) + "\n"
identation_ranges = __compute_folding_ranges_identation(text)
folding_ranges = __merge_folding_ranges(folding_ranges, identation_ranges)
folding_ranges = __merge_folding_ranges(
left=folding_ranges, right=identation_ranges
)
break
if not isinstance(node, SKIP_NODES):
valid = __check_if_node_is_valid(node)
if valid:
start_line, end_line, stack = __compute_start_end_lines(node, stack)
start_line, end_line, stack = __compute_start_end_lines(
node=node, stack=stack
)
if end_line > start_line:
current_end = folding_ranges.get(start_line, -1)
folding_ranges[start_line] = max(current_end, end_line)
Expand Down
4 changes: 3 additions & 1 deletion pylsp/plugins/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

@hookimpl
def pylsp_document_highlight(document, position):
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
usages = document.jedi_script().get_references(**code_position)

def is_valid(definition):
Expand Down
8 changes: 5 additions & 3 deletions pylsp/plugins/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

@hookimpl
def pylsp_hover(config, document, position):
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
definitions = document.jedi_script(use_document_path=True).infer(**code_position)
word = document.word_at_position(position)

Expand Down Expand Up @@ -43,8 +45,8 @@ def pylsp_hover(config, document, position):
return {
"contents": _utils.format_docstring(
# raw docstring returns only doc, without signature
definition.docstring(raw=True),
preferred_markup_kind,
contents=definition.docstring(raw=True),
markup_kind=preferred_markup_kind,
signatures=[signature] if signature else None,
)
}
32 changes: 20 additions & 12 deletions pylsp/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def pylsp_completions(config, document, position):
"""Get formatted completions for current code position"""
settings = config.plugin_settings("jedi_completion", document_path=document.path)
resolve_eagerly = settings.get("eager", False)
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)

code_position["fuzzy"] = settings.get("fuzzy", False)
completions = document.jedi_script(use_document_path=True).complete(**code_position)
Expand All @@ -67,22 +69,24 @@ def pylsp_completions(config, document, position):
SNIPPET_RESOLVER.cached_modules = modules_to_cache_for

include_params = (
snippet_support and should_include_params and use_snippets(document, position)
snippet_support
and should_include_params
and use_snippets(document=document, position=position)
)
include_class_objects = (
snippet_support
and should_include_class_objects
and use_snippets(document, position)
and use_snippets(document=document, position=position)
)
include_function_objects = (
snippet_support
and should_include_function_objects
and use_snippets(document, position)
and use_snippets(document=document, position=position)
)

ready_completions = [
_format_completion(
c,
d=c,
markup_kind=preferred_markup_kind,
include_params=include_params if c.type in ["class", "function"] else False,
resolve=resolve_eagerly,
Expand All @@ -97,7 +101,7 @@ def pylsp_completions(config, document, position):
for i, c in enumerate(completions):
if c.type == "class":
completion_dict = _format_completion(
c,
d=c,
markup_kind=preferred_markup_kind,
include_params=False,
resolve=resolve_eagerly,
Expand All @@ -112,7 +116,7 @@ def pylsp_completions(config, document, position):
for i, c in enumerate(completions):
if c.type == "function":
completion_dict = _format_completion(
c,
d=c,
markup_kind=preferred_markup_kind,
include_params=False,
resolve=resolve_eagerly,
Expand Down Expand Up @@ -152,7 +156,9 @@ def pylsp_completion_item_resolve(config, completion_item, document):

if shared_data:
completion, data = shared_data
return _resolve_completion(completion, data, markup_kind=preferred_markup_kind)
return _resolve_completion(
completion=completion, d=data, markup_kind=preferred_markup_kind
)
return completion_item


Expand Down Expand Up @@ -211,7 +217,7 @@ def _resolve_completion(completion, d, markup_kind: str):
completion["detail"] = _detail(d)
try:
docs = _utils.format_docstring(
d.docstring(raw=True),
contents=d.docstring(raw=True),
signatures=[signature.to_string() for signature in d.get_signatures()],
markup_kind=markup_kind,
)
Expand All @@ -230,14 +236,16 @@ def _format_completion(
snippet_support=False,
):
completion = {
"label": _label(d, resolve_label_or_snippet),
"label": _label(definition=d, resolve=resolve_label_or_snippet),
"kind": _TYPE_MAP.get(d.type),
"sortText": _sort_text(d),
"insertText": d.name,
}

if resolve:
completion = _resolve_completion(completion, d, markup_kind)
completion = _resolve_completion(
completion=completion, d=d, markup_kind=markup_kind
)

# Adjustments for file completions
if d.type == "path":
Expand All @@ -260,7 +268,7 @@ def _format_completion(
completion["insertText"] = path

if include_params and not is_exception_class(d.name):
snippet = _snippet(d, resolve_label_or_snippet)
snippet = _snippet(definition=d, resolve=resolve_label_or_snippet)
completion.update(snippet)

return completion
Expand Down
2 changes: 1 addition & 1 deletion pylsp/plugins/jedi_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def pylsp_rename(config, workspace, document, position, new_name):
log.debug(
"Executing rename of %s to %s", document.word_at_position(position), new_name
)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs = _utils.position_to_jedi_linecolumn(document=document, position=position)
kwargs["new_name"] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion pylsp/plugins/pydocstyle_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def pylsp_lint(config, workspace, document):
for error in errors:
if error.code not in checked_codes:
continue
diags.append(_parse_diagnostic(document, error))
diags.append(_parse_diagnostic(document=document, error=error))
except pydocstyle.parser.ParseError:
# In the case we cannot parse the Python file, just continue
pass
Expand Down
10 changes: 7 additions & 3 deletions pylsp/plugins/pylint_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ def pylsp_lint(config, workspace, document, is_saved):
if settings.get("executable") and sys.version_info[0] >= 3:
flags = build_args_stdio(settings)
pylint_executable = settings.get("executable", "pylint")
return pylint_lint_stdin(pylint_executable, document, flags)
return pylint_lint_stdin(
pylint_executable=pylint_executable, document=document, flags=flags
)
flags = _build_pylint_flags(settings)
return PylintLinter.lint(document, is_saved, flags=flags)

Expand Down Expand Up @@ -260,8 +262,10 @@ def pylint_lint_stdin(pylint_executable, document, flags):
:return: linting diagnostics
:rtype: list
"""
pylint_result = _run_pylint_stdio(pylint_executable, document, flags)
return _parse_pylint_stdio_result(document, pylint_result)
pylint_result = _run_pylint_stdio(
pylint_executable=pylint_executable, document=document, flags=flags
)
return _parse_pylint_stdio_result(document=document, stdout=pylint_result)


def _run_pylint_stdio(pylint_executable, document, flags):
Expand Down
6 changes: 4 additions & 2 deletions pylsp/plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

@hookimpl
def pylsp_references(document, position, exclude_declaration):
code_position = _utils.position_to_jedi_linecolumn(document, position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
usages = document.jedi_script().get_references(**code_position)

if exclude_declaration:
Expand All @@ -20,7 +22,7 @@ def pylsp_references(document, position, exclude_declaration):
# Filter out builtin modules
return [
{
"uri": uris.uri_with(document.uri, path=str(d.module_path))
"uri": uris.uri_with(uri=document.uri, path=str(d.module_path))
if d.module_path
else document.uri,
"range": {
Expand Down
Loading