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

feat: add mypy,mypyc,dmypy #31

Merged
merged 3 commits into from
Oct 23, 2023
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
- [ ] [shellcheck](https://github.com/koalaman/shellcheck)
- [ ] [stylelint](https://stylelint.io/)
- [x] [ruff](https://github.com/astral-sh/ruff)
- [x] [mypy](https://mypy.readthedocs.io/en/stable/index.html)
- [x] [mypyc](https://mypyc.readthedocs.io/en/latest/index.html)
- [x] [dmypy](https://mypy.readthedocs.io/en/stable/mypy_daemon.html)

## License MIT
3 changes: 3 additions & 0 deletions lua/guard-collection/linter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ return {
shellcheck = require('guard-collection.linter.shellcheck'),
stylelint = require('guard-collection.linter.stylelint'),
ruff = require('guard-collection.linter.ruff'),
mypy = require('guard-collection.linter.mypy').mypy,
mypyc = require('guard-collection.linter.mypy').mypyc,
dmypy = require('guard-collection.linter.mypy').dmypy,
}
78 changes: 78 additions & 0 deletions lua/guard-collection/linter/mypy/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local lint = require('guard.lint')

return function(cmd, args)
local opts = {
offset = 1,
severities = {
error = lint.severities.error,
warning = lint.severities.warning,
note = lint.severities.info,
},
source = 'mypy',
}

-- see spec for pattern examples
local full = {
regex = '([^:]+):(%d+):(%d+):(%d+):(%d+): (%a+): (.*) %[([%a-]+)%]',
groups = { 'filename', 'lnum', 'col', 'end_lnum', 'end_col', 'severity', 'message', 'code' },
}

-- no err code
local no_err = {
regex = '([^:]+):(%d+):(%d+):(%d+):(%d+): (%a+): (.*)',
groups = { 'filename', 'lnum', 'col', 'end_lnum', 'end_col', 'severity', 'message' },
}

return {
cmd = cmd,
args = args,
fname = true,
parse = function(result, bufnr)
local diags, offences = {}, {}

local lines = vim.split(result, '\n', { trimempty = true })

for _, line in ipairs(lines) do
local offence = {}

local groups = full.groups
local matches = { line:match(full.regex) }

if #matches ~= #groups then
matches = { line:match(no_err.regex) }
groups = no_err.groups
end

-- regex matched
if #matches == #groups then
for i = 1, #groups do
offence[groups[i]] = matches[i]
end

offences[#offences + 1] = offence
end
end

vim.tbl_map(function(mes)
local code = mes.code
if not mes.code then
code = ''
end
local diag = lint.diag_fmt(
bufnr,
tonumber(mes.lnum) - opts.offset,
tonumber(mes.col) - opts.offset,
('%s [%s]'):format(mes.message, code),
opts.severities[mes.severity],
opts.source
)

diag.end_col = tonumber(mes.end_col) - opts.offset
diag.end_lnum = tonumber(mes.end_lnum) - opts.offset
diags[#diags + 1] = diag
end, offences)

return diags
end,
}
end
20 changes: 20 additions & 0 deletions lua/guard-collection/linter/mypy/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local base = require('guard-collection.linter.mypy.base')

local args = {
'--hide-error-codes',
'--hide-error-context',
'--no-color-output',
'--show-absolute-path',
'--show-column-numbers',
'--show-error-codes',
'--show-error-end',
'--no-error-summary',
'--no-pretty',
}

local unpack = table.unpack or unpack
return {
mypy = base('mypy', args),
mypyc = base('mypyc', args),
dmypy = base('dmypy', { 'run', '--', unpack(args) }),
}
84 changes: 84 additions & 0 deletions test/linter/mypy_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
describe('mypy', function()
it('can lint', function()
local helper = require('test.linter.helper')
local ns = helper.namespace
local ft = require('guard.filetype')
ft('python'):lint('mypy')
require('guard').setup()

local diagnostics = helper.test_with('python', {
[[def f(i: int) -> int:]],
[[ i += "123"]],
[[ return i]],
[[sum()]],
})
assert.are.same({
{
bufnr = 3,
col = 9,
end_col = 13,
end_lnum = 1,
lnum = 1,
message = 'Unsupported operand types for + ("int" and "str") [operator]',
namespace = 33,
severity = 1,
source = 'mypy',
},
{
bufnr = 3,
col = 0,
end_col = 4,
end_lnum = 3,
lnum = 3,
message = 'All overload variants of "sum" require at least one argument [call-overload]',
namespace = 33,
severity = 1,
source = 'mypy',
},
{
bufnr = 3,
col = 0,
end_col = 4,
end_lnum = 3,
lnum = 3,
message = 'Possible overload variants: []',
namespace = 33,
severity = 3,
source = 'mypy',
},
{
bufnr = 3,
col = 0,
end_col = 4,
end_lnum = 3,
lnum = 3,
message = ' def sum(Iterable[bool], /, start: int = ...) -> int []',
namespace = 33,
severity = 3,
source = 'mypy',
},
{
bufnr = 3,
col = 0,
end_col = 4,
end_lnum = 3,
lnum = 3,
message = ' def [_SupportsSumNoDefaultT <: _SupportsSumWithNoDefaultGiven] sum(Iterable[_SupportsSumNoDefaultT], /) -> _SupportsSumNoDefaultT | Literal[0] []',
namespace = 33,
severity = 3,
source = 'mypy',
},
{
bufnr = 3,
col = 0,
end_col = 4,
end_lnum = 3,
lnum = 3,
message = ' def [_AddableT1 <: SupportsAdd[Any, Any], _AddableT2 <: SupportsAdd[Any, Any]] sum(Iterable[_AddableT1], /, start: _AddableT2) -> _AddableT1 | _AddableT2 []',
namespace = 33,
severity = 3,
source = 'mypy',
},
}, diagnostics)
end)
end)
5 changes: 3 additions & 2 deletions test/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ sudo apt-get install -qqq \
clang-format clang-tidy fish elixir &
luarocks install luacheck &
go install github.com/segmentio/golines@latest &
pip -qqq install autopep8 black djhtml flake8 isort pylint yapf codespell ruff sqlfluff &
go install github.com/golangci/golangci-lint/cmd/[email protected] &
pip -qqq install autopep8 black djhtml flake8 isort pylint yapf codespell ruff sqlfluff mypy &
npm install -g --silent \
prettier @fsouza/prettierd sql-formatter shellcheck shfmt @taplo/cli &
gem install -q rubocop &
# Block, homebrew takes the longest time
brew install \
swiftformat swift-format hadolint google-java-format pgformatter fnlfmt ormolu golangci-lint
swiftformat swift-format hadolint google-java-format pgformatter fnlfmt ormolu

# Install standalone binary packages
bin="/home/runner/.local/bin"
Expand Down
Loading