Skip to content

refactor: simplify CSVWriter into a single primary function for appending rows - #357

Merged
G-Rath merged 11 commits into
mainfrom
refactor/csv-writer
Aug 25, 2026
Merged

refactor: simplify CSVWriter into a single primary function for appending rows#357
G-Rath merged 11 commits into
mainfrom
refactor/csv-writer

Conversation

@G-Rath

@G-Rath G-Rath commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

When reviewing this class as part of #348, I realized there were some unused and duplicated code, but then later realized ultimately what we're using this for is locking the file to do thread safe writes, and appending rows to a csv file.

In light of that, I've ended up refactoring the class to a single public append_rows function, along with adding tests

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors CSVWriter to remove unused functionality, simplify row-buffering by making add_rows variadic, and adds unit tests to validate CSV output behavior.

Changes:

  • Simplifies CSVWriter by removing unused methods and making add_rows(*rows) variadic.
  • Updates call sites to use the new add_rows shape (including *audit_result where appropriate).
  • Adds a dedicated tests/test_output.py suite covering CSV write behavior (no rows, header, BOM, multiple writes, column ordering).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/test_output.py Adds tests validating CSVWriter behavior for headers/BOM/multiple writes/column ordering.
src/output.py Refactors CSVWriter API (variadic add_rows, simplified file writing, internal lock helper).
src/crawler.py Updates CSV writing call sites to use the new add_rows API.
src/audit_manager.py Updates results writing to splat list results into add_rows, and adds a (currently flawed) type annotation.
Suppressed comments (1)

src/output.py:52

  • The add_rows docstring still describes a single list[...] argument, but the method is now variadic (*rows). This makes the doc misleading for callers and for generated docs.
  def add_rows(self, *rows: dict[Any, Any]) -> None:
    """Add a list of rows to the CSV row buffer.

    Args:
        rows (list[dict[Any, Any]]): list of rows of data
    """

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/audit_manager.py Outdated
Comment thread src/output.py
Comment on lines +68 to 72
file_already_exists = os.path.exists(path)
with open(path, 'a', encoding='utf-8-sig') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=keys)
if not file_exists:
if not file_already_exists:
writer.writeheader()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't new, though I might address it as part of dealing with #348 (comment)

@G-Rath G-Rath changed the title refactor: simplify CSVWriter refactor: simplify CSVWriter into a single primary function for appending rows Aug 24, 2026
@G-Rath
G-Rath requested a lite review from Copilot August 24, 2026 22:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread src/output.py
Comment on lines +43 to +47
def append_rows(self, path: str, *rows: dict[Any, Any]) -> None:
"""Add a list of rows to a CSV file.

Args:
path (str): path to file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot what's the right way to document variadic params in Python docstrings?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the starred name in the docstring arg list, e.g. *rows (dict[str, Any]): one or more rows of data (or “variable number of row dicts”). I’ve updated append_rows to that form and tightened the type hints in commit 7fb39b5.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked you a question, not to do an action 🤦

@G-Rath
G-Rath force-pushed the refactor/csv-writer branch from 7fb39b5 to 07f9adf Compare August 24, 2026 22:17
@G-Rath
G-Rath merged commit 1a59e63 into main Aug 25, 2026
12 checks passed
@G-Rath
G-Rath deleted the refactor/csv-writer branch August 25, 2026 22:47
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.

4 participants