ruff
ruff copied to clipboard
Q000 is not enforced with `inline-quotes = "single"`
following my comment in https://github.com/astral-sh/ruff/issues/7834#issuecomment-2083830199 and https://github.com/OpenDevin/OpenDevin/pull/1425#discussion_r1582056596
We are using v0.4.1
with preco-commit and setting single-quotes but it is not enforced...
This is our config:
[lint]
select = [
"E", "W", "F",
"Q",
]
[lint.flake8-quotes]
docstring-quotes = "double"
inline-quotes = "single"
[format]
quote-style = "single"
and this is the sample string which is untouched
logger.info(
f"Connecting to {username}@{hostname} via ssh. If you encounter any issues, you can try `ssh -v -p {self._ssh_port} {username}@{hostname}` with the password '{self._ssh_password}' and report the issue on GitHub."
)
I don't think we touch this because it contains single quotes within it, so changing to single quotes would thus require that we escape the inner quotes, and the rule prefers avoiding escapes.
@charliermarsh The line should actually be this:
f"Connecting to {username}@{hostname} via ssh. "
Full context: https://github.com/xingyaoww/OpenDevin/blob/e38b3ab1efdbadb990988318ecd4a3faf741233a/opendevin/sandbox/docker/ssh_box.py#L178-L182
logger.info(
f"Connecting to {username}@{hostname} via ssh. "
f"If you encounter any issues, you can try `ssh -v -p {self._ssh_port} {username}@{hostname}` with the password '{self._ssh_password}' and report the issue on GitHub. "
f"If you started OpenDevin with `docker run`, you should try `ssh -v -p {self._ssh_port} {username}@localhost` with the password '{self._ssh_password} on the host machine (where you started the container)."
)
https://github.com/zheller/flake8-quotes/, on the other hand, would have caught this line.
Yeah, we enforce it on the entire implicit concatenation. See: https://github.com/astral-sh/ruff/issues/2400.
Yeah, we enforce it on the entire implicit concatenation. See: #2400.
Interesting, this "bug fix" seems to be a "bug" to me and the original version seemed more correct 🤣 but I see why you want to choose this.
If one chooses to use single quote, does Q000 attempt to change it to double quotes in this case? From a developer's perspective, I don't care whether it's single quote or double quote - as long as the linter could enforce a consistent behavior, I am happy.
If one chooses to use single quote, does Q000 attempt to change it to double quotes in this case?
No, the rule wouldn't convert a single-quoted string to a double-quoted string if the user preferred the "single" quotes in their config. But, in your case, there's a single quote in the string content which is why Ruff doesn't flag the string. This is to maintain consistency across all the parts of an implicitly concatenated string. For example, the following is inconsistent because some parts uses single quotes while others uses double quotes.
logger.info(
f'Connecting to {username}@{hostname} via ssh. '
f"If you encounter any issues, you can try `ssh -v -p {self._ssh_port} {username}@{hostname}` with the password '{self._ssh_password}' and report the issue on GitHub. "
f"If you started OpenDevin with `docker run`, you should try `ssh -v -p {self._ssh_port} {username}@localhost` with the password '{self._ssh_password} on the host machine (where you started the container)."
)
I think in this case you might get away with using !r
instead of explicit quotes:
logger.info(
f'Connecting to {username}@{hostname} via ssh. '
f'If you encounter any issues, you can try `ssh -v -p {self._ssh_port} {username}@{hostname}` with the password {self._ssh_password!r} and report the issue on GitHub. '
f'If you started OpenDevin with `docker run`, you should try `ssh -v -p {self._ssh_port} {username}@localhost` with the password {self._ssh_password!r} on the host machine (where you started the container).'
)
This should put the quotes around the self._ssh_password
value. For example:
In [3]: print(f'hello {x!r}')
hello 'world'
Does this help?
The behaviors here are intentional and I think we're unlikely to make a bunch of changes to the rule, since we generally recommend using the formatter to enforce this anyway (which also allows specifying single-quote for inline with double-quote for docstrings).
Thanks, I think we can live with that protocol. @Borda what do you think? Can we close the issue now?
Closing for now as "working as intended", hopefully clarified by the above!