Skip to content

Conversation

@akoeplinger
Copy link
Contributor

@akoeplinger akoeplinger commented Nov 25, 2025

Follow-up to #20630

The fix didn't fully work since when we raise the ImportError in find_module on L302 we don't pass a named argument into the format string which causes a KeyError:

raise ImportError(_ERR_MSG.format(name), name=name)

We need to use a format string without named arguments, like Python 3.13 and earlier did.

/cc @tausbn

Here's a stack trace from the original error I hit (this is from a pipeline running inside MSFT 1ES PT):

[2025-11-24 12:01:17] [build-stdout] [ERROR] [6] Failed to extract file D:\a\_work\1\s\eng\common\cross\install-debs.py: 'name'
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\worker.py", line 263, in _extract_loop
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\extractors\super_extractor.py", line 34, in process
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\extractors\py_extractor.py", line 44, in process
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\extractors\py_extractor.py", line 30, in _get_module_and_imports
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\extractors\py_extractor.py", line 30, in <genexpr>
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\python\imports.py", line 147, in parse_imports
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\python\imports.py", line 174, in _parse_imports_no_init
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\python\imports.py", line 136, in _absolute_import
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\python\finder.py", line 336, in find
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmle\python\finder.py", line 258, in find
[2025-11-24 12:01:17] [build-stdout] [TRACEBACK] [6] "semmleFile "D:\a\_work\_temp\codeql3000\distribution\codeql\python\tools\python3src.zip\imp.py", line 302, in find_module
Follow-up to github#20630

The fix didn't fully work since when we raise the ImportError in `find_module` we don't pass a named argument into the format string which causes a `KeyError`.

We need to use a format string without named arguments, like Python 3.13 and earlier did.
@akoeplinger akoeplinger requested a review from a team as a code owner November 25, 2025 11:40
Copilot AI review requested due to automatic review settings November 25, 2025 11:40
@akoeplinger akoeplinger requested a review from a team as a code owner November 25, 2025 11:40
Copy link
Contributor

Copilot AI left a comment

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 fixes a KeyError that occurs in Python 3.14 when the find_module function raises an ImportError. The issue was introduced in a previous fix that used a named format placeholder {name!r} but the code calls .format(name) with a positional argument instead of a keyword argument.

  • Changed the format string from '{name!r}' to '{!r}' to accept positional arguments

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! ❤️
I took the liberty of adding a commit that bumps the extractor version and adds a change note.

@akoeplinger
Copy link
Contributor Author

Sure, thank you :)

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

2 participants