Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ def _get_group_dict(line: str) -> Optional[Dict[str, str]]:
return None


def _build_message_doc_url(code: str) -> str:
"""Build the URL to the documentation for this warning."""
anchor = utils.ERROR_CODE_ANCHORS.get(code)
if anchor is None:
return ""
return utils.ERROR_CODE_BASE_URL + anchor


def _parse_output_using_regex(
content: str, severity: Dict[str, str]
) -> list[lsp.Diagnostic]:
Expand Down Expand Up @@ -174,6 +182,8 @@ def _parse_output_using_regex(
character=end_column, # ignore col_offset for end
)

documentation_url = _build_message_doc_url(data["code"])

diagnostic = lsp.Diagnostic(
range=lsp.Range(
start=start,
Expand All @@ -182,6 +192,7 @@ def _parse_output_using_regex(
message=data.get("message"),
severity=_get_severity(data["code"], data["type"], severity),
code=data["code"],
code_description=lsp.CodeDescription(href=documentation_url),
source=TOOL_DISPLAY,
)
diagnostics.append(diagnostic)
Expand Down
56 changes: 56 additions & 0 deletions bundled/tool/lsp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,62 @@
# Save the working directory used when loading this module
SERVER_CWD = os.getcwd()
CWD_LOCK = threading.Lock()
ERROR_CODE_BASE_URL = "https://mypy.readthedocs.io/en/stable/"
ERROR_CODE_ANCHORS = {
"attr-defined": "error_code_list.html#check-that-attribute-exists-attr-defined",
"union-attr": "error_code_list.html#check-that-attribute-exists-in-each-union-item-union-attr",
"name-defined": "error_code_list.html#check-that-name-is-defined-name-defined",
"used-before-def": "error_code_list.html#check-that-a-variable-is-not-used-before-it-s-defined-used-before-def",
"call-arg": "error_code_list.html#check-arguments-in-calls-call-arg",
"arg-type": "error_code_list.html#check-argument-types-arg-type",
"call-overload": "error_code_list.html#check-calls-to-overloaded-functions-call-overload",
"valid-type": "error_code_list.html#check-validity-of-types-valid-type",
"var-annotated": "error_code_list.html#require-annotation-if-variable-type-is-unclear-var-annotated",
"override": "error_code_list.html#check-validity-of-overrides-override",
"return": "error_code_list.html#check-that-function-returns-a-value-return",
"return-value": "error_code_list.html#check-that-return-value-is-compatible-return-value",
"assignment": "error_code_list.html#check-types-in-assignment-statement-assignment",
"method-assign": "error_code_list.html#check-that-assignment-target-is-not-a-method-method-assign",
"type-var": "error_code_list.html#check-type-variable-values-type-var",
"operator": "error_code_list.html#check-uses-of-various-operators-operator",
"index": "error_code_list.html#check-indexing-operations-index",
"list-item": "error_code_list.html#check-list-items-list-item",
"dict-item": "error_code_list.html#check-dict-items-dict-item",
"typeddict-item": "error_code_list.html#check-typeddict-items-typeddict-item",
"typeddict-unknown-key": "error_code_list.html#check-typeddict-keys-typeddict-unknown-key",
"has-type": "error_code_list.html#check-that-type-of-target-is-known-has-type",
"import": "error_code_list.html#check-that-import-target-can-be-found-import",
"no-redef": "error_code_list.html#check-that-each-name-is-defined-once-no-redef",
"func-returns-value": "error_code_list.html#check-that-called-function-returns-a-value-func-returns-value",
"abstract": "error_code_list.html#check-instantiation-of-abstract-classes-abstract",
"type-abstract": "error_code_list.html#safe-handling-of-abstract-type-object-types-type-abstract",
"safe-super": "error_code_list.html#check-that-call-to-an-abstract-method-via-super-is-valid-safe-super",
"valid-newtype": "error_code_list.html#check-the-target-of-newtype-valid-newtype",
"exit-return": "error_code_list.html#check-the-return-type-of-exit-exit-return",
"name-match": "error_code_list.html#check-that-naming-is-consistent-name-match",
"literal-required": "error_code_list.html#check-that-literal-is-used-where-expected-literal-required",
"no-overload-impl": "error_code_list.html#check-that-overloaded-functions-have-an-implementation-no-overload-impl",
"unused-coroutine": "error_code_list.html#check-that-coroutine-return-value-is-used-unused-coroutine",
"assert-type": "error_code_list.html#check-types-in-assert-type-assert-type",
"truthy-function": "error_code_list.html#check-that-function-isn-t-used-in-boolean-context-truthy-function",
"str-bytes-safe": "error_code_list.html#check-for-implicit-bytes-coercions-str-bytes-safe",
"syntax": "error_code_list.html#report-syntax-errors-syntax",
"misc": "error_code_list.html#miscellaneous-checks-misc",
"type-arg": "error_code_list2.html#check-that-type-arguments-exist-type-arg",
"no-untyped-def": "error_code_list2.html#check-that-every-function-has-an-annotation-no-untyped-def",
"redundant-cast": "error_code_list2.html#check-that-cast-is-not-redundant-redundant-cast",
"redundant-self": "error_code_list2.html#check-that-methods-do-not-have-redundant-self-annotations-redundant-self",
"comparison-overlap": "error_code_list2.html#check-that-comparisons-are-overlapping-comparison-overlap",
"no-untyped-call": "error_code_list2.html#check-that-no-untyped-functions-are-called-no-untyped-call",
"no-any-return": "error_code_list2.html#check-that-function-does-not-return-any-value-no-any-return",
"no-any-unimported": "error_code_list2.html#check-that-types-have-no-any-components-due-to-missing-imports-no-any-unimported",
"unreachable": "error_code_list2.html#check-that-statement-or-expression-is-unreachable-unreachable",
"redundant-expr": "error_code_list2.html#check-that-expression-is-redundant-redundant-expr",
"truthy-bool": "error_code_list2.html#check-that-expression-is-not-implicitly-true-in-boolean-context-truthy-bool",
"truthy-iterable": "error_code_list2.html#check-that-iterable-is-not-implicitly-true-in-boolean-context-truthy-iterable",
"undefined": "error_code_list2.html#check-that-type-ignore-include-an-error-code-ignore-without-code",
"unused-awaitable": "error_code_list2.html#check-that-awaitable-return-value-is-used-unused-awaitable"
}


def as_list(content: Union[Any, List[Any], Tuple[Any]]) -> List[Any]:
Expand Down
9 changes: 9 additions & 0 deletions src/test/python_tests/test_linting.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def _log_handler(params):
"message": 'Name "x" is not defined ',
"severity": 1,
"code": "name-defined",
"codeDescription": {
"href": "https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-name-is-defined-name-defined"
},
"source": "Mypy",
}
],
Expand Down Expand Up @@ -117,6 +120,9 @@ def _log_handler(params):
"message": 'Name "x" is not defined ',
"severity": 1,
"code": "name-defined",
"codeDescription": {
"href": "https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-name-is-defined-name-defined"
},
"source": "Mypy",
}
],
Expand Down Expand Up @@ -236,6 +242,9 @@ def _log_handler(params):
"message": 'Name "x" is not defined ',
"severity": 2,
"code": "name-defined",
"codeDescription": {
"href": "https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-name-is-defined-name-defined"
},
"source": "Mypy",
}
],
Expand Down