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

nesting of string literals with the same quote type inside f-string #10184

Closed
Zerotask opened this issue Mar 1, 2024 · 5 comments
Closed

nesting of string literals with the same quote type inside f-string #10184

Zerotask opened this issue Mar 1, 2024 · 5 comments
Labels
formatter Related to the formatter preview Related to preview mode features style How should formatted code look

Comments

@Zerotask
Copy link

Zerotask commented Mar 1, 2024

before

email = factory.LazyAttribute(lambda obj: f"info@{obj.name.lower().replace(' ', '')}.de")

after

email = factory.LazyAttribute(lambda obj: f"info@{obj.name.lower().replace(" ", "")}.de")

issue

(tooltip from PyCharm)

Python version 3.11 does not allow nesting of string literals with the same quote type inside f-strings

solution

email = factory.LazyAttribute(lambda obj: f'info@{obj.name.lower().replace(" ", "")}.de')

or don't change the single quotes at all.


ruff 0.2.2 with preview enabled

[tool.ruff]
line-length = 100
output-format = "grouped"
extend-exclude = ["apps/*/migrations/*", "static", "staticfilles", "templates"]
target-version = "py312"
fix = true

[tool.ruff.format]
preview = true
docstring-code-format = true

[tool.ruff.lint]
preview = true
ignore = [
    "E501",
    "S101",
    "S106",
    "RUF012",
    "PT027",
    "ISC001",
    "FBT001",
    "FBT002",
    "FBT003",
    "PLC0415"
]
select = [
    "F",     # Pyflakes
    "E",     # pycodestyle errors
    "W",     # pycodestyle warnings
    "C90",   # mccabe
    "I",     # isort"
    "N",     # pep8-naming
    "UP",    # pyupgrade
    "PL",    # pylint
    "PERF",  # perflint
    "S",     # flake8-bandit
    "FBT",   # flake8-boolean-trap
    "B",     # flake8-bugbear
    "DJ",    # flake8-django
    "ICN",   # flake8-import-conventions
    "ASYNC", # flake8-async
    "C4",    # flake8-comprehensions
    "Q",     # flake8-quotes
    "RET",   # flake8-return
    "SIM",   # flake8-simplify
    "TID",   # flake8-tidy-imports
    "TCH",   # flake8-type-checking
    "PTH",   # flake8-use-pathlib
    "INT",   # flake8-gettext
    "SLOT",  # flake8-slots
    "SLF",   # flake8-self
    "RSE",   # flake8-raise
    "PYI",   # flake8-pyi"
    "PIE",   # flake8-pie"
    "ISC",   # flake8-implicit-str-concat
    "A",     # flake8-builtins
    "COM",   # flake8-commas
    "T10",   # flake8-debugger
    "YTT",   # flake8-2020
    "G",     # flake8-logging-format
    "ERA",   # eradicate
    "RUF",   # ruff
    "FLY",   # flynt
    "FURB",  # refurb
]
@Zerotask Zerotask changed the title quotes replaced inside a f-string nesting of string literals with the same quote type inside f-string Mar 1, 2024
@MichaReiser
Copy link
Member

Is this after you ran ruff format or after you applied a fix? Do you remember the name of the rule that you applied?

@Zerotask
Copy link
Author

Zerotask commented Mar 1, 2024

Yes, it must be ruff format.
If I remove preview = true from format, it doesn't replace the single quotes

@MichaReiser
Copy link
Member

MichaReiser commented Mar 1, 2024

Yeah, that's expected because you configured target-version = "py312", which allows the formatter to re-use the same quotes. However, the style is still in preview and we're open to change how the formatter picks quotes.

What are your reasons for preferring alternating quotes? What's your preferred formatting if the f-string has one more level of nesting?

CC: @dhruvmanila

@MichaReiser MichaReiser added formatter Related to the formatter preview Related to preview mode features style How should formatted code look labels Mar 1, 2024
@Zerotask
Copy link
Author

Zerotask commented Mar 1, 2024

What are your reasons for preferring alternating quotes? What's your preferred formatting if the f-string has one more level of nesting?

I prefer double quotes and I try to avoid nesting f-strings. You are right that with Python 3.12 the f-string behavior has changed. I was just a bit confused at the beginning because PyCharm marked it.
Even though we define Python 3.12 as required, the alternating style would also allow Python 3.11 to work properly (better backward compatibility)

@MichaReiser
Copy link
Member

Even though we define Python 3.12 as required, the alternating style would also allow Python 3.11 to work properly (better backward compatibility)

That's true. I'm leaning towards leaving it as is and allow users to configure whether they want backwards compatibility or not by setting the target-version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter preview Related to preview mode features style How should formatted code look
Projects
None yet
Development

No branches or pull requests

2 participants