psf/black

Re-condensation / Simplification Of Code After Line Length Reduction

Closed this issue · 8 comments

Generally i've come to really love the black formatter, so first off thanks for that!

There is really only one thing I dont like about it which is that if the length of a line is reduced such that it could be put simply on one line, the nested format remains and leaves sometimes goofy looking artifacts.

I believe the black formatter would be better off it attempted to (perhaps via option) to first eliminate any line nesting or extra space then performed a format.

Example - a list comprehension with conditional logic removed

  existing = list(
      [bw.uuid for bw in session.query(Benchmark_Whitelist_DB).all()]
  )

Could easily be replaced with:

  existing = [bw.uuid for bw in session.query(Benchmark_Whitelist_DB).all()]

No doubt that is more readable, and I think this would reduce some of the opposition to regular use of black.

I think this case could be isolated for strings of a certain line length, between the tab and a colon?

I feel like someone has to have suggested this before?

Your example suggests to remove a list() call. That would change the structure of the code, so it goes beyond what Black can do as an autoformatter. Part of Black's goal is to make it very safe to use by not changing the meaning of code. Changes like your list() suggestion are a better fit for a linter like Ruff.

I see your point, I dont spend alot of time thinking about text formatting but i'm glad someone does: )

I think my second example is bad per your response.

What about, this should still be possible recondensed into a single line?

  existing = list([bw.uuid for bw in session.query(Benchmark_Whitelist_DB).all()])

Black will generally merge things back into a single line if they fit within the configured maximum line length, yes.

Ah great then I'll close this since it seems like it seems this has already been considered. Thanks

There are some cases where you'll need to use the --skip-magic-trailing-comma flag for Black to merge things back into a single line

Reopening this as I think there may be some issues where line-condensation doesn't work appropariately.

Example 1 Black Formatted

            self.smx_etfs = disk_cache_function( #EstimatedFundData)
                EstimatedFundData,
                static_dict = SmartCache(
                    **{
                        "disk_cache_path": os.path.join(
                            disk_cache_path, "cache_etfs.db"
                        ),
                        "memitems": 100,
                        "memtime": 3600*48,
                        "disk_size_limit": 5e9,
                    },
                ),
            )

What should exist (i think) maybe there is a rule about trailing line length.

            self.smx_etfs = disk_cache_function( #EstimatedFundData)
                EstimatedFundData,
                static_dict = SmartCache(
                        **{"disk_cache_path": os.path.join(disk_cache_path, "cache_etfs.db"),
                        "memitems": 100,
                        "memtime": 3600*48,
                        "disk_size_limit": 5e9,
                    },
                ),
            )

Another example which I see most of the time

            self.smx_intraday_source = self.refreshDataSource(
                ModelIntradayDataSource
            )

Should be

            self.smx_intraday_source = self.refreshDataSource(ModelIntradayDataSource)

What would the rule about trailing line length be? As JelleZijlstra said above, if a line fits into the configured line length, it will be condensed. This does coincidentally include your second example, as with 3 levels of indentation the condensed line is 86 chars long, within the default 88 char limit. playground link

Your first example would have back condense the line to a length of 93, which is why it doesn't happen. Wherever possible, black will fit code into the line length limit.

If you want your code to be compressed more horizontally, you can always raise the line length limit, or change to a more flat coding style with less indentations.

@MeGaGiGaGon you are correct this is my mistake. The rulers in my vscode instance were set differently than the black input of 80.

Thanks for your time and response.