Skip to content

Commit

Permalink
python: flake8: improve f-strings handling with F821 (neomake#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored Feb 16, 2018
1 parent 8f39323 commit dbd873a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions autoload/neomake/makers/ft/python.vim
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,22 @@ function! neomake#makers#ft#python#Flake8EntryProcess(entry) abort
let line = get(getbufline(a:entry.bufnr, a:entry.lnum), 0, '')
" NOTE: uses byte offset, starting at col means to start after
" the opening quote.
let pos = match(line, '\C\V{'.token, a:entry.col)
let pattern = '\V\C{\.\{-}\zs'.escape(token, '\').'\>'
let pos = match(line, pattern, a:entry.col)
if pos == -1
let line_offset = 0
while line_offset < 10
let line_offset += 1
let line = get(getbufline(a:entry.bufnr, a:entry.lnum + line_offset), 0, '')
let pos = match(line, '\C\V{'.token)
let pos = match(line, pattern)
if pos != -1
let a:entry.lnum = a:entry.lnum + line_offset
break
endif
endwhile
endif
if pos > 0
let a:entry.col = pos + 2
let a:entry.col = pos + 1
let a:entry.length = strlen(token)
endif
endif
Expand Down
14 changes: 13 additions & 1 deletion tests/ft_python.vader
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ Execute (flake8: postprocess for F821 in continuous f-strings):
\ 'bar',
\ 'BAZ = 1',
\ 'foo = (f"prefix {foo}"',
\ ' f"prefix {BAZ} {baz} {obj.attr}")',
\ ' f"prefix {BAZ} {baz} {obj.attr} {f(arg)}")',
\ ])

" Output from flake8:
" t-undefined-in-fstring.py:1:1: F821 undefined name 'bar'
" t-undefined-in-fstring.py:3:9: F821 undefined name 'baz'
" t-undefined-in-fstring.py:3:9: F821 undefined name 'obj'
" t-undefined-in-fstring.py:3:9: F821 undefined name 'f'
" t-undefined-in-fstring.py:3:11: F821 undefined name 'arg'
" t-undefined-in-fstring.py:3:18: F821 undefined name 'foo'

let e = {'lnum': 1, 'bufnr': bufnr, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': 821, 'type': 'F', 'pattern': '', 'text': 'F821 undefined name ''bar'''}
Expand All @@ -148,6 +150,16 @@ Execute (flake8: postprocess for F821 in continuous f-strings):
call neomake#makers#ft#python#Flake8EntryProcess(e)
AssertEqual [e.lnum, e.col, e.length], [4, 30, 3]

" Needs adjustment for second line in f-string, handling func.
let e = {'lnum': 3, 'bufnr': bufnr, 'col': 9, 'valid': 1, 'vcol': 0, 'nr': 821, 'type': 'F', 'pattern': '', 'text': 'F821 undefined name ''f'''}
call neomake#makers#ft#python#Flake8EntryProcess(e)
AssertEqual [e.lnum, e.col, e.length], [4, 41, 1]

" Needs adjustment for second line in f-string, handling func arg.
let e = {'lnum': 3, 'bufnr': bufnr, 'col': 11, 'valid': 1, 'vcol': 0, 'nr': 821, 'type': 'F', 'pattern': '', 'text': 'F821 undefined name ''arg'''}
call neomake#makers#ft#python#Flake8EntryProcess(e)
AssertEqual [e.lnum, e.col, e.length], [4, 43, 3]

" Something that cannot be found.
let e = {'lnum': 3, 'bufnr': bufnr, 'col': 9, 'valid': 1, 'vcol': 0, 'nr': 821, 'type': 'F', 'pattern': '', 'text': 'F821 undefined name ''cannotbefound'''}
call neomake#makers#ft#python#Flake8EntryProcess(e)
Expand Down

0 comments on commit dbd873a

Please sign in to comment.