ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Q000 is not enforced with `inline-quotes = "single"`

Open Borda opened this issue 9 months ago • 8 comments

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."
)

Borda avatar Apr 29 '24 23:04 Borda

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 avatar Apr 30 '24 03:04 charliermarsh

@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)."
        )

li-boxuan avatar Apr 30 '24 03:04 li-boxuan

https://github.com/zheller/flake8-quotes/, on the other hand, would have caught this line.

li-boxuan avatar Apr 30 '24 03:04 li-boxuan

Yeah, we enforce it on the entire implicit concatenation. See: https://github.com/astral-sh/ruff/issues/2400.

charliermarsh avatar Apr 30 '24 03:04 charliermarsh

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.

li-boxuan avatar Apr 30 '24 03:04 li-boxuan

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?

dhruvmanila avatar Apr 30 '24 07:04 dhruvmanila

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).

charliermarsh avatar Apr 30 '24 16:04 charliermarsh

Thanks, I think we can live with that protocol. @Borda what do you think? Can we close the issue now?

li-boxuan avatar May 01 '24 04:05 li-boxuan

Closing for now as "working as intended", hopefully clarified by the above!

charliermarsh avatar May 04 '24 21:05 charliermarsh