yapf icon indicating copy to clipboard operation
yapf copied to clipboard

`column_limit = 0` setting produces `ZeroDivisionError`

Open alexreg opened this issue 4 years ago • 3 comments

If I set column_limit = 0 in my settings file, then I the following exception is raised. I was trying to disable a column limit (this would be a mildly nice feature), but regardless of whether that is allowed, I think it's safe to say that raising an exception in this circumstance is a bug.

Traceback (most recent call last):
  File "/usr/local/bin/yapf", line 8, in <module>
    sys.exit(run_main())
  File "/usr/local/lib/python3.9/site-packages/yapf/__init__.py", line 362, in run_main
    sys.exit(main(sys.argv))
  File "/usr/local/lib/python3.9/site-packages/yapf/__init__.py", line 126, in main
    changed = FormatFiles(
  File "/usr/local/lib/python3.9/site-packages/yapf/__init__.py", line 204, in FormatFiles
    changed |= _FormatFile(filename, lines, style_config, no_local_style,
  File "/usr/local/lib/python3.9/site-packages/yapf/__init__.py", line 227, in _FormatFile
    reformatted_code, encoding, has_change = yapf_api.FormatFile(
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/yapf_api.py", line 94, in FormatFile
    reformatted_source, changed = FormatCode(
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/yapf_api.py", line 166, in FormatCode
    reformatted_source = reformatter.Reformat(
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/reformatter.py", line 94, in Reformat
    if not _AnalyzeSolutionSpace(state):
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/reformatter.py", line 501, in _AnalyzeSolutionSpace
    count = _AddNextStateToQueue(penalty, node, False, count, p_queue)
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/reformatter.py", line 529, in _AddNextStateToQueue
    must_split = previous_node.state.MustSplit()
  File "/usr/local/lib/python3.9/site-packages/yapf/yapflib/format_decision_state.py", line 536, in MustSplit
    if (self.column_limit - self.column) / float(self.column_limit) < 0.3:
ZeroDivisionError: float division by zero

alexreg avatar Sep 20 '21 23:09 alexreg

Hmm...Using 0 to indicate that no column limit applies is an interesting feature. However, it could backfire. If you have a large data literal, it'll be placed on one line. I'm not sure if that's preferable...

bwendling avatar Nov 04 '21 07:11 bwendling

@gwelymernans Could always add another option to toggle whether to apply it to data literals or simply leave those alone (as-is).

alexreg avatar Nov 04 '21 15:11 alexreg

Using 0 to indicate that no column limit applies is an interesting feature.

Would be greatly appreciated if this could be implemented. 🙏

lewis-yeung avatar May 26 '22 14:05 lewis-yeung