Skip to content

Commit

Permalink
fixing ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
agserrano3 committed Jul 30, 2024
1 parent fa937af commit d49d754
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 127 deletions.
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(verbose=args.verbose, log_config=args.log_config, log_file=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(
column=position["character"], lines=document.lines, line_number=position["line"]
column=position["character"],
lines=document.lines,
line_number=position["line"],
),
}
return code_position
Expand Down
9 changes: 7 additions & 2 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=document, position=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,7 +59,10 @@ def pylsp_definitions(
follow_builtin_imports=settings.get("follow_builtin_imports", True),
**code_position,
)
definitions = [_resolve_definition(maybe_defn=d, script=script, settings=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

Expand Down
7 changes: 6 additions & 1 deletion pylsp/plugins/flake8_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ 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=flake8_executable, args=args, document=document, source=source)
output = run_flake8(
flake8_executable=flake8_executable,
args=args,
document=document,
source=source,
)
return parse_stdout(source=source, stdout=output)


Expand Down
28 changes: 22 additions & 6 deletions pylsp/plugins/folding.py
Original file line number Diff line number Diff line change
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=identation_stack, level=level, level_limits=level_limits, folding_ranges=folding_ranges, current_line=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=identation_stack, level_limits=level_limits, current_line=current_line, folding_ranges=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=identation_stack, level_limits=level_limits, current_line=current_line, folding_ranges=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 @@ -154,7 +164,9 @@ 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=node, end_line=end_line, stack=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(left=folding_ranges, right=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=node, stack=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=document, position=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
4 changes: 3 additions & 1 deletion 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=document, position=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
16 changes: 12 additions & 4 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=document, position=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,7 +69,9 @@ 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=document, position=position)
snippet_support
and should_include_params
and use_snippets(document=document, position=position)
)
include_class_objects = (
snippet_support
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=completion, d=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 @@ -237,7 +243,9 @@ def _format_completion(
}

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

# Adjustments for file completions
if d.type == "path":
Expand Down
8 changes: 6 additions & 2 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=pylint_executable, document=document, flags=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,7 +262,9 @@ def pylint_lint_stdin(pylint_executable, document, flags):
:return: linting diagnostics
:rtype: list
"""
pylint_result = _run_pylint_stdio(pylint_executable=pylint_executable, document=document, flags=flags)
pylint_result = _run_pylint_stdio(
pylint_executable=pylint_executable, document=document, flags=flags
)
return _parse_pylint_stdio_result(document=document, stdout=pylint_result)


Expand Down
4 changes: 3 additions & 1 deletion 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=document, position=position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
usages = document.jedi_script().get_references(**code_position)

if exclude_declaration:
Expand Down
12 changes: 10 additions & 2 deletions pylsp/plugins/rope_autoimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ def _process_statements(
edit_range = {"start": start, "end": start}
edit = {"range": edit_range, "newText": suggestion.import_statement + "\n"}
score = _get_score(
source=suggestion.source, full_statement=suggestion.import_statement, suggested_name=suggestion.name, desired_name=word
source=suggestion.source,
full_statement=suggestion.import_statement,
suggested_name=suggestion.name,
desired_name=word,
)
if score > _score_max:
continue
Expand Down Expand Up @@ -258,7 +261,12 @@ def pylsp_completions(
suggestions = list(autoimport.search_full(word, ignored_names=ignored_names))
results = sorted(
_process_statements(
suggestions=suggestions, doc_uri=document.uri, word=word, autoimport=autoimport, document=document, feature="completions"
suggestions=suggestions,
doc_uri=document.uri,
word=word,
autoimport=autoimport,
document=document,
feature="completions",
),
key=lambda statement: statement["sortText"],
)
Expand Down
8 changes: 6 additions & 2 deletions pylsp/plugins/rope_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def pylsp_completions(config, workspace, document, position):
"data": {"doc_uri": document.uri},
}
if resolve_eagerly:
item = _resolve_completion(completion=item, data=d, markup_kind=preferred_markup_kind)
item = _resolve_completion(
completion=item, data=d, markup_kind=preferred_markup_kind
)
new_definitions.append(item)

# most recently retrieved completion items, used for resolution
Expand Down Expand Up @@ -105,7 +107,9 @@ def pylsp_completion_item_resolve(config, completion_item, document):

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


Expand Down
7 changes: 5 additions & 2 deletions pylsp/plugins/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

@hookimpl
def pylsp_signature_help(config, document, position):
code_position = _utils.position_to_jedi_linecolumn(document=document, position=position)
code_position = _utils.position_to_jedi_linecolumn(
document=document, position=position
)
signatures = document.jedi_script().get_signatures(**code_position)

if not signatures:
Expand Down Expand Up @@ -54,7 +56,8 @@ def pylsp_signature_help(config, document, position):
{
"label": p.name,
"documentation": _utils.format_docstring(
contents=_param_docs(docstring=docstring, param_name=p.name), markup_kind=preferred_markup_kind
contents=_param_docs(docstring=docstring, param_name=p.name),
markup_kind=preferred_markup_kind,
),
}
for p in s.params
Expand Down
12 changes: 9 additions & 3 deletions test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class Dispatcher(FakeEditorMethodsMixin, MethodDispatcher):
def workspace(tmpdir, endpoint) -> None:
"""Return a workspace."""
ws = Workspace(root_uri=uris.from_fs_path(str(tmpdir)), endpoint=endpoint)
ws._config = Config(root_uri=ws.root_uri, init_opts={}, process_id=0, capabilities={})
ws._config = Config(
root_uri=ws.root_uri, init_opts={}, process_id=0, capabilities={}
)
yield ws
ws.close()

Expand All @@ -125,14 +127,18 @@ def workspace_other_root_path(tmpdir, endpoint):
"""Return a workspace with a root_path other than tmpdir."""
ws_path = str(tmpdir.mkdir("test123").mkdir("test456"))
ws = Workspace(root_uri=uris.from_fs_path(ws_path), endpoint=endpoint)
ws._config = Config(root_uri=ws.root_uri, init_opts={}, process_id=0, capabilities={})
ws._config = Config(
root_uri=ws.root_uri, init_opts={}, process_id=0, capabilities={}
)
return ws


@pytest.fixture
def config(workspace):
"""Return a config object."""
cfg = Config(root_uri=workspace.root_uri, init_opts={}, process_id=0, capabilities={})
cfg = Config(
root_uri=workspace.root_uri, init_opts={}, process_id=0, capabilities={}
)
cfg._plugin_settings = {
"plugins": {"pylint": {"enabled": False, "args": [], "executable": None}}
}
Expand Down
Loading

0 comments on commit d49d754

Please sign in to comment.