Zac-HD / shed

`shed` canonicalises Python code. Shed your legacy, stop bikeshedding, and move on. Black++
https://pypi.org/project/shed/
GNU Affero General Public License v3.0
342 stars 23 forks source link

`docshed` should only apply in docstrings, not other string literals #95

Closed Zac-HD closed 1 year ago

Zac-HD commented 1 year ago

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 apply docshed() selectively.

jakkdl commented 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))

Zac-HD commented 1 year ago

Sounds good to me!

jakkdl commented 1 year ago

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

jakkdl commented 1 year ago

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)