python-trio / python-trio/flake8-async

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

Open
#193 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
26
Forks
10
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing test test_line_numbers_match_end_result and trace Plugin.from_source, initialize_options, and plugin.run. Check how autofixers and visitors are sequenced and how line markers are retained. Done means autofixes run before reparsing, visitors see the transformed source, and the assertion reports the correct line for trio.sleep(0).

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.