-
Notifications
You must be signed in to change notification settings - Fork 291
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
Problem with formatting f-strings on python3.12 #712
Comments
It even corrupts sources by adding newlines to fstrings, as can be tested with: https://github.com/stackabletech/image-tools/pull/6/files#diff-cdc0846192c47ce22d8f9a51ee3c0ed9847a222827f12d00a89569b815d92e57R177 |
I'm seeing the same behavior where it's corrupting f-strings by adding newlines inside replacements. Using autopep8 2.0.4 and Python 3.12.0.
|
I don't mean to come across as alarmist, but this is a serious bug. The https://github.com/apache/trafficserver project relies upon autopep8 to format its Python files, and this single issue keeping us from moving to Thank you for this tool. It has been helpful to the Apache Traffic Server project. Kind regards, |
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
There are two issues to be discussed and they are listed in order. 1. First IssueFirst, @amirstm 's example is incorrect. $ echo connector = f"socks5://{user}:{password}:{url}:{port}"
connector = fsocks5://{user}:{password}:{url}:{port}
$ echo connector = f"socks5://{user}:{password}:{url}:{port}" | autopep8 -
connector = fsocks5: // {user}: {password}: {url}: {port} The echo command is not outputting the correct Python f-string example because you did not specify a single quote as an argument. $ echo 'connector = f"socks5://{user}:{password}:{url}:{port}"'
connector = f"socks5://{user}:{password}:{url}:{port}"
$ echo 'connector = f"socks5://{user}:{password}:{url}:{port}"' | autopep8 -
connector = f"socks5://{user}:{password}:{url}:{port}" environment: $ python -V
Python 3.12.0
$ autopep8 --version
autopep8 2.0.4 (pycodestyle: 2.11.1) 2. Second Issue@razvan @supakeen @bneradt Thanks for reporting. #712 (comment) Second, a line break is inserted at the point after the If you have an opinion on this behavior, please let us know. Thanks |
@hhatto correct, I personally feel like There might be some discussion to be had about things that are expected to be formatted inside |
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us.
@hhatto: Thanks for the reply. Just in case it's helpful, autopep8 on Python 3.12 greatly changes the white space within format strings, not only the addition of newlines. It adds or subtracts a variety of types of white space in various places. Here's some samples of a run of it on the apache/trafficserver code base: Removing white spacediff --git a/tests/gold_tests/bad_http_fmt/bad_http_fmt.test.py b/tests/gold_tests/bad_http_fmt/bad_http_fmt.test.py
index 8e84e18e1..059971175 100644
--- a/tests/gold_tests/bad_http_fmt/bad_http_fmt.test.py
+++ b/tests/gold_tests/bad_http_fmt/bad_http_fmt.test.py
@@ -45,7 +45,7 @@ ts.Disk.ip_allow_yaml.AddLines([
' action: allow',
' methods:',
' - GET',
- f' - {random_method}',
+ f' - {random_method}',
]) Adding spaces around colons:diff --git a/tests/gold_tests/autest-site/trafficserver.test.ext b/tests/gold_tests/autest-site/trafficserver.test.ext
index 1ebf39e0d..59de90f49 100755
--- a/tests/gold_tests/autest-site/trafficserver.test.ext
+++ b/tests/gold_tests/autest-site/trafficserver.test.ext
@@ -409,9 +409,9 @@ def MakeATSProcess(obj, name, command='traffic_server', select_ports=True,
port_str += " {ssl_port}:quic {ssl_portv6}:quic:ipv6".format(
ssl_port=p.Variables.ssl_port, ssl_portv6=p.Variables.ssl_portv6)
if enable_proxy_protocol:
- port_str += f" {p.Variables.proxy_protocol_port}:pp {p.Variables.proxy_protocol_portv6}:pp:ipv6"
+ port_str += f" {p.Variables.proxy_protocol_port}: pp {p.Variables.proxy_protocol_portv6}: pp: ipv6"
if enable_tls:
- port_str += f" {p.Variables.proxy_protocol_ssl_port}:pp:ssl {p.Variables.proxy_protocol_ssl_portv6}:pp:ssl:ipv6"
+ port_str += f" {p.Variables.proxy_protocol_ssl_port}: pp: ssl {p.Variables.proxy_protocol_ssl_portv6}: pp: ssl: ipv6"
#p.Env['PROXY_CONFIG_HTTP_SERVER_PORTS'] = port_str
p.Disk.records_config.update({
'proxy.config.http.server_ports': port_str, Addition of newlinesYou mentioned this above: @@ -73,8 +74,9 @@ tr = Test.AddTestRun()
tr.Processes.Default.StartBefore(ts)
tr.Processes.Default.Command = (
server_cmd(1) +
- fr" ; printf 'GET {random_path}HTTP/1.0\r\n" +
- fr"Host: localhost:{Test.Variables.server_port}\r\n" +
+ fr"
+printf 'GET {random_path}HTTP/1.0\r\n" +
+ fr"Host: localhost: {Test.Variables.server_port}\r\n" +
r"X-Req-Id: 0\r\n\r\n'" +
f" | nc localhost {ts.Variables.port} >> client.log" +
" ; echo '======' >> client.log" Here's a particularly extreme example: diff --git a/tests/gold_tests/cache/background_fill.test.py b/tests/gold_tests/cache/background_fill.test.py
index 56125a492..c0d02fbb4 100644
--- a/tests/gold_tests/cache/background_fill.test.py
+++ b/tests/gold_tests/cache/background_fill.test.py
@@ -58,7 +58,7 @@ class BackgroundFillTest:
"dest_ip=* ssl_cert_name=server.pem ssl_key_name=server.key")
self.ts[name].Disk.records_config.update({
- "proxy.config.http.server_ports": f"{self.ts[name].Variables.port} {self.ts[name].Variables.ssl_port}:ssl",
+ "proxy.config.http.server_ports": f"{self.ts[name].Variables.port} {self.ts[name].Variables.ssl_port}: ssl",
"proxy.config.http.background_fill_active_timeout": "0",
"proxy.config.http.background_fill_completed_threshold": "0.0",
"proxy.config.http.cache.required_headers": 0, # Force cache
@@ -111,15 +111,41 @@ class BackgroundFillTest:
tr = Test.AddTestRun()
self.__checkProcessBefore(tr)
tr.Processes.Default.Command = f"""
-curl -X PURGE --http1.1 -vs http://127.0.0.1:{self.ts['for_httpbin'].Variables.port}/drip?duration=4;
-timeout 2 curl --http1.1 -vs http://127.0.0.1:{self.ts['for_httpbin'].Variables.port}/drip?duration=4;
+curl -X PURGE --http1.1 -vs http: //127.0.0.1: {self.ts['for_httpbin'].Variables.port}/drip?duration=4;
+timeout 2 curl --http1.1 -vs http://127.0.0.1:{self.ts['for_httpbin ' ]
+ .
+ V
+ a
+ r
+ i
+ a
+ b
+ l
+ e
+ s
+ .
+ p
+ o
+ r
+ t
+ } /
+ d
+ r
+ i
+ p
+ ?
+ d
+ u
+ r
+ a
+ t
+ i
+ o
+ n
+ =
+ 4
sleep 4; Removing newlinesIn addition to additional newlines, it seems to also sometimes remove newlines: -curl --http1.1 -vsk https://127.0.0.1:{self.ts['for_httpbin'].Variables.ssl_port}/drip?duration=4
-"""
- tr.Processes.Default.ReturnCode = 0
- tr.Processes.Default.Streams.stderr = "gold/background_fill_1_stderr.gold"
- self.__checkProcessAfter(tr)
-
+curl --http1.1 -vsk https://127.0.0.1:{self.ts['for_httpbin'].Variables.ssl_port}/drip?d uration=4 """ tr.Processes.Default.ReturnCode = 0 tr.Processes.Default.Streams.stderr = "gold/background_fill_1_stderr.gold" self.__checkProcessAfter(tr) Adding trailing white spaceNote that the previous patch actually adds a trailing space at the end, which may not render in github. |
autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us. Conflicts: CMakeLists.txt tools/git/pre-commit
* yapf: use yapf instead of autopep8 autopep8 does not support Python 3.12, which is a requirement for moving to fedora:39. The following ticket has been open without comment from the development community for 6 weeks: hhatto/autopep8#712 Transitioning to yapf is easy, and the community behind it seems stronger. I think moving forward it is a better Python formatter for us. Conflicts: CMakeLists.txt tools/git/pre-commit * yapf preconditioning Conflicts: tests/gold_tests/autest-site/trafficserver.test.ext tests/gold_tests/bad_http_fmt/bad_http_fmt.test.py tests/gold_tests/remap/remap_reload.test.py tests/gold_tests/tls/allow-plain.test.py tests/gold_tests/tls/tls_0rtt_server.test.py tests/gold_tests/tls/tls_check_cert_select_plugin.test.py tests/gold_tests/tls/tls_check_dual_cert_selection_plugin.test.py tests/gold_tests/tls/tls_client_versions_minmax.test.py tests/gold_tests/tls/tls_sni_ip_allow.test.py tests/gold_tests/tls/tls_sni_with_port.test.py * formatting file changes.
I'm wondering, is it possible you're running the CLI within a virtualenv with a different version of python? If I run it from the CLI with my default python, 3.12, it still breaks my f-strings. If I have autopep8 installed in a project's virtualenv with python 3.11 there's no problem |
Edit: of course this only applies to Mac, and in particular homebrew python, but hopefully it's helpful If you came here like me in hopes of a workaround, this seems to be working for me: check if 'brew' exists and if the default python3 version is 3.12, and if so, add python 3.11 to start of if _command_exists "brew";
then
# HACK: autopep8 is currently broken on python 3.12
# temporarily set python 3.11 at start of PATH
# https://github.com/hhatto/autopep8/issues/712
if python3 --version | grep -E '^Python 3\.12' >/dev/null;
then
PATH="$(brew --prefix python@3.11)"/libexec/bin:$PATH
export PATH
fi
fi |
One interesting find is that it seems to be virtualenv inside py312 that is causing the issues. I was trying to write a test case for the (excellent btw) tests that autopep8 has and struggled and then noticed:
which looks clearly incorrect. However with the non-virtualenv version:
so here no diff is shown which is expected. |
I just checked an my autopep8 installation is via brew, which wraps autopep8 in python 3.11, so no wonder it works as expected:
For the breaking part, I think pre-commit always runs python in a virtual env, so yes, this matches the observed behaviour of problems with python 3.12 in a virtual env. |
@mvo5 That's a very interesting observation especially as you have the same versions of Python installed in the virtual environment and outside of it. Could you show the output of |
AFAICT the output inside the virtualenv and outside are identical (diff shows no difference). It an interesting issue, I really wonder how the fact that it runs inside/outside a virtualenv can have this effect (and why only on py312). |
I have been hitting similar problem like @mvo5 . I have had identical version in use [1] https://github.com/PyCQA/pycodestyle/blob/main/CHANGES.txt |
Thanks, that is amazing. I think that unblocks us now. |
autopep8 had some issues with python3.12. It seems just moving to the latest version of pycodestyle does fix the issue. Closes: hhatto#712 Thanks: "hruskape" on github
will there be a new release of autopep8 in order to trigger an update of pycodstyle? |
autopep8 v2.1.0 has been released. |
The autopep8 v2.1.0 bugfix doesn't seem cover a few edge cases with pycodestyle 2.11.0 and 2.11.1:
Environment: $ python -V
Python 3.12.3
$ autopep8 --version
autopep8 2.1.0 (pycodestyle: 2.11.1)
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu Noble Numbat (development branch)" I can drop the |
`autopep8` has a bug on Python 3.12 where it may change the contents of f-strings, which breaks our tests which assert that some string is literally equal to another string. hhatto/autopep8#712
Can confirm. Several format strings in I've added |
autopep8 is breaking long lines in a way not compatible with python3.9 hhatto/autopep8#712
Hey.
There is an issue with autopep8 and python3.12 that autopep8 tries to format the content inside the f-strings.
For instance
connector = f"socks5://{user}:{password}:{url}:{port}"
gets formatted to:
connector = f"socks5: //{user}: {password}: {url}: {port}"
The following is another sample:
The text was updated successfully, but these errors were encountered: