Skip to content

Respect padding in pipe/github separator rows#426

Open
kdeldycke wants to merge 1 commit intoastanin:masterfrom
kdeldycke:fix-gfm-separator-padding
Open

Respect padding in pipe/github separator rows#426
kdeldycke wants to merge 1 commit intoastanin:masterfrom
kdeldycke:fix-gfm-separator-padding

Conversation

@kdeldycke
Copy link
Copy Markdown
Contributor

@kdeldycke kdeldycke commented Apr 6, 2026

Follow-up to #410, which gave the github format alignment colons in separator rows by aliasing it to pipe.

This PR fixes a second inconsistency in pipe that #410 inherited: separator rows ignore the format's declared padding.

Problem

The pipe and github formats declare padding=1. Data rows honour this: _pad_row adds a space on each side of cell content. Separator rows do not: _pipe_segment_with_colons fills the full padded width with dashes, swallowing the padding positions.

| item   |     qty |
|:-------|--------:|    <-- no spaces after | or before |
| spam   | 41.9999 |
| eggs   |     451 |

The separator row is the only place in the table where padding=1 is silently ignored.

Fix

After this change, separator rows respect the format's padding, consistent with data rows:

| item   |     qty |
| :----- | ------: |    <-- spaces at padding positions, like data rows
| spam   | 41.9999 |
| eggs   |     451 |

Column widths are preserved: two padding spaces replace two dashes, keeping each segment the same total width.

The implementation adds a padding parameter to _pipe_segment_with_colons. When non-zero, it places spaces at the padding positions instead of filling them with dashes:

Alignment Before (padding ignored) After (padding=1 honoured)
left |:--------| | :------ |
right |--------:| | ------: |
center |:-------:| | :-----: |
none |---------| | ------- |

The padding parameter flows through the existing call chain:

  1. _format_table already has pad = fmt.padding.
  2. _append_line and _build_line now accept an optional padding keyword and forward it to callable line formats.
  3. _pipe_line_with_colons passes it to _pipe_segment_with_colons.

Other callable line formats (_grid_line_with_colons, _html_begin_table_without_header, _latex_line_begin_tabular, _asciidoc_row) accept **kwargs for forward compatibility but do not use the parameter, so their output is unchanged.

Custom padding=0 formats are unaffected

The custom TableFormat with padding=0 shown in #195 (comment) is not affected by this change. That example uses non-callable Line objects (not _pipe_line_with_colons), so _build_line takes the else branch which is untouched. And with padding=0, the _pipe_segment_with_colons path produces zero-width padding strings, which is a no-op.

Original example from @astanin still produces the exact same output:

>>> import tabulate as t
>>> t.MIN_PADDING = 0
>>> github_nopadding = t.TableFormat(
...     lineabove=t.Line("|", "-", "|", "|"),
...     linebelowheader=t.Line("|", "-", "|", "|"),
...     linebetweenrows=None,
...     linebelow=None,
...     headerrow=t.DataRow("|", "|", "|"),
...     datarow=t.DataRow("|", "|", "|"),
...     padding=0,
...     with_header_hide=["lineabove"],
... )
>>> print(t.tabulate([[1,22],[33,4]], headers=["aa","bb"], tablefmt=github_nopadding))
|aa|bb|
|--|--|
| 1|22|
|33| 4|

Discovery context

I found this while investigating an infinite formatting cycle in click-extra between two CI jobs: one regenerates a markdown table using tabulate with tablefmt="github", the other normalizes markdown files with mdformat. Each run undid the other's output on the separator row: tabulate produced |:---|, mdformat normalized it to | :-- |, and the cycle repeated.

The concrete fight is visible in https://github.com/kdeldycke/click-extra/pull/1603/changes#diff-21283f808de8251aeb373ddd7d30d906afbb0f4dfe168733afb14f7ae6137984 (table regeneration strips spaces) and https://github.com/kdeldycke/click-extra/pull/1611/changes#diff-21283f808de8251aeb373ddd7d30d906afbb0f4dfe168733afb14f7ae6137984 (mdformat re-adds them), both touching docs/pygments.md.

Neither tool was wrong: GFM accepts both forms. The root cause was that tabulate's pipe format declared padding=1 but only honoured it for data rows. With this fix, tabulate's output already matches what GFM normalizers expect, so the cycle stops.

History

This is the second half of a fix I started in 2023:

#410 solved the alignment colon problem but did not address the padding inconsistency in separator rows, because the pipe format itself had the same behaviour. This PR completes the chain by making separator rows respect the format's declared padding, just like data rows already do.

kdeldycke added a commit to kdeldycke/click-extra that referenced this pull request Apr 6, 2026
@kdeldycke kdeldycke force-pushed the fix-gfm-separator-padding branch from a16c979 to 1e997d3 Compare April 6, 2026 10:42
@kdeldycke kdeldycke force-pushed the fix-gfm-separator-padding branch from 1e997d3 to e647025 Compare April 6, 2026 10:45
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 6, 2026

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.20%. Comparing base (268615a) to head (e647025).

Files with missing lines Patch % Lines
tabulate/__init__.py 95.45% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #426      +/-   ##
==========================================
+ Coverage   90.18%   90.20%   +0.02%     
==========================================
  Files           3        3              
  Lines         958      960       +2     
  Branches      228      228              
==========================================
+ Hits          864      866       +2     
  Misses         60       60              
  Partials       34       34              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant