astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.42k stars 1.08k forks source link

PIE790 fix can make non-docstrings into docstrings #12616

Open dscorbett opened 3 months ago

dscorbett commented 3 months ago

There is one case where the fix for PIE790 is not completely safe: it changes behavior when the placeholder statement is blocking a string from being a docstring.

$ ruff --version
ruff 0.5.5
$ cat pie790.py
pass
"docstring?"
print(f"{__doc__=}")
$ python pie790.py
__doc__=None
$ ruff check --isolated --fix --select PIE790 pie790.py
Found 1 error (1 fixed, 0 remaining).
$ python pie790.py
__doc__='docstring?'
MichaReiser commented 3 months ago

Interesting. It shouldn't be to hard to not flag the rule in this case. Just wondering, what's the real world code where you found this issue because a string literal probably indicates a missing assignment (or return, ...).?

dscorbett commented 3 months ago

This is just something theoretical I noticed while reading the rule description. I agree that anything that looks like my sample code probably has a bug. I think PIE790 should still flag it, just not automatically fix it.