python-trio / flake8-async

Highly opinionated linter for Trio code
https://flake8-async.readthedocs.io
MIT License
17 stars 2 forks source link

first transform, then visit, to not get incorrect line markers #193

Open jakkdl opened 1 year ago

jakkdl commented 1 year ago

low-priority QoL - unless flake8-trio is used in automated environments.

Failing test:

def test_line_numbers_match_end_result():
    text = """import trio
with trio.move_on_after(10):
  ...

trio.sleep(0)
"""
    plugin = Plugin.from_source(text)
    initialize_options(plugin, args=["--enable=TRIO100,TRIO115", "--autofix=TRIO100"])
    errors = tuple(plugin.run())
    new_source = plugin.module.code
    assert errors[1].line == plugin.module.code.split('\n').index('trio.sleep(0)')+1

the assertion fails with 5 != 4 as the line numbers of objects are never updated once parsed when initiating the plugin.

The best way to fix this would probably, as the header suggest, be to first make a pass where all enabled autofixers are run - with autofixed errors being printed out with old and/or no line markers. Then the file is reparsed, and enabled visitors are run on the file.