Comments (6)
There's a workaround if your parser doesn't support comments the way you want: transform the comments into strings with a unique random prefix, run it through your pretty-printer, then transform any strings that have the prefix back into comments.
It's incredibly dirty (you'll need to escape quotes, for example), but matches well with programmers' intuition about comments and ASTs, and is quite easy to implement.
from black.
Comments don't have a place in the grammar. The AST I'm using, lib2to3, is placing them as whitespace prefixes on the next instruction. An explicit goal of Black is to be deterministic by never consulting existing formatting on the file.
Those two things combined together make it very hard for me to support one particular use case: "suffix" comments on blocks. Prefix comments are easy and this is what most code does: standalone comments describe the line behind them, not before them.
I'm not closing this as this might have a solution in the future, but this is not easy to fix.
from black.
If lib2to3 turns out to be too limited, I think you could also hack comment support into a regular parser by doing a second pass scanning for comments (they're included in tokenize
output) and then matching that up with your AST using the line/offset numbers. I think prettier might use this way?
Comment handling is super hard so I sympathize. But I'm really skeptical about whether a real-world formatter can get away with handling comments properly without taking their original spacing into account. For another much simpler example: the way black mangles the vertical spacing around section break comments in this file is pretty annoying. I'm not happy that that file is so complicated that it needs those section breaks, but since I'm reimplementing the huge socket
module interface I can't really change the file, and those comments help make it more manageable.
#22 is another tricky comment handling issue -- I hope it's just a minor bug, but I wouldn't be shocked if it turns out to be unfixable while sticking to lib2to3's grammar.
from black.
The approach suggested by @boppreh seems promising. In Python we could use triple-quoted strings for the temporary replacement, which would dramatically reduce the frequency of needing quoting.
from black.
Here's an example of the replace-twice approach, in this case to avoid processing comments and strings by replacing them identifiers: https://github.com/boppreh/altpython/blob/master/altpython.py
from black.
Here's an example of changing indents of commented code. Subsequently uncommenting for debugging would not work as originally intended.
@@ -22,7 +25,8 @@ class TestBmpReference(PillowTestCase):
im.load()
except Exception: # as msg:
pass
- # print("Bad Image %s: %s" %(f,msg))
+
+ # print("Bad Image %s: %s" %(f,msg))
def test_questionable(self):
""" These shouldn't crash/dos, but it's not well defined that these
@@ -47,35 +51,38 @@ class TestBmpReference(PillowTestCase):
except Exception: # as msg:
if os.path.basename(f) in supported:
raise
- # print("Bad Image %s: %s" %(f,msg))
+
+ # print("Bad Image %s: %s" %(f,msg))
def test_good(self):
from black.
Related Issues (20)
- Changes to files happing quickly after running `black` are not detected HOT 4
- `blank_line_after_nested_stub_class`: blank line got added after a nested class with `...` HOT 1
- Black 23.12.0 requires aiohttp when the d extra isn't installed HOT 3
- `allow_empty_first_line_before_new_block_or_comment` can lead to inconsistent formatting HOT 2
- Unable to install dependency aiohttp using requirements.txt generated by poetry after upgrade to black 23.12.0 HOT 2
- Preview style feedback: Parenthesizing long dict, conditional, and type annotations HOT 6
- Black fails to format complicated f-string HOT 2
- What can I / should I expect when working with huge files (100 MB+)? HOT 1
- `wrap_long_dict_value_in_parens` can introduce unnecessary nesting for dicts in dicts
- Installation and update of black over a docker container HOT 1
- f-string with internal same-quote expression HOT 1
- Error on invalid configuration HOT 6
- Method argument values as multi-line strings and continuation visual indents HOT 1
- Whitespace before `fmt: skip` comment is normalised HOT 8
- Add option for single quoted strings as default HOT 2
- Possible to format a Python code contained in a string variable? HOT 1
- Formatting of long lines only work when line-length is specified HOT 2
- Inconsistent formatting of `**` in tenary expression. HOT 2
- Can not format `method` code when method str parameter length default value too long HOT 5
- INTERNAL ERROR: Black produced invalid code: f-string: expecting '}' HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from black.