Skip to content

Commit 0efe84e

Browse files
Danthewaannmfussenegger
authored andcommitted
Add dmypy linter
1 parent 6b46370 commit 0efe84e

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Other dedicated linters that are built-in are:
119119
| [dash][dash] | `dash` |
120120
| [deadnix][deadnix] | `deadnix` |
121121
| [deno][deno] | `deno` |
122+
| [dmypy][dmypy] | `dmypy` |
122123
| [DirectX Shader Compiler][dxc] | `dxc` |
123124
| [djlint][djlint] | `djlint` |
124125
| [dotenv-linter][dotenv-linter] | `dotenv_linter` |
@@ -539,6 +540,7 @@ busted tests/
539540
[gdlint]: https://github.com/Scony/godot-gdscript-toolkit
540541
[rpm]: https://rpm.org
541542
[ec]: https://github.com/editorconfig-checker/editorconfig-checker
543+
[dmypy]: https://mypy.readthedocs.io/en/stable/mypy_daemon.html
542544
[deno]: https://github.com/denoland/deno
543545
[standardjs]: https://standardjs.com/
544546
[biomejs]: https://github.com/biomejs/biome

lua/lint/linters/dmypy.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- path/to/file:line:col: severity: message
2+
local pattern = '([^:]+):(%d+):(%d+):(%d+):(%d+): (%a+): (.*) %[(%a[%a-]+)%]'
3+
local groups = { 'file', 'lnum', 'col', 'end_lnum', 'end_col', 'severity', 'message', 'code' }
4+
local severities = {
5+
error = vim.diagnostic.severity.ERROR,
6+
warning = vim.diagnostic.severity.WARN,
7+
note = vim.diagnostic.severity.HINT,
8+
}
9+
10+
return {
11+
cmd = 'dmypy',
12+
stdin = false,
13+
ignore_exitcode = true,
14+
args = {
15+
'run',
16+
'--timeout',
17+
'50000',
18+
'--',
19+
'--show-column-numbers',
20+
'--show-error-end',
21+
'--hide-error-context',
22+
'--no-color-output',
23+
'--no-error-summary',
24+
'--no-pretty',
25+
'--python-executable',
26+
function()
27+
return vim.fn.exepath 'python3' or vim.fn.exepath 'python'
28+
end
29+
},
30+
parser = require('lint.parser').from_pattern(
31+
pattern,
32+
groups,
33+
severities,
34+
{ ['source'] = 'dmypy' },
35+
{ end_col_offset = 0 }
36+
),
37+
}

spec/dmypy_spec.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
describe('linter.dmypy', function()
2+
it('can parse the output', function()
3+
local parser = require('lint.linters.dmypy').parser
4+
local bufnr = vim.uri_to_bufnr('file:///foo.py')
5+
local output = [[
6+
/foo.py:10:15:10:20: error: Incompatible return value type (got "str", expected "bool") [return-value]
7+
/foo.py:20:25:20:30: error: Argument 1 to "foo" has incompatible type "str"; expected "int" [arg-type]
8+
]]
9+
local result = parser(output, bufnr)
10+
11+
assert.are.same(2, #result)
12+
13+
local expected_error = {
14+
source = 'dmypy',
15+
message = 'Incompatible return value type (got "str", expected "bool")',
16+
code = 'return-value',
17+
lnum = 9,
18+
col = 14,
19+
end_lnum = 9,
20+
end_col = 20,
21+
severity = vim.diagnostic.severity.ERROR,
22+
user_data = {
23+
lsp = {
24+
code = 'return-value'
25+
}
26+
}
27+
}
28+
assert.are.same(expected_error, result[1])
29+
30+
local expected_warning = {
31+
source = 'dmypy',
32+
message = 'Argument 1 to "foo" has incompatible type "str"; expected "int"',
33+
code = 'arg-type',
34+
lnum = 19,
35+
col = 24,
36+
end_lnum = 19,
37+
end_col = 30,
38+
severity = vim.diagnostic.severity.ERROR,
39+
user_data = {
40+
lsp = {
41+
code = 'arg-type'
42+
}
43+
}
44+
}
45+
assert.are.same(expected_warning, result[2])
46+
end)
47+
end)

0 commit comments

Comments
 (0)