asottile / pyupgrade

A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.
MIT License
3.62k stars 183 forks source link

Versioned branch fixer removes comments after `if` block #970

Closed adamchainz closed 1 month ago

adamchainz commented 1 month ago

The 'versioned branch' fixer currently removes comments after a dropped if block:

 import sys

-if sys.version_info < (3, 2):
-    print("3.2")
-
-# let's go
 print("ok")

The comment is outside of the block and should be preserved.

(Just fixed a similar issue in django-upgrade: https://github.com/adamchainz/django-upgrade/issues/495 .)

asottile commented 1 month ago

there's a duplicate somewhere. this is relatively unfixable because there's no definitive end of a block

adamchainz commented 1 month ago

Isn't this sufficient? #971 . It seems to, at least.

BonaFideIT commented 1 month ago

This could be the duplicate issue which @asottile mentioned: https://github.com/asottile/pyupgrade/issues/931.

But I think @adamchainz is right, @asottile can you recheck this please?

asottile commented 1 month ago

there's a case that this creates a syntax error, I can't remember the exact one