pre-commit / pre-commit-hooks

Some out-of-the-box hooks for pre-commit
MIT License
5.2k stars 694 forks source link

`double-quote-string-fixer` does not rewrite "simple" f-strings #1055

Closed mxr closed 3 months ago

mxr commented 3 months ago

The hook rewrites "..." to '...' but does not rewrite f"..." to f'...' (even when there are no string quotes inside the f-string). See the below example:

$ tail $(git ls-files)
==> .pre-commit-config.yaml <==
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
    -   id: double-quote-string-fixer

==> t.py <==
print(f"{0}")
print("0")
$ "${VIRTUAL_ENV}"/bin/pre-commit run --all-files
fix double quoted strings................................................Failed
- hook id: double-quote-string-fixer
- exit code: 1
- files were modified by this hook

Fixing strings in t.py
$ tail t.py
print(f"{0}")
print('0')

(expected f"{0}" to be rewritten to f'{0}' the same way that "0" was rewritten to '0')

My environment:

$ "${VIRTUAL_ENV}"/bin/pre-commit --version
pre-commit 3.7.1
$ "${VIRTUAL_ENV}"/bin/python -c '...' # ran the code from https://github.com/asottile/scratch/wiki/platforms
- **`sys.version`**: `'3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]'`
- **`os.name`**: `'posix'`
- **`sys.platform`**: `'darwin'`
- **`platform.system()`**: `'Darwin'`
- **`platform.python_implementation()`**: `'CPython'`
- **`sys.implementation.name`**: `'cpython'`
- **`sysconfig.get_platform()`**: `'macosx-14.0-arm64'`
asottile commented 3 months ago

yeah unfortunately the updates in python 3.12 meant we can't safely rewrite fstrings at all without potentially breaking older pythons

mxr commented 3 months ago

I see. Running the hook works under py39. Maybe I will try to run the hook under py39 as a workaround