Closed Zac-HD closed 1 year ago
Looks like libcst
doesn't care to distinguish docstrings from strings. But I can write logic that checks strings if they are the first statement in a ClassDef
, FunctionDef
or Module
.
(to be precise, a SimpleString
[which uses """
], that is the first Expr
in the body of a SimpleStatementLine
, which is the first node in the body of an IndentedBlock
, which is the body of a ClassDef
or FunctionDef
. (with Module
docstrings skipping the last check as its body doesnt have an IndentedBlock
))
Sounds good to me!
actually, thinking about this one more - I'm not sure I see a strong reason why docstrings should be treated differently to other strings? Maybe in this case you only want to write invalid code in normal strings, but I could see wanting formatting in normal strings as well as wanting to write invalid code in a docstring.
I know you've expressed hesitations against #noqa
/# fmt: off
/other configuration - but I think that's maybe a better solution to this. Much more general, and doesn't start specialcasing different types of strings.
Either way probably need to move the docshed invocation though
actually no, changing string literals should definitely be a no-no. But I'm still in favor of some type of # shed: skip
(in this case so you can skip code in docstrings)
The status quo makes it impossible to write large string literals which include syntax-highlighted demonstrations of bad code formatting, which we might want to do! We should therefore ensure that we only format docstrings, not string literals in general.
Unfortunately that probably means moving this pass to
--refactor
for.py
files, so that we can use libcst to applydocshed()
selectively.