Skip to content

Fix TOML comment stripping in extend_list and remove_from_list#1591

Merged
nathanjmcdougall merged 7 commits into
mainfrom
copilot/fix-ruff-toml-comments-issue
Mar 27, 2026
Merged

Fix TOML comment stripping in extend_list and remove_from_list#1591
nathanjmcdougall merged 7 commits into
mainfrom
copilot/fix-ruff-toml-comments-issue

Conversation

Copilot AI commented Mar 27, 2026

Copy link
Copy Markdown
Contributor
  • Investigate the root cause: extend_list and remove_from_list in _file/toml/io_.py replace tomlkit Array objects with plain Python lists, stripping comments and formatting
  • Fix extend_list: use in-place insert() instead of d + list(values) to preserve tomlkit Array metadata
  • Fix remove_from_list: use in-place remove() instead of list comprehension to preserve tomlkit Array metadata
  • Add tests for comment preservation in both extend_list and remove_from_list
  • Add a test for the specific usethis rule --ignore ERA001 scenario with ruff.toml comments
  • Fix add_author to unwrap tomlkit Array items to plain dicts for the delete/re-set dance
  • Update test_append expectation for cosmetic whitespace change (one fewer leading newline)
  • Fix ty type error: use Mapping isinstance check to narrow item type before dict() conversion
  • Fix AGENTS.md table alignment (lines 212-213 trailing pipe misalignment)
  • Run affected tests to verify (all pass)
  • Run static checks: basedpyright (0 errors), ty (all checks passed), ruff (all pass)

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI and others added 2 commits March 27, 2026 10:03
Use in-place append/remove on tomlkit Array objects instead of
creating plain Python lists, which strips comments and formatting.

Fixes #884

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/032e12ce-4fc2-4e64-87e1-f3914b7fcf59

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix usethis rule to preserve comments in ruff.toml Fix TOML comment stripping in extend_list and remove_from_list Mar 27, 2026
Copilot AI requested a review from nathanjmcdougall March 27, 2026 10:09
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review March 27, 2026 10:10
@codspeed-hq

codspeed-hq Bot commented Mar 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/fix-ruff-toml-comments-issue (f8d45fb) with main (ee85faf)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (836ff1a) during the generation of this report, so ee85faf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@nathanjmcdougall

Copy link
Copy Markdown
Collaborator

@copilot the TestAddAuthor.test_append test is failing, please investigate.

… add_author

The in-place Array modification in extend_list now preserves the
tomlkit Array type. The add_author delete/re-set dance needs plain
Python dicts to trigger tomlkit's format conversion to [[project.authors]].

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/6a19efab-dd8b-4ba2-ae5e-1f9670293228

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>

Copilot AI commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot the TestAddAuthor.test_append test is failing, please investigate.

Fixed in f00ffcb. The add_author function's delete/re-set dance relied on the old extend_list behavior of creating a plain Python list (which triggered tomlkit's format conversion to [[project.authors]]). With the in-place Array modification, the tomlkit Array type is now preserved, so add_author needs to explicitly unwrap items to plain dicts via [dict(item) for item in full_raw] before re-setting. The test expectation was also updated for a minor cosmetic whitespace difference (one fewer leading newline).

@codecov

codecov Bot commented Mar 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@nathanjmcdougall

Copy link
Copy Markdown
Collaborator

@copilot the static checks are failing - you need to fix these

…able alignment

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/109d7daa-f84f-425a-b82b-ebb5e8491acf

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>

Copilot AI commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

@copilot the static checks are failing - you need to fix these

Fixed in 282b11a. Two issues:

  1. ty type error in author.py:34dict(item) where item is object doesn't match any dict.__init__ overload. Fixed by narrowing with assert isinstance(item, Mapping) before calling dict(item).

  2. markdownlint AGENTS.md:212-213 — table pipe misalignment (160 chars vs 161 for other rows). This was pre-existing from the merge with main but fixed here by adding a trailing space to align the pipes.

@nathanjmcdougall nathanjmcdougall merged commit ebb0ef2 into main Mar 27, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/fix-ruff-toml-comments-issue branch March 27, 2026 13:17
@nathanjmcdougall nathanjmcdougall linked an issue Mar 30, 2026 that may be closed by this pull request
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.

usethis rule --ignore ERA001 removes comments in ruff.toml

2 participants