Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up to "Unrelated comment was deleted after a change #495" #506

Open
BonaFideIT opened this issue Oct 17, 2024 · 6 comments
Open

Follow up to "Unrelated comment was deleted after a change #495" #506

BonaFideIT opened this issue Oct 17, 2024 · 6 comments

Comments

@BonaFideIT
Copy link
Contributor

BonaFideIT commented Oct 17, 2024

Python Version

3.11.10

Django Version

4.2.15

Package Version

1.22.1

Description

This is a follow up issue from #495.

As mentioned in pyupgrade in asottile/pyupgrade#931, the trailing comments problem is not fixable.

The following test case will fail pass:

def test_removed_block_internal_comment_2():
    check_transformed(
        """\
        import django

        if django.VERSION < (3, 2):
            foo()
            # test comment
        # test comment 2
        """,
        """\
        import django

        # test comment 2
        """,
    )

Should the changes from c219454 be reverted since it does not resolve all possible variants of the problem, @adamchainz what do you think?

@BonaFideIT BonaFideIT changed the title Follow up to https://github.com/adamchainz/django-upgrade/issues/495 Follow up to "Unrelated comment was deleted after a change #495" Oct 17, 2024
@adamchainz
Copy link
Owner

I want to find the time to noodle about this one a bit first before acting. Perhaps there is a solution that we've all missed. I don't think the behaviour affects many cases either way so should be fine to leave as-is for a little bit.

@BonaFideIT
Copy link
Contributor Author

Thanks! If we can help you with this, please do not hesitate to write.

@adamchainz
Copy link
Owner

Well, if you want, learn about tokenization and why the indentation doesn't tell you about comments. Because without looking deeper, it seems to me that the information should be retrievable, since tokenize-rt is able to roundtrip tokens.

Check out https://www.youtube.com/watch?v=G1omxo5pphw for Anthony's intro to using tokens and AST for formatters, how djanog-upgrade and pyupgrade work.

@BonaFideIT
Copy link
Contributor Author

BonaFideIT commented Oct 18, 2024

Ok, I watched the video and I dig into it a bit. I'm wrong your fix works fine, even for the test I mentioned above. So I'm not sure why this would be a problem as I haven't found an edge case where the test won't pass.

@adamchainz
Copy link
Owner

Cool. Maybe there are some tests we could add, at least? :)

@BonaFideIT
Copy link
Contributor Author

If I understand the problem from the ticket here asottile/pyupgrade#931 correctly, removing can lead to invalid syntax. But as far as I can see, these cases cannot be solved automatically anyway. Moreover, they do not apply exactly to this case here.

But it's a good idea to add a few more tests to make sure that everything continues to work in the future. I will open a PR for some tests that might be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants