Skip to content

Fix bin.addheader bug causing crash on modules with %s in module body - #682

Open
jfeo wants to merge 4 commits into
nextfrom
fix/addheader_error_on_modules_with_format_strings
Open

jfeo wants to merge 4 commits into
nextfrom
fix/addheader_error_on_modules_with_format_strings

Conversation

@jfeo

@jfeo jfeo commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Fix bin.addheader bug causing it to crash on modules with %s anywhere
in the module body.

Also adds fundamental testing of the bin.addheader.main function, and some
support functions in tests.support.iosupp for working reading and writing files
and filetrees in tests.

…anywhere in the module body

Also adds fundamental testing of the `bin.addheader.main` function, and some support functions for working with io
@jfeo
jfeo requested a review from a team September 30, 2026 11:27
@jfeo jfeo self-assigned this Sep 30, 2026
@jfeo jfeo added the bug Something isn't working label Sep 30, 2026
Comment thread bin/addheader.py

@jonasbardino jonasbardino 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.

The fix looks right and good idea to add unit tests while at it.

A number of GH actions failed so I'd be nice if you could either look into fixing or commenting on them before I proceed with the review. We have seen false positives e.g. from unrelated lint warnings in the past but it doesn't look like just that here.

@jfeo

jfeo commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

The fix looks right and good idea to add unit tests while at it.

A number of GH actions failed so I'd be nice if you could either look into fixing or commenting on them before I proceed with the review. We have seen false positives e.g. from unrelated lint warnings in the past but it doesn't look like just that here.

I'm investigating this. I ran the jobs locally with act successfully. It appears that checkout/v4 falls back to using the GitHub REST API because rocky9/rocky10 has an old version of git, and I wonder if that could be related to the import issue.

@jfeo

jfeo commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

The fix looks right and good idea to add unit tests while at it.
A number of GH actions failed so I'd be nice if you could either look into fixing or commenting on them before I proceed with the review. We have seen false positives e.g. from unrelated lint warnings in the past but it doesn't look like just that here.

I'm investigating this. I ran the jobs locally with act successfully. It appears that checkout/v4 falls back to using the GitHub REST API because rocky9/rocky10 has an old version of git, and I wonder if that could be related to the import issue.

I found the reason.

The issue is that checkout/v4 under Rocky9/Rocky10 (git version < 2.18) fall back to the GitHub Rest API and downloads an archive tarball, which respects the export-ignore rule for bin/addheader.py in .gitattributes

jfeo added 2 commits October 2, 2026 10:51
…y in rocky9/rocky10 CI

This is done because the CI `checkout` action falls back to using the GitHub REST API when run under Rocky9/Rocky10, and the REST API respects `.gitattributes`, thus excluding `bin/addheader.py`.
Comment thread .gitattributes
def add_header(modulename: str, modulebody: str) -> str:
"""Naive function to add a header, used for generting test data."""
return (
HEADER_FORMAT.format(modulename=modulename, description=DEFAULT_DESC)

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.

We have opted for percent-expansion over other string construction methods so far. I think we need to discuss in the dev team whether or when to stray from that.

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.

Alright. Should I change this printf-style formatting for now then?

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.

The path of least resistance for merging is going with percent expansion. You can still use the dict form to preserve named values.
Feel free to branch the current version with the other review issues addressed and save it for a possible follow up PR only to switch to .format after we have discussed it in the team next week(?)

Comment thread tests/support/iosupp.py
TreeDict = Union[dict[str, "TreeDict"], dict[str, str]]


def write_file(directory: str, name: str, content: str) -> None:

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.

I think we want to discuss strict typing in the dev team before we venture too far down the path of using it.

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.

Okay. So far I have opted to add it when adding or modifying code, since it can co-exist with untyped code.

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.

Yes, I noticed it in other PRs too and while I'm not against the idea and it doesn't hurt as such it still potentially introduces inconsistency. So I still think it's something we want to briefly discuss with pros and cons weighed in the dev team before we decide the path ahead.

import os
import shutil
import tempfile
import unittest

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.

minor: unittest import appears unused now

self.target_dir = tempfile.mkdtemp(prefix=self.id())

def after_each(self) -> None:
shutil.rmtree(self.target_dir)

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.

Hmm, I think I recall that we wipe test locations automatically in other cases to avoid this kind of explicit post-test cleanup, but I might be wrong or perhaps it's specific to the new iosupp module. In any case it would be nice to avoid because it's easy to forget.

import unittest

from bin.addheader import __file__ as addheader_module_file
from bin.addheader import main

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.

nit-pick: should we keep all imports from one module in a single import ?

Comment thread tests/support/iosupp.py
def write_tree(root: str, tree: TreeDict) -> None:
"""
Write the given given tree to the given root directory.
"""

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.

nit-pick: is this black formatting or on purpose? I'd expect shorter than 80 char doc-strings to remain on a single line as PEP8 recommends.

Comment thread tests/support/iosupp.py
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

@jonasbardino jonasbardino Oct 2, 2026 •

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.

nit-pick: this header looks oddly under-filled (significantly shorter than 80 chars). Did you use addheader to generate it or do we have those same overly short lines in another module you reused from?
Same applies to similar occurrences in other files below.



def add_header(modulename: str, modulebody: str) -> str:
"""Naive function to add a header, used for generting test data."""

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.

nit-pick: typo in generating

Comment thread tests/support/iosupp.py

def write_tree(root: str, tree: TreeDict) -> None:
"""
Write the given given tree to the given root directory.

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.

nit-pick: duplicate 'given'

@jonasbardino jonasbardino 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.

Looks good overall, but I think we need to sort out a few things as mentioned in the comments. Also has minor bits and pieces to polish while at it.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants