Skip to content

Clarity on formatting behavior for trailing commas #2195

@C-Loftus

Description

@C-Loftus

My understanding from submitting PRs like #2159 is that the project wants code to be formatted without trailing commas.

As a principle, I am happy to defer on any formatter preferences, but I thought it might be helpful to get clarity on two issues which affect contribution:

  1. I could not find this in the docs/CONTRIBUTING.md
  2. I don't believe that there are any autoformatters which can apply this custom behavior / check for violations automatically. (If I am incorrect, please let me know since that would resolve this issue)
    • This adds extra work for contributors to manually add and maintainers to manually check
    • flake8/ruff have custom hooks to check for the presence of trailing commas but not any to ensure there are none.
    • autopep8, black & ruff all generally are agnostic on this or suggest to add trailing commas for the purpose of cleaner git diffs when editing lists

Context

Before I contribute I try to run an autoformatter like ruff to ensure the maintainers don't need to check for style issues.

I can automatically set the prefered line length and quote style for the project without issue. However, there is no way to set the trailing comma preference.

i.e. something like this

uvx ruff format --line-length 80 pygeoapi/api/__init__.py  --config "format.quote-style = 'single'" && uvx flake8

This is a bit of a headache in large PRs since it requires manually editing code from an autoformatter each time, effectively preventing its use until the final PR.

My understanding is that PEP 8 says trailing commas are optional or encouraged

When trailing commas are redundant, they are often helpful when a version control system is used, when a list of values, arguments or imported items is expected to be extended over time. The pattern is to put each value (etc.) on a line by itself, always adding a trailing comma, and add the close parenthesis/bracket/brace on the next line.

Screenshot from PEP 8
Image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions