asottile / pyupgrade

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

Formatting bug? Triple-quote changed to 2x triple-quote #923

Closed mpasternak closed 9 months ago

mpasternak commented 9 months ago

Hi there,

either a bug in my code/comments or a bug in pyupgrade.

Please download the file: https://github.com/iplweb/bpp/blob/e7f0c818b43a47f1926b72a42f9d2ac3ad981460/src/bpp/models/jednostka.py#L233

Then format it using pyugprade 3.15.0 and then check the comment near line 234. It will change to:

def pracownicy(self):
        """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
        aktualni pracownicy, obecni pracownicy"""y"""
        return Autor.objects.filter(pk__in=self.aktualni_autorzy(), pokazuj=True)

Is this a known bug? Is there something wrong with my code?

asottile commented 9 months ago

can't reproduce -- must be some other tool that you're confusing for pyupgrade

mpasternak commented 9 months ago

I am able to reproduce it on a clean virtualenvironment with this versions:

[d] pip list                                                       15:39:11
Package     Version
----------- -------
pip         23.3.1
pyupgrade   3.15.0
tokenize-rt 5.2.0

[d] python --version                                               15:39:12
Python 3.12.0

[d] curl "https://raw.githubusercontent.com/iplweb/bpp/e7f0c818b43a47f1926b72a42f9d2ac3ad981460/src/bpp/models/jednostka.py" > jednostka.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16044  100 16044    0     0  65410      0 --:--:--

[d] grep y\" jednostka.py                                          15:41:13
        aktualni pracownicy, obecni pracownicy"""

[d] pyupgrade jednostka.py                                         15:41:15
Rewriting jednostka.py

[d] grep y\" jednostka.py                                          15:41:19
        aktualni pracownicy, obecni pracownicy"""y"""
mpasternak commented 9 months ago

... and I have exactly the same behavior with version installed from git.

Which version of pyupgrade were you using that you're unable to reproduce it?

hugovk commented 9 months ago

I can reproduce, running pyupgrade 3.15.0 on Python 3.12.1 with no options on:

def thing():
    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
    aktualni pracownicy, obecni pracownicy"""
    ...

Gives:

def thing():
    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
    aktualni pracownicy, obecni pracownicy"""y"""
    ...
 def thing():
     """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
-    aktualni pracownicy, obecni pracownicy"""
+    aktualni pracownicy, obecni pracownicy"""y"""
     ...
asottile commented 9 months ago

alright I can with 3.12 now. I had only tried 3.8 and 3.10 and the versions you claimed

next time include your actual information in your report

asottile commented 9 months ago

this is a bug in cpython -- please report there. here's a little testcase you can take too:

import io
import tokenize

src = '''\
def thing():
    """Autorzy, którzy tą jednostkę mają wpisani jako AKTUALNA -- czyli
    aktualni pracownicy, obecni pracownicy"""
    ...
'''
tokens = list(tokenize.generate_tokens(io.StringIO(src).readline))
assert tokens[7].end == (3, 45), tokens[7].end
mpasternak commented 9 months ago

Will do, thanks for taking time to analyze this @asottile & @hugovk

asottile commented 9 months ago

you can work around this by ensuring your docstrings are ascii-only

hugovk commented 9 months ago

Reported to CPython at https://github.com/python/cpython/issues/112943.

mpasternak commented 9 months ago

@hugovk thanks, I planned to report it some time later, but the way you did it is obviously much more professional than I could ever come up with.

I'm just a small developer doing my own thing, I did not expect that such a small minor bug would reach cpython repo. Thank you!