t3rn0 / ast-comments

Extension to the built-in ast module. Finds comments in source code and adds them to the parsed tree.
MIT License
31 stars 10 forks source link

Misplaced inline comment in ast-comments 1.1.1 #21

Closed dvarrazzo closed 1 year ago

dvarrazzo commented 1 year ago

Hello,

In 1.1.1 there is a regression w.r.t. 1.1.0 and an inline comment gets misplaced.

The file in question is this. In 1.1.1, the noqa comment on line 5 ends up associated to the if true at line 444. Using this script for processing:

# test-ast.py

import sys
import ast_comments as ast

tree = ast.parse(sys.stdin.read())
print(ast.unparse(tree))

and processing the file with the two versions:

$ wget -q https://raw.githubusercontent.com/psycopg/psycopg/d0bc9241881161d8aaf22597e3ad007f93d9c516/tests/pool/test_pool_null_async.py

$ pip install ast-comments==1.1.0
...
Successfully installed ast-comments-1.1.0

$ python test-ast.py < test_pool_null_async.py > ast-comments-1.1.0.py

$ pip install ast-comments==1.1.1
...
Successfully installed ast-comments-1.1.1

$ python test-ast.py < test_pool_null_async.py > ast-comments-1.1.1.py

$ diff -u ast-comments-1.1.0.py ast-comments-1.1.1.py

The difference between the files is:

--- ast-comments-1.1.0.py   2023-10-02 11:55:19.532576524 +0200
+++ ast-comments-1.1.1.py   2023-10-02 11:56:22.216400976 +0200
@@ -1,7 +1,7 @@
 import logging
 from typing import Any, Dict, List
 import pytest
-from packaging.version import parse as ver  # noqa: F401  # used in skipif
+from packaging.version import parse as ver
 import psycopg
 from psycopg.pq import TransactionStatus
 from psycopg.rows import class_row, Row, TupleRow
@@ -360,7 +360,8 @@
         assert stats.get('connections_errors', 0) == 0
         assert stats.get('connections_lost', 0) == 0
         assert 200 <= stats['connections_ms'] < 300
-if True:  # ASYNC
+if True:  # noqa: F401  # used in skipif
+    # ASYNC

     @pytest.mark.skipif('sys.version_info < (3, 8)', reason='asyncio bug')
     async def test_cancellation_in_queue(dsn):
t3rn0 commented 1 year ago

Removed 1.1.1 from PyPi. Decided to remove these changes from 1.1.* versions. Plan to bring them back with appropriate fixes in 1.2.0.

dvarrazzo commented 1 year ago

Probably yanking 1.1.1 wasn't necessary, publishing 1.1.2 would have been enough. But thank you for looking into it.